班级声明中的全球资格 班长

发布于 2024-08-31 17:15:46 字数 594 浏览 7 评论 0原文

我们发现了类似于以下内容的内容(不要问...):

namespace N {
    struct A { struct B; };
}

struct A { struct B; };

using namespace N;

struct ::A::B {}; // <- point of interest

有趣的是,这可以在 VS2005、icc 11.1 和 Comeau(在线)上编译良好,但在 GCC 上会失败:

类名的全局限定在“{”标记之前无效

从C++03,附件A,在我看来GCC是正确的:

  • class-head可以由nested-name组成-specifieridentifier
  • nested-name-specifier 不能以全局限定符 (::) 开头
  • ,显然,两者都不能identifier

...或者我忽略了什么?

We found something similar to the following (don't ask ...):

namespace N {
    struct A { struct B; };
}

struct A { struct B; };

using namespace N;

struct ::A::B {}; // <- point of interest

Interestingly, this compiles fine with VS2005, icc 11.1 and Comeau (online), but fails with GCC:

global qualification of class name is invalid before '{' token

From C++03, Annex A, it seems to me like GCC is right:

  • the class-head can consist of nested-name-specifier and identifier
  • nested-name-specifier can't begin with a global qualification (::)
  • obviously, neither can identifier

... or am i overlooking something?

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

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

发布评论

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

评论(1

冷弦 2024-09-07 17:15:46

我认为你说得对:在这种情况下,GCC 严格执行了标准,而其他人则执行得不太严格(看看 问题#355)。

您可以执行以下操作来解决语法的限制

struct identity< ::A >::type::B {}; 

或者您使用显式命名的typedef

typedef ::A AHidden;
struct AHidden::B { };

或者,当然,您可以交换using namespace和嵌套的顺序类定义。请注意,附录 A 仅供参考。规范性文本位于条款 5.1/79 中。

I think you are getting it right: GCC implements the standard to the letter in this case, while the others implement it less strict (have a look at issue #355).

You could do the following to work-around the limitation of the syntax

struct identity< ::A >::type::B {}; 

Or you use an explicit named typedef

typedef ::A AHidden;
struct AHidden::B { };

Or, of course, you exchange the order of using namespace and the nested class definition. Notice that Annex A is informative only. The normative text is at clauses 5.1/7 and 9.

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