如何解决与其他类型参数一起使用“好奇重复模板模式CRTP”
|| 我尝试使用好奇重复模板模式(CRTP)并提供其他类型参数:template <typename Subclass,typename Int,typename Float>
class Base {
Int *i;
Float *f;
};
...
class A : public Base<A,double,int> {
};
这可能是一个错误,更合适的超类将是Base<A,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时,有没有传入“ \ named \”模板参数的好方法?
解决方法
您可以使用特征类:
// 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的答案显然是正确的,但是如果用户未提供正确的typedef,您仍然可能会遇到一些问题。
使用编译时检查工具可以“断言”所使用的类型正确。根据您使用的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\");
};
这与运行时世界中的典型防御性编程习惯非常相似。
,您实际上甚至不需要traits类。以下内容也适用:
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,Derived> BaseType;
// or use T1 and T2 as you need it
};
int main()
{
typedef Derived<int,float> MyDerivedType;
MyDerivedType Test;
return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。