How can I make a constructor which lets me construct with a braced-init-list?

It can only be done for aggregates (arrays and certain classes. Contrary to popular belief, this works for many nonpods too). Writing a constructor that takes them is not possible. Since you tagged it as “C++0x”, then this is possible though. The magic words is “initializer-list constructor”. This goes like Phenotype(std::initializer_list<uint8> c) { assert(c.size() <= … Read more

Strange values in a lambda returning initializer_list

From: http://en.cppreference.com/w/cpp/utility/initializer_list The underlying array is not guaranteed to exist after the lifetime of the original initializer list object has ended. The storage for std::initializer_list is unspecified (i.e. it could be automatic, temporary, or static read-only memory, depending on the situation). I don’t think the initializer list is copy-constructable. std::set and other containers are. Basically … Read more

lifetime of a std::initializer_list return value

std::initializer_list is not a container, don’t use it to pass values around and expect them to persist DR 1290 changed the wording, you should also be aware of 1565 and 1599 which aren’t ready yet. Then the return value’s array should also survive into the calling function, and it should be possible to preserve it … Read more

Why is the std::initializer_list constructor preferred when using a braced initializer list?

§13.3.1.7 [over.match.list]/p1: When objects of non-aggregate class type T are list-initialized (8.5.4), overload resolution selects the constructor in two phases: Initially, the candidate functions are the initializer-list constructors (8.5.4) of the class T and the argument list consists of the initializer list as a single argument. If no viable initializer-list constructor is found, overload resolution … Read more

Initialization difference with or without curly braces in C++

Short version Initialization via {..} is list-initialization, which prohibits narrowing conversions. For example, if LLONG_MAX is the maximum value of an long long int, and your int cannot represent that: int x = LLONG_MAX; // probably accepted with a warning int x {LLONG_MAX}; // error Similarly: long long y = /*something*/; int x = y; … Read more

Why use variadic arguments now when initializer lists are available?

If by variadic arguments you mean the ellipses (as in void foo(…)), then those are made more or less obsolete by variadic templates rather than by initializer lists – there still could be some use cases for the ellipses when working with SFINAE to implement (for instance) type traits, or for C compatibility, but I … Read more

Why could const member be initialized twice?

It’s not initialized twice; the default member initializer is just ignored. So for A a(555);, a.k is initialized as 555. If a member has a default member initializer and also appears in the member initialization list in a constructor, the default member initializer is ignored. From the standard, [class.base.init]/10: If a given non-static data member … Read more

Calling initializer_list constructor via make_unique/make_shared

std::make_unique is a function template which deduces the argument types which are passed to the object constructor. Unfortunately, braced lists are not deducible (with an exception for auto declarations), and so you cannot instantiate the function template when that missing parameter type. You can either not use std::make_unique, but please don’t go that route – … Read more

How to construct std::array object with initializer list? [duplicate]

An std::array<> has no constructor that takes an std::initializer_list<> (initializer list constructor) and there is no special language support for what it may mean to pass a std::initializer_list<> to a class’ constructors such that that may work. So that fails. For it to work, your derived class needs to catch all elements and then forward … Read more