Is it possible to invoke a user-defined conversion function via list-initialization?

The original intent of 13.3.3.1p4 is to describe how to apply the requirement in 12.3p4 that: 4 – At most one user-defined conversion (constructor or conversion function) is implicitly applied to a single value. Before defect 84, 13.3.3.1p4 was almost purely informative: 4 – In the context of an initialization by user-defined conversion (i.e., when … Read more

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

Why do auto and template type deduction differ for braced initializers?

There are two important reasons for templates not to do any deduction (the two that I remember in a discussion with the guy in charge) Concerns about future language extensions (there are multiple meanings you could invent – what about if we wanted to introduce perfect forwarding for braced init list function arguments?) The braces … Read more

Why does the standard differentiate between direct-list-initialization and copy-list-initialization?

Because they don’t do the exact same thing. As stated in 13.3.1.7 [over.match.list]: In copy-list-initialization, if an explicit constructor is chosen, the initialization is ill-formed. In short, you can only use implicit conversion in copy-list-initialization contexts. This was explicitly added to make uniform initialization not, um, uniform. Yeah, I know how stupid that sounds, but … Read more

Why can I not brace initialize a struct derived from another struct?

Answer for C++ standard versions before C++17: Your problem has to do with aggregate initialization: struct X is an aggregate while struct Y is not. Here is the standard quote about aggregates (8.5.1): An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no brace-or-equal-initializers for non-static data members (9.2), … Read more

What are the advantages of list initialization (using curly braces)?

Basically copying and pasting from Bjarne Stroustrup’s “The C++ Programming Language 4th Edition”: List initialization does not allow narrowing (§iso.8.5.4). That is: An integer cannot be converted to another integer that cannot hold its value. For example, char to int is allowed, but not int to char. A floating-point value cannot be converted to another … Read more

Why is list initialization (using curly braces) better than the alternatives?

Basically copying and pasting from Bjarne Stroustrup’s “The C++ Programming Language 4th Edition”: List initialization does not allow narrowing (§iso.8.5.4). That is: An integer cannot be converted to another integer that cannot hold its value. For example, char to int is allowed, but not int to char. A floating-point value cannot be converted to another … Read more