Inconsistent gcc diagnostic for string initialization

While: char a[5] = {‘h’,’e’,’l’,’l’,’o’,’\0′}; is invalid. (C11, 6.7.9p2) “No initializer shall attempt to provide a value for an object not contained within the entity being initialized.” This: char b[5] = “hello”; is explicitly allowed by C (emphasis mine): (C11, 6.7.9p14) “An array of character type may be initialized by a character string literal or … 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

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

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

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

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

Initializer-list-constructing a vector of noncopyable (but movable) objects

Maybe this clause from 8.5.4.5 explains it (my emphasis): An object of type std::initializer_list is constructed from an initializer list as if the implementation allocated an array of N elements of type E, where N is the number of elements in the initializer list. Each element of that array is copy-initialized with the corresponding element … 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

Are multiple mutations of the same variable within initializer lists undefined behavior pre C++11

The code is not undefined pre-C++11 but the evaluation order is unspecified. If we look at the draft standard section 1.9 Program execution paragraph 12 says: A full-expression is an expression that is not a subexpression of another expression. […] and paragraph 15 says: There is a sequence point at the completion of evaluation of … Read more