c – 从属非类型模板参数
考虑下列课程:
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 有人能用简单的英文解释这里发生了什么吗? 解决方法
您可以通过在非模板基类中声明它来使枚举类型不依赖(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 ... }; “错误类”链接已经给出了应该增加错误的预期情况的描述.错误认为所有依赖类型都是禁止的,但实际上这就是标准说的:
因此,即使名称标志将以某种方式依赖,只要不依赖于“错误类”链接的示例中的专业化参数,则不会使其变得不正确. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |