mercredi 5 décembre 2018

Assigning a initializer_list to std::array

I am trying to assign a initializer_list to std::array like below:

std::array<int,2> arr = {0,1};
arr = {0,1,2};

Immediately I get an error saying that there is no viable overload for operator=

I know that I can't attempt to assign an initializer list whose size is greater than the array's size but I am just trying to understand how std::array does this.

I tried looking through the code at https://code.woboq.org/gcc/libstdc++-v3/include/std/array.html but I don't see an overloaded operator= there. So how does std::array use the default operator= to flag such attempts as a compiler error. How does the default operator= know to check the size of the initializer_list? Please help me understand this.

Aucun commentaire:

Enregistrer un commentaire