Why does ‘std::vector b{2};’ create a 1-element vector, and not a 2-element one?

Yes, this behaviour is intended, according to §13.3.1.7 Initialization by list-initialization 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 … Read more

Are multiple mutations within initializer lists undefined behavior?

Yes, the code is valid and does not have undefined behavior. Expressions in an initizalizer list are evaluated left-to-right and sequenced before the evaluation of the constructor of S. Therefore, your program should consistently assign value 2 to variable i. Quoting § 8.5.4 of the C++ Standard: “Within the initializer-list of a braced-init-list, the initializer-clauses, … Read more

Brace-enclosed initializer list constructor

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

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