指定具有特征的模板化类
我有一个指示特征的 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'
I have a struct
which indicates a trait:
template<typename T>
struct FooTraits
{
static const NEbool s_implementsFoo = false;
};
And I can specialize it with a class, thus:
class Apple {};
template<>
struct FooTraits< Apple >
{
static const NEbool s_implementsFoo = true;
};
However currently I cannot use FooTraits
if the class I wish to specialize it on is also templatized, thus:
template< typename T >
class Fruit {};
template<>
struct FooTraits< Fruit >
{
static const NEbool s_implementsFoo = true;
};
How should I express this last block of code so any Fruit< T >
has s_implementsFoo = true
?
Currently the following errors are reported:
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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最初我写的是,
FooTraits
并不依赖于模板化参数,所以为什么在我意识到我的大脑放屁之前放入模板。这就是反对票和评论,你能做到吗?它正在我的机器上编译
Originally I wrote, the
FooTraits
doesn't depend on the templated argument so why put in a template before I realized my brain fart. That's the downvote and the commentsCan you do this? It's compiling on my machine