c++ - 为什么 `int ;` 在 C 中编译得很好,但在 C++ 中却不行?

标签 c++ c gcc language-lawyer variable-declaration

考虑以下程序(参见现场演示 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/

相关文章:

c - header .h : No such file or directory even though source and header are in same directory

c++ - 指针算术编译失败,gcc 说类型转换无效

.net - 理解 C++ .Net 中的 String^

c - 为什么要为该数组中的第一个元素打印垃圾?

c++ - GCM 中的 Crypto++ AES 正在生成类似的密文

c - OpenGL4 gl_texture_2d_array不会显示

c - 是否有类似 PPI 或 Perl::Critic 的 C 语言?

c - <sys/queue.h> 库中 TAILQ 用法的最小示例

c++ - 在 OpenCV 中进行边缘检测后找到最佳感兴趣区域

c++ - 重载 'ref(Select::Expressions::Code&)' 的调用在 C++11 中不明确