问题描述
这是一个后续问题。
在,@ 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!
,然后他添加了
$ 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:
这篇关于为什么class {int i; };是不是完全符合标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!