Why does initialization of array of pairs still need double braces in C++14?

This appears to be a parsing ambuguity somewhat similar to the famous most vexing parse. I suspect what’s going on is that: If you write std::array<std::pair<int, int>, 3> b {{1, 11}, {2, 22}, {3, 33}}; the compiler has two ways to interpret the syntax: You perform a full-brace initialization (meaning the outermost brace refers to … Read more

C++11 initializer list fails – but only on lists of length 2

Introduction Imagine the following declaration, and usage: struct A { A (std::initializer_list<std::string>); }; A {{“a” }}; // (A), initialization of 1 string A {{“a”, “b” }}; // (B), initialization of 1 string << !! A {{“a”, “b”, “c”}}; // (C), initialization of 3 strings In (A) and (C), each c-style string is causing the initialization … Read more

Initializing fields in constructor – initializer list vs constructor body [duplicate]

They are not the same if member1 and member2 are non-POD (i.e. non-Plain Old Data) types: public : Thing(int _foo, int _bar){ member1 = _foo; member2 = _bar; } is equivalent to public : Thing(int _foo, int _bar) : member1(), member2(){ member1 = _foo; member2 = _bar; } because they will be initialized before the … 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

Why isn’t std::initializer_list a language built-in?

There were already examples of “core” language features that returned types defined in the std namespace. typeid returns std::type_info and (stretching a point perhaps) sizeof returns std::size_t. In the former case, you already need to include a standard header in order to use this so-called “core language” feature. Now, for initializer lists it happens that … Read more

When to use the brace-enclosed initializer?

I think the following could be a good guideline: If the (single) value you are initializing with is intended to be the exact value of the object, use copy (=) initialization (because then in case of error, you’ll never accidentally invoke an explicit constructor, which generally interprets the provided value differently). In places where copy … Read more

Can I list-initialize a vector of move-only type?

Edit: Since @Johannes doesn’t seem to want to post the best solution as an answer, I’ll just do it. #include <iterator> #include <vector> #include <memory> int main(){ using move_only = std::unique_ptr<int>; move_only init[] = { move_only(), move_only(), move_only() }; std::vector<move_only> v{std::make_move_iterator(std::begin(init)), std::make_move_iterator(std::end(init))}; } The iterators returned by std::make_move_iterator will move the pointed-to element when being … Read more

Initializing a member array in constructor initializer

How can I do what I want to do (that is, initialize an array in a constructor (not assigning elements in the body)). Is it even possible? Yes. It’s using a struct that contains an array. You say you already know about that, but then I don’t understand the question. That way, you do initialize … Read more

C compile error: “Variable-sized object may not be initialized”

I am assuming that you are using a C99 compiler (with support for dynamically sized arrays). The problem in your code is that at the time when the compilers sees your variable declaration it cannot know how many elements there are in the array (I am also assuming here, from the compiler error that length … Read more