c++ - 为什么类 { int i; };不完全符合标准?

标签 c++ class definition identifier standards-compliance

这是一个后续问题。

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/

相关文章:

c++ - std::length_error 尝试从类数组输出字符串时

html - 替换 DOA <di> 标签

c++ - 有没有办法用一行来初始化容器?

c++ - 将参数传递给父类(super class)构造函数

javascript - jQuery - 将函数应用于具有相同类名的多个选择元素

c++ - 使用 decltype() 声明函数签名

scheme - 以下表达式中的值、变量、对象和名称之间有什么区别?

c++ - 是什么导致 WS_TABSTOP 影响窗口绘制顺序?

c++ - OpenCV 中颜色差异的简单度量?

class - 在 TypeScript 中声明动态添加的类属性