C++惯用法:奇特的递归模板模式(Curiously Recurring Template Pattern,CRTP,Mixin-from-above)
来源:互联网 发布:dns 协议端口号为53 编辑:程序博客网 时间:2024/04/30 20:07
#include<stddef.h>
#include<iostream>
template<typename CountedType>
class ObjectCounter{
static size_t count;
protected:
ObjectCounter(){ ++ObjectCounter<CountedType>::count; } //声明为protected,防止生成对象,限定只能被继承
ObjectCounter(const ObjectCounter<CountedType>& ){ ++ObjectCounter<CountedType>::count; }
~ObjectCounter(){ --count; }
public:
static size_t getCount(){ return ObjectCounter<CountedType>::count; }//作为静态函数,类方法
};
template<typename CountedType>
size_t ObjectCounter<CountedType>::count = 0;
template<typename T>
class MyString : public ObjectCounter<MyString<T> >{}; //CRTP
意图:
使用派生类作为模板参数特化基类。
与多态的区别:
多态是动态绑定(运行时绑定),CRTP是静态绑定(编译时绑定)
在实现多态时,需要重写虚函数,因而这是运行时绑定的操作。
然而如果想在编译期确定通过基类来得到派生类的行为,CRTP便是一种独佳选择,它是通过派生类覆盖基类成员函数来实现静态绑定的。
范式:
- class derived : public base<derived>
- {
- // attributes and behaviors
- }
示例代码:
- template <class Derived>
- struct base
- {
- void interface()
- {
- // 转换为子类指针,编译期将绑定至子类方法
- static_cast<Derived*>(this)->implementation();
- }
- static void static_interface()
- {
- // 编译期将绑定至子类方法
- Derived::static_implementation();
- }
- // 下面两个方法,默认实现可以存在,或者应该被继承子类的相同方法覆盖
- void implementation();
- static void static_implementation();
- };
- // The Curiously Recurring Template Pattern (CRTP)
- struct derived_1 : base<derived_1>
- {
- // 这里子类不实现,将使用父类的默认实现
- //void implementation();
- // 此方法将覆盖父类的方法
- static void static_implementation();
- };
- struct derived_2 : base<derived_2>
- {
- // 此方法将覆盖父类的方法
- void implementation();
- // 这里子类不实现,将使用父类的默认实现
- //static void static_implementation();
- };
缺点:
CRTP由于基类使用了模板,目前的编译器不支持模板类的导出,因而不能使用导出接口。
其它使用领域:
在数值计算中,往往要对不同的模型使用不同的计算方法(如矩阵),一般使用继承提供统一接口(如operator运算符),但又希望不损失效率。这时便又可取CRTP惯用法,子类的operator实现将覆盖基类的operator实现,并可以编译期静态绑定至子类的方法。
- C++惯用法:奇特的递归模板模式(Curiously Recurring Template Pattern,CRTP,Mixin-from-above)
- C++惯用法:奇特的递归模板模式(Curiously Recurring Template Pattern,CRTP,Mixin-from-above)
- C++惯用法:奇特的递归模板模式(Curiously Recurring Template Pattern,CRTP,Mixin-from-above)
- C++惯用法:奇特的递归模板模式(Curiously Recurring Template Pattern,CRTP,Mixin-from-above)
- 奇异递归模板模式(Curiously Recurring Template Pattern,CRTP)
- 奇异递归模板模式( Curiously Recurring Template Pattern,CRTP)1
- 递归模板模式--The curiously recurring template pattern
- 关于CRTP(Curiously Recurring Template Prattern)的使用
- curiously recurring template pattern
- Curiously Recurring Template Pattern
- C++惯用法:奇特的递归模板模式
- Curiously recurring template pattern (zz)
- Curiously Recurring Template
- 奇异递归模板模式(CRTP)应用--表达式模板(expression template) 2
- CRTP 奇异递归模板模式
- 奇特递归模板模式
- 模板模式(Template Pattern)
- 模板模式(Template Pattern)
- Windows下搭建Eclipse+Android4.0开发环境
- JasperRunManager、JasperPrintManager、JRLoader、JRSaver的简单用法
- Linux的bg和fg命令 ---让程序在前台后台之间切换
- 2015年2月开始CSDN博客生活
- 函数的意义
- C++惯用法:奇特的递归模板模式(Curiously Recurring Template Pattern,CRTP,Mixin-from-above)
- java动态页面静态化
- cf 401D. Roman and Numbers 数位dp,状压
- 表格分组折叠
- flex mobile性能优化点
- 关于寄存器ESP和EBP的一些理解
- python新式类多重继承 广度优先搜索 C3算法
- hdu 1864 最大报销额
- WebMatrix