initializer_list immutable nature leads to excessive copying
c++, c++11, c++14, initializer-list, move-semantics
Solution
There is a recent proposal for movable initializer lists, where, in particular, the authors say:
`std::initializer_list` was designed around 2005 (N1890) to 2007 (N2215), before move semantics matured, around 2009. At the time, it was not anticipated that copy semantics would be insufficient or even suboptimal for common value-like classes. There was a 2008 proposal N2801 Initializer lists and move semantics but C++0x was already felt to be slipping at that time, and by 2011 the case had gone cold.
Problem
Why does the access to `std::initializer_list` not allow us to change its content? It's a big disadvantage of `std::initializer_list` when using it for its main purpose (to initialize a container), since it's use leads to excessive copy-construction/copy-assignment, instead of move-construction/move-assignment. ``` #include <initializer_list> #include <iostream> #include <vector> #include <cstdlib> struct A { A() = default; A(A const &) { std::cout << __PRETTY_FUNCTION__ << std::endl; } A(A &&) { std::cout << __PRETTY_FUNCTION__ << std::endl; } A & operator = (A const &) { std::cout << __PRETTY_FUNCTION__ << std::endl; return *this; } A & operator = (A &&) { std::cout << __PRETTY_FUNCTION__ << std::endl; return *this; } }; int main() { std::vector< A >{A{}, A{}, A{}}; return EXIT_SUCCESS; } ``` Output (as expected): ``` A::A(const A &) A::A(const A &) A::A(const A &) ``` Why is its design so constrained?