指定具有特征的模板化类

时间:2010-10-12 14:31:51

标签: c++ templates traits

我有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'

1 个答案:

答案 0 :(得分:3)

最初我写道,FooTraits并不依赖于模板化的论证,所以为什么在我意识到我的大脑放屁之前放入模板。这是downvote和评论

你能做到吗?它正在我的机器上编译

template<typename T>
struct FooTraits< Fruit<T> >
{
    static const NEbool s_implementsFoo = true;
};
相关问题