使用奇怪循环模板模式(CRTP)和其他类型参数
Use Curiously Recurring Template Pattern (CRTP) with additional type parameters
我尝试使用奇怪循环模板模式(CRTP)并提供额外的类型参数:
template <typename Subclass, typename Int, typename Float>
class Base {
Int *i;
Float *f;
};
...
class A : public Base<A, double, int> {
};
这可能是一个bug,更合适的超类应该是Base<A, double, int>
——尽管这个参数顺序不匹配不是那么明显。如果我可以在typedef中使用参数的含义:
template <typename Subclass>
class Base {
typename Subclass::Int_t *i; // error: invalid use of incomplete type ‘class A’
typename Subclass::Float_t *f;
};
class A : public Base<A> {
typedef double Int_t; // error: forward declaration of ‘class A’
typedef int Double_t;
};
然而,这不能在gcc 4.4上编译,报告的错误如上面的注释所示——我认为原因是在创建A之前,它需要实例化Base模板,但这反过来又需要知道A。
在使用CRTP时,是否有一个很好的方法来传递"命名"模板参数?
你可以使用一个trait类:
// Must be specialized for any type used as TDerived in Base<TDerived>.
// Each specialization must provide an IntType typedef and a FloatType typedef.
template <typename TDerived>
struct BaseTraits;
template <typename TDerived>
struct Base
{
typename BaseTraits<TDerived>::IntType *i;
typename BaseTraits<TDerived>::FloatType *f;
};
struct Derived;
template <>
struct BaseTraits<Derived>
{
typedef int IntType;
typedef float FloatType;
};
struct Derived : Base<Derived>
{
};
@James的回答显然是正确的,但是如果用户没有提供正确的typedefs,您仍然可能遇到一些问题。
使用编译时检查工具可以"断言"使用的类型是右。根据您使用的c++版本,您可能必须使用Boost。
在c++ 0x中,这是结合:
-
static_assert
:一个用于编译时检查的新工具,它允许您指定消息 -
type_traits
头,它提供了一些谓词,如std::is_integral
或std::is_floating_point
的例子:
template <typename TDerived>
struct Base
{
typedef typename BaseTraits<TDerived>::IntType IntType;
typedef typename BaseTraits<TDerived>::FloatType FloatType;
static_assert(std::is_integral<IntType>::value,
"BaseTraits<TDerived>::IntType should have been an integral type");
static_assert(std::is_floating_point<FloatType>::value,
"BaseTraits<TDerived>::FloatType should have been a floating point type");
};
这与运行时世界中典型的防御性编程习惯用法非常相似。
实际上你甚至不需要trait类。
template
<
typename T1,
typename T2,
template <typename, typename> class Derived_
>
class Base
{
public:
typedef T1 TypeOne;
typedef T2 TypeTwo;
typedef Derived_<T1, T2> DerivedType;
};
template <typename T1, typename T2>
class Derived : public Base<T1, T2, Derived>
{
public:
typedef Base<T1, T2, Derived> BaseType;
// or use T1 and T2 as you need it
};
int main()
{
typedef Derived<int, float> MyDerivedType;
MyDerivedType Test;
return 0;
}
相关文章:
- 在 c++ 中的模板实例化中使用带有构造函数的类作为类型参数
- 如何解决一元"*"(有"字符")错误的无效类型参数?
- "std::shared_ptr":不是参数"_Ty"的有效模板类型参数
- 具有可变参数非类型参数的模板专用化
- 函数类型参数的模板参数推导
- PowerShell 使用结构类型参数调用 C++ DLL 的导出函数
- 对于非常量指针类型的参数,未调用具有常量指针模板类型参数的功能
- 为模板传递非类型参数 agument
- 为什么带有类型参数的运算符 () 可以应用于 result_of 上下文中的类型?
- 使用其他模板类型参数作为要在函数签名中使用的类型别名声明
- 如何避免具有相同类型参数的函数中的错误
- 将内置类型变量传递给只有一个类类型参数的"+"运算符函数时自动类型转换的构造函数
- c++非类型参数包扩展
- 如何实现对参数顺序不可知的std::same_as的广义形式(即对于两个以上的类型参数)
- 在不同的模板参数包之间分发非类型参数包
- 如何在使用容器和字符串时强制使用显式分配器类型参数
- 错误:一元"*"的类型参数无效(具有"int"):使用 mergesort 计算
- EXPECT_CALL具有 unique_ptr 引用类型参数的模拟函数
- 作为模板类型参数,为什么 type[N] 与其专用版本不匹配----模板<类 T>类 S<T[]>
- 使用奇怪循环模板模式(CRTP)和其他类型参数