相关非类型模板参数
考虑以下类:
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 parameter
- 有人能用简单的英语解释这里发生了什么吗?
- 我如何在 VC++ 2010 上解决此问题(即在模板化
Foo
中保留内部伪显式专业化)?
Consider the following class:
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> {/* ... */};
};
The class outline above compiles and functions as expected when tested on VC++ 2010 and Comeau C++. However, when Foo
is made into a template itself, the above snippet breaks under VC++ 2010.
For example, the following snippet:
template<class> class Foo
{
// Same contents as the original non-templated Foo.
};
Yields the following 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
- Can someone explain what is going on here in plain English?
- How can I fix this (i.e., keep internal pseudo-explicit specializations in a templated
Foo
) on VC++ 2010?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过在非模板基类中声明枚举类型来使其不依赖(C++03 使嵌套类依赖于 #108 但这不包括枚举,但即使这样,这样的代码仍然是合法的)。
“错误类”链接已经给出了应该出现错误的预期情况的描述。该错误认为所有依赖类型都是被禁止的,但实际上标准是这样说的:
因此,即使名称
Flags
在某种程度上具有依赖性,只要它不依赖于专业化的参数(如“错误类”的示例中所示),也不会使其格式错误“ 关联。You can make the enumeration type non-dependent by declaring it in a non-template base class (C++03 made nested classes dependent in #108 but that doesn't include enumeration, but even if, such code would still be legal).
The "error class" link already gives a description of the intended cases where the error should be risen. The error thinks that all dependent types are forbidden, but in fact this is what the Standard says:
So even if the name
Flags
would be somehow dependent, that wouldn't make it ill-formed as long as it doesn't depend on a parameter of the specialization like in the example of your "error class" link.