指定具有特征的模板化类

发布于 2024-09-27 03:01:02 字数 1081 浏览 3 评论 0原文

我有一个指示特征的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

脱离于你 2024-10-04 03:01:02

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

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

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

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 comments

Can you do this? It's compiling on my machine

template<typename T>
struct FooTraits< Fruit<T> >
{
    static const NEbool s_implementsFoo = true;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文