CRTP -计数个人类和总数

CRTP - Counting Individual Classes and Total Count

本文关键字:人类 CRTP      更新时间:2023-10-16

我最近了解了CRTP,并一直在练习一些有更好文档的代码示例,以便更好地理解它。目前的代码块,我有能够计算不同的派生类的实例,但如果我想找到所有实例的总数,不管类?下面是我的代码:

template <typename CountedType>
class countable {
    static unsigned count_;
public:
    countable() { ++count_; }
    countable(countable const&) { ++count_; }
    ~countable() { --count_; }
    static unsigned n_alive() { return count_; }
};
template <typename CountedType>
unsigned countable<CountedType>::count_ = 0;
template <typename CharT>
class BasicMyString : public basic_string < CharT >, public countable<BasicMyString<CharT>> { };

typedef BasicMyString<char> MyString;
typedef BasicMyString<wchar_t> WMyString;
template <typename T>
class MyVector : public vector<T>, public countable<MyVector<T>> {};
int main() {
    MyString s1, s2;
    WMyString wms;
    MyVector<double> v;
    cout << "Number of MyString: " << MyString::n_alive() << endl;
    cout << "Number of WMyString: " << WMyString::n_alive() << endl;
    cout << "Number of MyVector: " << MyVector<double>::n_alive() << endl;
   //cout << "Total number of objects: " << countable::n_alive() << endl;
}

当前被注释掉的最后一行是我希望能够调用的方式,以查看当前有多少个对象。我知道我可以通过另一种方式找到总数,通过创建一个全局计数器并在类中创建一个新函数来返回全局计数,但我希望能够像这样调用它,而不定义模板化参数。我必须做一个适配器类型的接口还是有一个更简单的方法来实现这一点?

谢谢。

方案1

使countable成为非模板类(如countable_base)的派生类,并将所有计数代码移到基类中。

class countable_base {
    static unsigned count_;
public:
    countable_base() { ++count_; }
    countable_base(countable_base const&) { ++count_; }
    ~countable_base() { --count_; }
    static unsigned n_alive() { return count_; }
};
unsigned countable_base::count_ = 0;
template <typename CountedType>
class countable : public countable_base {};

解决方案2

使countable成为正规类

class countable {
    static unsigned count_;
public:
    countable() { ++count_; }
    countable(countable const&) { ++count_; }
    ~countable() { --count_; }
    static unsigned n_alive() { return count_; }
};
unsigned countable::count_ = 0;

您可以创建一个额外的类来执行计数,这将是您当前模板countable类的基础,如:

class countable_total {
    static unsigned count_;
protected:
    countable_total() { ++count_; }
    countable_total(countable_total const&) { ++count_; }
    ~countable_total() { --count_; }
public:
    static unsigned n_alive() { return count_; }
};
template <typename CountedType>
class countable : public countable_total {
    static unsigned count_;
public:
    countable() { ++count_; }
    countable(countable const&) { ++count_; }
    ~countable() { --count_; }
    static unsigned n_alive() { return count_; }
};
template <typename CountedType>
unsigned countable<CountedType>::count_ = 0;
// in cpp
unsigned countable_total::count_ = 0;