相关非类型模板参数

发布于 2024-09-10 12:52:57 字数 1325 浏览 0 评论 0原文

考虑以下类:

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

  1. 有人能用简单的英语解释这里发生了什么吗?
  2. 我如何在 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

  1. Can someone explain what is going on here in plain English?
  2. How can I fix this (i.e., keep internal pseudo-explicit specializations in a templated Foo) on VC++ 2010?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给妤﹃绝世温柔 2024-09-17 12:52:57

如何在 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 ...
};

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

与专门化的非类型实参相对应的模板参数的类型不应依赖于专门化的参数。

因此,即使名称 Flags 在某种程度上具有依赖性,只要它不依赖于专业化的参数(如“错误类”的示例中所示),也不会使其格式错误“ 关联。

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

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).

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

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

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:

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文