I have the following code snippet:
int i[] = {42, i[0]};
Is such initialization allowed or leads to undefined behaviour?
Three major compilers (gcc, clang, msvc) give me 42
for i[1]
. Hence looks legit, but I would like to see a cite from the standard for this case.
Yes, it is well-defined.
int i[] = {42, i[0]};
This is an aggregate1 initialization2. Aggregate initialization observes this rule:
[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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With