这是一个后续问题。
在 previous question , @JohannesSchaub-litb 说以下代码不完全符合标准:
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.
我真的无法理解这一点。他在说什么名字?
谁能进一步详细说明(最好引用标准)?
最佳答案
标准的第 9 条允许 class {public: int i;}
(注意缺少最后的分号),因为这个 decl-specifier-seq 用于未命名的class 可能用于其他一些构造,例如 typedef 或变量声明。 class {public: int i;};
的问题(注意现在出现了最后的分号)是这个类规范现在变成了一个声明。这是标准第 7 条第 3 款的非法声明:
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.
关于c++ - 为什么类 { int i; };不完全符合标准?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13138605/