C++ Reference ยท Types and Language Concepts
Brace (list) initialization
Reference for C++ brace initialization: list-init rules, narrowing conversions, std::initializer_list constructor priority, empty braces and auto deduction.
Brace initialization (also called list initialization) writes the initial value or values between { and }: int x{5};, std::vector<int> v{1, 2, 3};, Point p{3, 4};. It works for built-in types, aggregates, containers and classes, which is why it is sometimes called "uniform" initialization.
Two features set it apart from parentheses. First, it forbids narrowing conversions, so int x{3.5}; is an error instead of silently truncating. Second, when a class has a constructor taking std::initializer_list, braces prefer it over every other constructor. That rule explains the classic surprise std::vector<int> v{3, 9} (two elements) versus v(3, 9) (three nines).
std::vector<int> a{1, 2, 3}; // three elements int n{}; // 0 // int bad{3.5}; // error: narrowing
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
T x{a, b, c}; // direct-list-initialization
T x = {a, b, c}; // copy-list-initialization (explicit constructors rejected)
T{a, b} // temporary
new T{a, b} // dynamic
return {a, b}; // return value
f({a, b}); // argument
T::T() : m{a, b} {} // member initializer
T(std::initializer_list<U>); // constructor that braces prefer
| Part | Description |
|---|---|
{} empty |
With a default constructor: value initialization. Without one but with an aggregate: members value-initialized. |
| elements | Each is converted to the target type; conversions that lose information are narrowing and ill-formed. |
std::initializer_list<U> |
A lightweight read-only view of a temporary array. Header <initializer_list> (included by most containers). |
Narrowing is: floating to integer; long double to double/float (or double to float) when the value is not a constant that fits exactly; integer to a narrower integer or to floating point when the value cannot be represented (constants that fit are allowed); and integer to a type that cannot hold every value of the source.
Errors / undefined behavior: a narrowing conversion is a compile error in conforming code (GCC reports a warning for some constant cases; heed it). An initializer_list stores a temporary array, so keeping it beyond the full expression leaves it dangling.
Usage notes
- Overload resolution in two phases. The compiler first looks only at
initializer_listconstructors using the braced elements; if one is viable, it wins even if another constructor is a better match. Only then does it consider all constructors. - Empty braces
{}call the default constructor, never aninitializer_listconstructor with zero elements. autoand braces.auto x = {1,2};isstd::initializer_list<int>.auto x{1};isint(C++17 rule). Several elements inauto x{1, 2};is an error.- Use parentheses when you mean a count or size (
std::vector<int> v(10)), and braces when you mean the contents. - Safer by default. Braces reject narrowing, so
int total{a * 1.5}flags a truncation. They also avoid the most vexing parse, becauseT x{};can only be an object. - Aggregates and designated initializers use the same brace syntax; C++20 adds the
.name = valueform (Designated initializers). - Templates.
T x{}gives a zero value for anyT; this is the idiom for "default value". initializer_listelements areconst. You cannot move out of them, so inserting a list ofstd::stringcopies each string. For move-only types, build the container withemplace_back.- Return by braces.
return {1, 2};constructs the return type; it skipsexplicitconstructors.