请考虑以下课程:
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.
};产生以下错误类
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 parameterFoo 2010上修复这个问题(即在模板化的VC++中保持内部伪显式的专门化)?发布于 2010-07-18 20:19:34
如何在VC++ 2010上修复这个问题(即在模板化的Foo中保留内部伪显式专门化)?
通过在非模板基类中声明枚举类型,可以使枚举类型不依赖(C++03使嵌套类依赖于#108,但这不包括枚举,但即使这样的代码仍然是合法的)。
struct FooBase {
enum Flags {Bar, Baz, Bax};
};
template<class> class Foo : public FooBase {
template< ::FooBase::Flags, class = void > struct Internal;
// same other stuff ...
};"error class“链接已经描述了错误应该出现的预期情况。错误认为所有依赖类型都是被禁止的,但实际上这是标准中所说的:
与专用的非类型参数相对应的模板参数的类型不应依赖于专门化的参数。
因此,即使名称Flags在某种程度上是依赖的,只要它不依赖于专门化的参数(如"error类“链接中的示例),就不会使其格式不正确。
https://stackoverflow.com/questions/3276596
复制相似问题