Which greedy initializer-list examples are lurking in the Standard Library?

I assume, with your examples for std::vector<int> and std::string you meant to also cover the other containers, e.g., std::list<int>, std::deque<int>, etc. which have the same problem, obviously, as std::vector<int>. Likewise, the int isn’t the only type as it also applies to char, short, long and their unsigned version (possibly a few other integral types, too). … Read more

C++ Copy constructor gets called instead of initializer_list

As pointed out by Nicol Bolas, the original version of this answer was incorrect: cppreference at the time of writing incorrectly documented the order in which constructors were considered in list-initialization. Below is an answer using the rules as they exist in the n4140 draft of the standard, which is very close to the official … Read more

What would a std::map extended initializer list look like?

It exists and works well: std::map <int, std::string> x { std::make_pair (42, “foo”), std::make_pair (3, “bar”) }; Remember that value type of a map is pair <const key_type, mapped_type>, so you basically need a list of pairs with of the same or convertible types. With unified initialization with std::pair, the code becomes even simpler std::map … Read more

Range-based for with brace-initializer over non-const values?

You are guessing correctly. std::initializer_list elements are always const (which makes sort()ing them impossible, as sort() is a non-const member function) and its elements are always copied (which would make sort()-ing them meaningless even if they weren’t const). From [dcl.init.list], emphasis mine: An object of type std::initializer_list<E> is constructed from an initializer list as if … Read more