加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 从属非类型模板参数

发布时间:2020-12-16 03:00:39 所属栏目:百科 来源:网络整理
导读:考虑下列课程: class Foo{ enum Flags {Bar,Baz,Bax}; templateFlags,class = void struct Internal; templateclass unused struct InternalBar,unused {/* ... */}; templateclass unused struct InternalBaz,unused {/* ... */}; templateclass unused st
考虑下列课程:
class Foo
{
  enum Flags {Bar,Baz,Bax};

  template<Flags,class = void> struct Internal;

  template<class unused> struct Internal<Bar,unused> {/* ... */};
  template<class unused> struct Internal<Baz,unused> {/* ... */};
  template<class unused> struct Internal<Bax,unused> {/* ... */};
};

上述课程纲要在VC 2010和Comeau C上进行测试时,按预期方式进行编译和运行.然而,当Foo成为模板本身时,上述代码片断在VC 2010之下.

例如,以下代码段:

template<class> class Foo
{
  // Same contents as the original non-templated Foo.
};

产生以下error class:

C2754: 'Foo<<unnamed-symbol>>::Internal<Bar,unused>' : a partial specialization cannot have a dependent non-type template parameter
C2754: 'Foo<<unnamed-symbol>>::Internal<Baz,unused>' : a partial specialization cannot have a dependent non-type template parameter
C2754: 'Foo<<unnamed-symbol>>::Internal<Bax,unused>' : a partial specialization cannot have a dependent non-type template parameter

有人能用简单的英文解释这里发生了什么吗?
>如何在VC 2010上解决这个问题(即在模板化的Foo中保留内部伪显式专业化)?

解决方法

How can I fix this (i.e.,keep internal pseudo-explicit specializations in a templated Foo) on VC++ 2010?

您可以通过在非模板基类中声明它来使枚举类型不依赖(C 03使得依赖于#108的嵌套类不包括枚举,但即使这样的代码仍然是合法的).

struct FooBase { 
  enum Flags {Bar,Bax};
};

template<class> class Foo : public FooBase {
  template< ::FooBase::Flags,class = void > struct Internal;
  // same other stuff ...
};

“错误类”链接已经给出了应该增加错误的预期情况的描述.错误认为所有依赖类型都是禁止的,但实际上这就是标准说的:

The type of a template parameter corresponding to a specialized non-type argument shall not be dependent on a parameter of the specialization.

因此,即使名称标志将以某种方式依赖,只要不依赖于“错误类”链接的示例中的专业化参数,则不会使其变得不正确.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读