Is it legal to declare a constexpr initializer_list object?

Update: The situation got a bit more complicated after the resolution of CWG DR 1684 removed the requirement quoted below. Some more information can be found in this discussion on the std-discussion mailing list and in the related question Why isn’t `std::initializer_list` defined as a literal type? [decl.constexpr]/8: A constexpr specifier for a non-static member … Read more

C++ vector of arrays

Unfortunately, std::array does not have an initializer list constructor. Indeed, it has no user-defined constructor whatsoever — this “feature” is a leftover from C++03 where omitting all user-defined constructors was the only way to enable the C-style brace initialization. It is IMHO a defect in the current standard. So why doesn’t built-in brace initialization work … Read more

Why ={} initialization doesn’t work for tuple?

In addition to Praetorian’s correct answer (which I’ve upvoted), I wanted to add a little more information… Post-C++14, the standard has been changed to allow: tuple<int, int> t2={1, 2}; to compile and have the expected semantics. The proposal that does this is N4387. This will also allow constructs such as: tuple<int, int> foo() { return … Read more

Ternary operator + C++11 constructor from initializer_list [duplicate]

Standard writes in 8.5.4.1: List-initialization Note: List-initialization can be used as the initializer in a variable definition (8.5) as the initializer in a new expression (5.3.4) in a return statement (6.6.3) as a function argument (5.2.2) as a subscript (5.2.1) as an argument to a constructor invocation (8.5, 5.2.3) as an initializer for a non-static … Read more

What Is a Curly-Brace Enclosed List If Not an intializer_list?

It is an braced-init-list. A braced-init-list existed before std::initializer_list and is used to initialize aggregates. int arr[] = {1,2,3,4,5}; The above used a braced-init-list to initialize the array, no std::initializer_list is created. On the other hand when you do std::vector<int> foo = {1,2,3,4,5}; foo is not an aggregate so the braced-init-list is used to create … Read more

tuple vector and initializer_list

The relevant std::tuple constructors are explicit. This means that what you want to do is not possible, since the syntax you want to use is defined in terms of copy initialization (which forbids calling an explicit constructor). In contrast, std::tuple<int, float, char> { 1, 2.2, ‘X’ } uses direct initialization. std::pair does have non-explicit constructors … Read more

Why can’t I initialize a reference in an initializer list with uniform initialization?

Yes, its a bug. This is something new and was voted in the working paper in February 2012 (link). Nicol Bolas makes a good point in that gcc is actually the conforming compiler according to the FDIS approved C++11 standard because the changes to the working paper were made after that.

static_assert on initializer_list::size()

“Initializer lists” are just horrible kludges. Don’t: #include <initializer_list> template<typename T> void Dont(std::initializer_list<T> list) { // Bad! static_assert(list.size() == 3, “Exactly three elements are required.”); } void Test() { Dont({1,2,3}); } Do: template<typename T, std::size_t N> void Do(const T(&list)[N]) { // Good! static_assert(N == 3, “Exactly three elements are required.”); } void Test() { Do({1,2,3}); … Read more