是否允许递归初始化数组?

Is it allowed to initialize array recursively?

我有以下代码片段:

int i[] = {42, i[0]};

是否允许这样的初始化或导致未定义的行为?

三大编译器(gcc、clang、msvc)给我 42 for i[1]。因此看起来是合法的,但我想看到这个案例的标准引用。

是的,定义明确。

int i[] = {42, i[0]};

这是一个聚合1初始化2。聚合初始化遵守这条规则:

[dcl.init.aggr]/6

The initializations of the elements of the aggregate are evaluated in the element order. That is, all value computations and side effects associated with a given element are sequenced before those of any element that follows it in order.


1) http://eel.is/c++draft/dcl.init.aggr#1

2) http://eel.is/c++draft/dcl.init.aggr#3