【发布时间】:2015-10-23 16:41:39
【问题描述】:
考虑以下程序(查看现场演示here)
#include <iostream>
int main()
{
enum days{}d;
std::cout<<sizeof(d);
}
当使用 g++ 4.8.1 编译时,它会在我的本地机器上打印 4 作为输出。这里怎么占4个字节?在给定链接中的 gcc 6.0 上,我也使用了 `-pedantic-option 但它仍然可以正常编译。
那为什么在 C 语言中是不允许的呢?我在 gcc 4.8.1 中尝试了以下程序。 (见现场演示here)
#include <stdio.h>
int main(void)
{
enum days{}d;
printf("sizeof enum is %u",sizeof(d));
}
编译器出现以下错误:
4 12 [Error] expected identifier before '}' token
5 36 [Error] 'd' undeclared (first use in this function)
5 36 [Note] each undeclared identifier is reported only once for each function it appears in
是否允许在 C++ 中有空枚举但在 C 中不允许?
【问题讨论】:
-
C++ 不是 C 的扩展,尽管很多时候它被混淆了。显然有一个非常广泛的共同点,但它们是具有不同规则的不同语言。
-
gcc 不会除了这个。 C++ 符合标准