Can a (C/C++) array initialization reference itself?

前端 未结 4 964
南方客
南方客 2021-01-01 10:00

I was wondering about an initialization of the following form:

int  array[] = {
v - 1,
array[0] + 1
} ;

In the initialization of the second

4条回答
  •  孤城傲影
    2021-01-01 10:12

    See 3.3.2 Point of declaration:

    The point of declaration for a name is immediately after its complete declarator (Clause 8) and before its initializer (if any), except as noted below. [ Example:

    int x = 12;
    { int x = x; }
    

    Here the second x is initialized with its own (indeterminate) value. —end example ]

    So you are referring to the array correctly, its name is known after the =.

    Then, 8.5.1 Aggregates:

    An aggregate is an array or a class [...]

    17: The full-expressions in an initializer-clause are evaluated in the order in which they appear.

    However, I see no reference to when the evaluated values are actually written into the array, so I wouldn't rely on this and would even go so far to declare your code as not well defined.

提交回复
热议问题