为什么 "initializer element is not a constant" 是......不再工作了?

Why "initializer element is not a constant" is... not working anymore?

static const int a = 42;
static const int b = a;

我预计此类代码会出现编译错误。初始值设定项必须是常量表达式或字符串文字。存储在类型为 int 且具有 const 类型限定符的对象中的值不是常量表达式。

我用 -Wall -Wextra -pedantic 编译,甚至 -ansi。那么:

令人惊讶的是,以下内容:

static const char * const a = "a";
static const char * const b = a;

对于下面的片段,我认为我 100% 确定它不应该编译:

static const int a[] = { 1, 2, 3 };
static const int b = a[1];

,但是:

我尝试浏览网络以寻求解释,这主要导致从关于非常量初始化器的旧 Whosebug 问题中复制代码并确定它们现在是否有效。我在 gcc 8 changes.

中找不到任何相关内容

我迷路了。这是预期的行为吗?这样的代码应该编译吗? Why/Why 不是吗? gcc7.4 和 gcc8.1 之间有什么变化?这是编译器错误吗?这是编译器扩展吗?

具有静态存储持续时间的对象的初始化器需要由常量表达式组成。作为@EugenSh。在评论中观察到,"constant expression" 是一个定义的术语。具体来说,在C2011中是section 6.6的主题。描述简直

A constant expression can be evaluated during translation rather than runtime, and accordingly may be used in any place that a constant may be.

但细节决定成败。常量表达式的语义细节包含针对特定种类和常量表达式用途的特定规则。

例如,表达式 a 在任何情况下都不是 "integer constant expression",无论 a 的类型或 constness,因此可能不是在标准需要特定种类的常量表达式的地方使用,例如在位域宽度中。

虽然标准没有给它命名,但它为初始化器中的常量表达式提供了稍微宽松的规则,这就是我们在这里考虑的情况:

Such a constant expression shall be, or evaluate to, one of the following:

  • an arithmetic constant expression,
  • a null pointer constant,
  • an address constant, or
  • an address constant for a complete object type plus or minus an integer constant expression.

术语"arithmetic constant expression"和"address constant"也有定义:

An arithmetic constant expression shall have arithmetic type and shall only have operands that are integer constants, floating constants, enumeration constants, character constants, sizeof expressions whose results are integer constants, and _Alignof expressions. [...]

An address constant is a null pointer, a pointer to an lvalue designating an object of static storage duration, or a pointer to a function designator; it shall be created explicitly using the unary & operator or an integer constant cast to pointer type, or implicitly by the use of an expression of array or function type. [...]

None 的各种 b 变量的初始值设定项符合这些规则。指定具有 const 限定类型的对象的左值表达式不属于允许出现在标准对初始化程序要求的任何常量表达式变体中的元素。

标准通常确实允许

An implementation may accept other forms of constant expressions.

,但这并没有覆盖它对出现在初始值设定项中的常量表达式的特定要求。

每个给定的变量声明 b 都违反了出现在约束之外的标准的 "shall" 要求。因此,由此产生的行为是未定义的,但标准不需要诊断。实现可以接受这种形式作为扩展,正如 GCC 8.2 显然所做的那样,并且 GCC 的 -pedantic 选项确保仅在标准要求的情况下进行诊断,其中不包括这些情况。

由于行为未定义,none 观察到的各种实施行为是不合格的。您不能依赖符合规范的实现来拒绝不符合规范的代码。在某些情况下(但不是这些)它必须 诊断 不符合,但即使在这种情况下也允许成功翻译。

I am lost. Is it expected behavior and such code should compile?

不,但是编译失败也不安全。

Why/Why not?

我在上面解释了为什么各种代码不符合标准,因此可能会被符合标准的编译器拒绝。但另一方面,符合标准的编译器不需要拒绝不符合标准的代码。

What changed between gcc7.4 and gcc8.1? Is this a compiler bug? Is this a compiler extension?

GCC 显然实施了扩展。我不确定这是否是故意的,但这肯定是结果。只要行为是人们天真地期望的,它看起来就很自然和良性,除非从 GCC 的角度来看(不)帮助您编写符合规范的代码。