我有一个struct
,它指示一个特征:
template<typename T>
struct FooTraits
{
static const NEbool s_implementsFoo = false;
};
我可以使用一个类来专门化它,因此:
class Apple {};
template<>
struct FooTraits< Apple >
{
static const NEbool s_implementsFoo = true;
};
但是,如果我要专门研究的类也已经模板化,那么目前我不能使用
FooTraits
,因此:template< typename T >
class Fruit {};
template<>
struct FooTraits< Fruit >
{
static const NEbool s_implementsFoo = true;
};
我应该如何表达这最后一个代码块,以便任何
Fruit< T >
都有s_implementsFoo = true
?当前报告以下错误:
error C3203: 'Fruit' : unspecialized class template can't be used as a template argument for template parameter 'T', expected a real type
error C2955: 'Fruit' : use of class template requires template argument list
see declaration of 'Fruit'
error C2990: 'FooTraits' : non-class template has already been declared as a class template
see declaration of 'FooTraits'
最佳答案
最初我写的是FooTraits
不依赖模板化的参数,所以为什么要在意识到大脑放屁之前放入模板。那是不满意的事和评论
你能做这个吗?它正在我的机器上编译
template<typename T>
struct FooTraits< Fruit<T> >
{
static const NEbool s_implementsFoo = true;
};
关于c++ - 指定具有特征的模板化类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3915758/