Is the following code always valid or is it compiler/platform-dependent? Obviously I could have initialized edges
using the value constructor, but I am curious to see if the copy assignment operator=
works here when edges
is initialized to size 0, and then set equal to a braced r-value.
It works on my macbook.
std::vector<std::vector<int>> edges;
edges = {{1,2,3},{4},{5,6}};
It's valid (since C++11). std::vector
has an overloaded operator=
taking std::initializer_list
.
Replaces the contents with those identified by initializer list
ilist
.
And std::initializer_list
could be constructed from braced-list in specified contexts.
(emphasis mine)
A
std::initializer_list
object is automatically constructed when:
- a braced-init-list is used to list-initialize an object, where the corresponding constructor accepts an
std::initializer_list
parameter- a braced-init-list is used as the right operand of assignment or as a function call argument, and the corresponding assignment operator/function accepts an
std::initializer_list
parameter- a braced-init-list is bound to auto, including in a ranged for loop