hrr*_*hrr 9 c++ templates named-parameters crtp
我尝试使用奇怪的重复模板模式(CRTP)并提供其他类型参数:
template <typename Subclass, typename Int, typename Float>
class Base {
Int *i;
Float *f;
};
...
class A : public Base<A, double, int> {
};
Run Code Online (Sandbox Code Playgroud)
这可能是一个错误,更合适的超类Base<A, double, int>
- 尽管这个参数顺序不匹配并不是那么明显.如果我可以在typedef中使用name的含义,那么这个bug会更容易看到:
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;
};
Run Code Online (Sandbox Code Playgroud)
但是,这不能在gcc 4.4上编译,报告的错误是作为上面的注释给出的 - 我认为原因是在创建A之前,它需要实例化Base模板,但这又需要知道A.
使用CRTP时是否有传递"命名"模板参数的好方法?
Jam*_*lis 21
你可以使用traits类:
// 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>
{
};
Run Code Online (Sandbox Code Playgroud)
Mat*_* M. 10
@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");
};
Run Code Online (Sandbox Code Playgroud)
这与运行时世界中典型的防御性编程习语非常相似.