为什么class {int i; };是不是完全符合标准? [英] Why class { int i; }; is not fully standard-conformant?

查看:239
本文介绍了为什么class {int i; };是不是完全符合标准?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个后续问题。

上一个问题,@ JohannesSchaub-litb说,以下代码完全符合标准:

In the previous question, @JohannesSchaub-litb said that the following code is not fully standard-conformant:

class { int i; };  //unnamed-class definition. § 9/1 allows this!

,然后他添加了


在语法上是有效的,它打破了这样的规则,即这样的类必须至少在它的封闭范围中声明一个名称。

while it is grammatically valid, it breaks the rule that such a class must declare at least one name into its enclosing scope.


$ b b

我真的不明白这一点。他在说什么名字?

I couldn't really understand this. What name is he talking about?

任何人都可以进一步阐述这一点(最好是引用标准)?

Could anyone elaborate on this further (preferably quoting the Standard)?

推荐答案

标准的第9条允许 class {public:int i;} (注意没有最后一个分号),因为这个 decl-specifier-seq 用于未命名的类可能用于其他一些结构,如typedef或变量声明。 class {public:int i;}; (注意最后的分号现在存在)的问题是这个类规范现在变成了一个声明。这是根据标准的第7条第3款的非法声明:

Clause 9 of the standard allows class {public: int i;} (note the lack of a final semicolon) because this decl-specifier-seq for an unnamed class might be used in some other construct such as a typedef or a variable declaration. The problem with class {public: int i;}; (note that the final semicolon is now present) is that this class specification now becomes a declaration. This is an illegal declaration per clause 7, paragraph 3 of the standard:


在这种情况下,除了未命名位的声明-field(9.6)中, decl-specifier-seq 应在程序中引入一个或多个名称,或重新声明前一个声明引入的名称。

In such cases, and except for the declaration of an unnamed bit-field (9.6), the decl-specifier-seq shall introduce one or more names into the program, or shall redeclare a name introduced by a previous declaration.

这篇关于为什么class {int i; };是不是完全符合标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆