考虑以下程序(参见现场演示 here)。
#include <stdio.h>
int main(void)
{
int ; // Missing variable name
puts("Surprise");
}
我的编译器 gcc 4.8.1 发出以下警告:
[Warning] useless type name in empty declaration [enabled by default]
为什么编译得很好?我不应该得到编译器错误吗?当我将 g++ 4.8.1 编译为 C++ 程序时,会出现以下错误:
[Error] declaration does not declare anything [-fpermissive]
最佳答案
C 标准说
A declaration other than a static_assert declaration shall declare at least a declarator (other than the parameters of a function or the members of a structure or union), a tag, or the members of an enumeration.
C++ 说
In a simple-declaration, the optional init-declarator-list can be omitted only when declaring a class (Clause 9) or enumeration.
任何一种语言的违规行为都需要进行诊断。这些标准不讨论编译器错误或警告。警告就是诊断。
关于c++ - 为什么 `int ;` 在 C 中编译得很好,但在 C++ 中却不行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33273550/