Skip to content
Tutorials Center
My learning

Topics

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).

C++
std::vector<int> a{1, 2, 3};   // three elements
int n{};                       // 0
// int bad{3.5};               // error: narrowing

Try it

C++ Braces versus parentheses

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.

C++ Narrowing, auto and custom initializer_list

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

C++
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_list constructors 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 an initializer_list constructor with zero elements.
  • auto and braces. auto x = {1,2}; is std::initializer_list<int>. auto x{1}; is int (C++17 rule). Several elements in auto 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, because T x{}; can only be an object.
  • Aggregates and designated initializers use the same brace syntax; C++20 adds the .name = value form (Designated initializers).
  • Templates. T x{} gives a zero value for any T; this is the idiom for "default value".
  • initializer_list elements are const. You cannot move out of them, so inserting a list of std::string copies each string. For move-only types, build the container with emplace_back.
  • Return by braces. return {1, 2}; constructs the return type; it skips explicit constructors.