Initializer syntax error no longer occurs with std :: array

According to the question std :: array C ++ 11 initializer syntax error Unable to assign the copied list to std :: array in this way:

std::array<int, 10> a = {0,1,2,3,4,5,6,7,8,9}; a = {0}; // error: cannot assign to an array from a braced list 

But in fact, I can no longer reproduce this error. My version of GCC is 4.8.2. Here is the code:

 #include <array> int main() { std::array<int, 10> a; a = {1}; return 0; } 

It compiles and runs without errors.

So the question is, am I doing something wrong here? Or were there any changes that led to such a change in behavior?

+7
c ++ arrays std
source share
1 answer

Expression a = {1}; corresponds to case (10) of initialization of the copy list (see here ). Therefore, it must be correct in accordance with the standard.

The problem you are facing may have something to do with the following change from C ++ 11 to C ++ 14: in C ++ 11, when performing aggregate initialization, brackets around nested initializer lists can only be canceled if syntax ... = {...} but not ...{...} . In C ++ 14, the latter is also allowed. See here for more details. Now suppose C ++ 11 whether a = {1}; to be right or not? I think the answer depends on how you interpret the standard. Clearly, {1} used to initialize the second operand operator= . Below are two possible explanations, which respectively give a positive answer and a negative.

Explanation for a positive answer: the syntax ... = {...} executes copy-list-init, and ...{...} executes direct-list-init. So, what the standard says is that brace alignment is allowed in copy-list-init, but not in direct-list-init. a = {1}; executes copy-list-init. So, the elite is in order.

Explanation for a negative answer: just give a standard literal interpretation. Elision is in order with the appearance of an equal sign, and not otherwise. In a = {1}; initialization of the operand operator= is implicit without an equal sign. So this is not normal. The statement here seems to offer this verbatim explanation.

+1
source share

All Articles