Possible Duplicate:
array initialization, is referencing a previous element ok?
I wonder if its safe to do such initialization in c/c++ standard:
int a = 5;
int tab[] = { a , tab[0] + 1 , tab[1] };
It successfully compiles and executes with gcc 4.5 and clang 2.9, but will it always be true?
Printing this table gives 5 6 6. Its initialized in global scope.
Generally its interesting in both, c and c++, but i want to use it in c++:)
In the C99 standard, it seems that the order of initialization of the members is guaranteed:
But as @Tomalak mentions in the comment, that does not provide a full guarantee for the operation, as the compiler would first evaluate all of the arguments and then apply the results in the previous order. That is, the previous quote does not impose an order between the initialization of
tab[0]and the evaluation of the expressiontab[0]+1that is used to initializetab[1](it only imposes an ordering between the initialization oftab[0]andtab[1])As of the C++ standard, neither in the current standard nor the FDIS of the upcoming C++0x standard seem to have an specific clause defining the order in which the initialization is performed. The only mention of ordering comes from
But that only relates to the order by which the entries in the initializer are written, not how it is actually evaluated.