考虑下列课程:
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.