为什么`int;`在C中编译好,但在C ++中编译不好? [英] Why does `int ;` compile fine in C, but not in C++?
问题描述
请考虑以下程序(请参阅现场演示此处)。
Consider the following program (see live demo here).
#include <stdio.h>
int main(void)
{
int ; // Missing variable name
puts("Surprise");
}
我的编译器gcc 4.8.1给出了下面的警告:
My compiler, gcc 4.8.1, gives the below warning:
警告无用的类型名称在空声明中[缺省情况下启用]
[Warning] useless type name in empty declaration [enabled by default]
为什么编译好?我不应该得到一个编译器错误?当我将它编译为C ++程序时,g ++ 4.8.1给出了以下错误:
$ b
Why does it compile fine? Shouldn't I get a compiler error? g++ 4.8.1 gives the following error when I compile it as a C++ program:
<错误>声明不声明任何东西[-fpermissive ]
[Error] declaration does not declare anything [-fpermissive]
推荐答案
The C standard says
除static_assert声明以外的声明应至少声明一个声明符(函数的参数或结构或联合的成员除外),标记或枚举的成员。
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++ says
简单声明,只有在声明类(第9章)或枚举时,才可以省略可选的init-declarator-list。
In a simple-declaration, the optional init-declarator-list can be omitted only when declaring a class (Clause 9) or enumeration.
A违反这两种语言都需要诊断。标准不会谈论编译器错误或警告。警告是诊断。
A violation of this in either language requires a diagnostic. The standards do not talk about compiler errors or warnings. A warning is a diagnostic.
这篇关于为什么`int;`在C中编译好,但在C ++中编译不好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!