Brace initialization in C++ means initialising an object with curly braces, such as int x{5}; or std::vector<int> v{1, 2, 3};. It was introduced in C++11 as uniform initialization, it rejects narrowing conversions, and when a class has a constructor taking std::initializer_list, braces prefer that constructor. Knowing that preference rule explains most surprises with std::vector.
This guide covers the syntax, narrowing checks, how std::initializer_list is declared in a class, and the overload rules that make std::vector<int>{3, 4} behave differently from std::vector<int>(3, 4).
Braces work for fundamental types, aggregates, containers and classes with constructors.
Empty braces value-initialise the object, so numbers become zero and pointers become null. This also avoids the "most vexing parse", where Widget w(); declares a function instead of an object.
List initialization forbids implicit conversions that may lose information, such as double to int or a large int to a smaller type, unless the value is a constant that fits.
This is a real safety feature. Prefer braces when you want the compiler to catch accidental precision loss.
std::initializer_list<T> (from <initializer_list>) is a lightweight read-only view over a braced list of values. A constructor taking one lets your class accept {...}.
The list's elements are const and the backing array lives only until the end of the full expression that created it. Copy the elements into your own storage, as the constructor does, and never keep the initializer_list itself as a member.
The rule: during list initialization, constructors taking std::initializer_list are considered first. Only if none is viable do the other constructors participate.
The same trap exists in your own classes. If a type has both Widget(int, int) and Widget(std::initializer_list<int>), then Widget w{1, 2} calls the list constructor. To call the other one, use parentheses: Widget w(1, 2).
| Expression | Constructor chosen |
|---|---|
std::vector<int> v(5) | size constructor, five zeros |
std::vector<int> v{5} | initializer_list, one element 5 |
std::vector<int> v(5, 1) | count and value |
std::vector<int> v{5, 1} | initializer_list, two elements |
With auto, behaviour changed in C++17. auto x{5}; deduces int, and auto x{1, 2}; is an error. auto x = {1, 2}; still deduces std::initializer_list<int>.
You can also return a braced list, and the return type decides the constructor:
This works because copy-list-initialization is used, and it fails if the selected constructor is explicit.
They are a good default because of narrowing checks and no vexing parse. Use parentheses when you want a non-list constructor, as with std::vector<int>(n, value).
No. It is a small view with begin(), end() and size(), elements are const, and it does not own the data beyond the expression.
No. The elements are const, so they are always copied. This is why constructing a vector of std::unique_ptr from a braced list fails.
std::vector<int> v{5} have one element?Because the initializer_list constructor wins over the size constructor when braces are used.
Yes. Structs without user-provided constructors can be initialised with {a, b, c}.
Brace initialization gives one syntax for all types, zero-initialises with {} and blocks narrowing. Constructors taking std::initializer_list are preferred whenever a brace list is used, so choose parentheses when you want size-and-value style constructors, and copy the list contents into your own storage.