海湾合作委员会 4.9 和 5.1 reject这个在全局范围内的简单 C99 声明。 Clang 接受它。
const int a = 1, b = a; // error: initializer element is not constant
这么基本的功能怎么能少呢?看起来很简单。
最佳答案
C991 部分 6.6 常量表达式
是控制部分。它在 6
和 7
小节中指出:
6/ An integer constant expression shall have integer type and shall only have operands that are integer constants, enumeration constants, character constants, sizeof expressions whose results are integer constants, and floating constants that are the immediate operands of casts.
Cast operators in an integer constant expression shall only convert arithmetic types to integer types, except as part of an operand to the sizeof operator.
整数和浮点常量的定义在标准的 6.4.4
中指定,并且仅限于实际的值(文字)而不是变量。
7/ More latitude is permitted for constant expressions in initializers. Such a constant expression shall be, or evaluate to, one of the following (a) an arithmetic constant expression, (b) a null pointer constant, (c) an address constant, or (d) an address constant for an object type plus or minus an integer constant expression.
因为 a
不是 6
或 7
小节中的那些东西,所以它不被认为是一个符合标准的常量表达式。
因此,真正的问题不是为什么 gcc
拒绝它,而是为什么 clang
接受它,而这似乎隐藏在 10
小节中> 同一部分的:
10/ An implementation may accept other forms of constant expressions.
换句话说,该标准规定了实现必须允许常量表达式,但不限制实现仅允许。
1 除了允许 _Alignof
和 sizeof
等小事外,C11 与 C11 基本相同。
关于c - GCC 不支持简单的整数常量表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30158413/