can i use braced list to assign std::array ?

i read in c++ primer that :

Unlike built-in arrays, the library array type does allow assignment. The left-and
right-hand operands must have the same type:
1
2
3
4
array<int, 10> a1 = {0,1,2,3,4,5,6,7,8,9};
array<int, 10> a2 = {0}; // elements all have value 0
a1 = a2; // replaces elements in a1
a2 = {0}; // error: cannot assign to an array from a braced list 

Because the size of the right-hand operand might differ from the size of the left-hand
operand, the array type does not support assign and it does not allow assignment
from a braced list of values.


but when i compile it, the code runs perfectly.
is this a compiler extension?
Is this a compiler extension?
No. a2 = {0}; list-initializes the parameter of the operator=.
http://en.cppreference.com/w/cpp/language/list_initialization

Note:
You can ask g++/clang++ to enforce standards-compliance by adding the flags -std=c++14 -Wall -Wextra -pedantic-errors to the command line. I'm not sure what the equivalent flags are for Microsoft's compiler.
Last edited on
thanks for replying ,
but why did he say that it won't compile ?
he wrote that we can't use braced list to assign std::array many times !
but why did he say that it won't compile?

I don't know. The uniform initialization syntax was new in C++11. Perhaps that idea was based off a paper which was later revised before the standard was published.
Topic archived. No new replies allowed.