理解策略模板的定义
understanding the definition of policy-based template
template <
typename T,
template <class> class OwnershipPolicy = RefCounted, #1
class ConversionPolicy = DisallowConversion, #2
template <class> class CheckingPolicy = AssertCheck,
template <class> class StoragePolicy = DefaultSPStorage
>
class SmartPtr;
Q1>第1行
的语法是什么template <class> class OwnershipPolicy = RefCounted,
为什么不提供如下参数?
template <class T2> class OwnershipPolicy = RefCounted,
Q2 >的区别是什么 # 1 和 # 2 ?
template <class> class OwnershipPolicy = RefCounted,
class ConversionPolicy = DisallowConversion,
为什么其中一行有template<class>
而另一行没有?
template <class> class OwnershipPolicy
是一个模板模板参数。也就是说,OwnershipPolicy
应该是一个接受一个(且只有一个)类型参数的模板。这个参数没有名字,因为不需要它,而且无论如何也不能用它做任何事情。
class ConversionPolicy
等价于typename ConversionPolicy
,即任何普通类型参数。
区别在于你如何使用它。对于模板模板参数,只提供模板的名称,稍后可以使用该名称实例化具体类型。对于typename
,您需要一个具体类型:
template <typename A, template <typename> class B>
struct foo {};
template <typename T>
struct x {};
struct y {};
template <typename T, typename U>
struct z {};
// both of these are valid:
foo<x<int>, x> a;
foo<y, x> b;
// these are not:
foo<y, x<int>> c;
foo<y, y> d;
foo<y, z> e; // z has two template arguments, B must have only one
值得注意的是,这种习惯用法被称为"基于策略的设计"。
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- #定义c-预处理器常量..我做错了什么
- 用C++中的一个变量定义一个常量
- 部分定义/别名模板模板参数
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- #为""定义宏;静态";针对不同的上下文
- 如何确保C++函数在定义之前声明(如override关键字)
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在命名空间中定义函数还是限定函数
- 此代码是否违反一个定义规则
- 编译C++时未定义的引用
- 不同翻译单元中不可重载的非内联函数定义
- 为什么在定义函数之前先声明它
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 如何在Z3中实现自定义简化策略
- 如何创建定义固定值类型的策略模板
- 理解策略模板的定义
- 使用自定义分配器修改增长策略
- Boost Wave自定义输入策略