CRTP -计数个人类和总数
CRTP - Counting Individual Classes and Total Count
我最近了解了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;
相关文章:
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- CRTP 单一实例不完整类型或非文本类型
- CRTP 中的复制赋值运算符 - gcc vs clang 和 msvc
- 将具有固定签名的自定义函数名称注入 CRTP
- 如何在MISRA C++之后实施CRTP
- 检测使用 CRTP 的类中的成员函数
- 简化使用 CRTP 模式的类的声明
- C++ CRTP initialization
- 概念可以与 CRTP 习语一起使用吗?
- CRTP - 危险的内存访问?
- 将自定义 QMap 模板实例化以人类可读的形式保存在 QSettings 中
- 如何断言 CRTP 的函数为最终函数?
- CRTP 模式不会触发完整的模板实例化
- 在 CRTP 中使用嵌套名称说明符
- CRTP 与基类向量
- 派生的成员 通过指向基、static_cast、crtp、删除模板的指针
- 为什么 CRTP 模板C++给出无效参数错误?
- CRTP相对于抽象类的优势
- 具有unique_ptr的 CRTP 会导致段错误
- CRTP -计数个人类和总数