A default member initializer in C++ is a value given to a non-static data member where it is declared, such as int count = 0; or std::string name{"unknown"};. An aggregate is a simple type, like a plain struct, that can be initialised member by member with braces. Together they let you write small data types with no constructors at all.
This guide explains both features, the exact rules for what counts as an aggregate in C++20 and later, and the C++20 designated initializers that make aggregate initialization readable. The keyword for this guide is aggregate initialization in C++.
Since C++11 you can put an initializer next to a member. Constructors that do not set that member use it.
Defaults declared once at the member stay correct as you add constructors. If a constructor's member initializer list names the member, that value wins over the default.
An aggregate is an array, or a class type that (in C++20 and later) has:
Default member initializers are allowed (since C++14). Aggregates are initialised with a brace list, and members are taken in declaration order.
Members without an initializer in the list are initialised from their default member initializer, or value-initialised (zero for numbers) if there is none. Too many initializers, or narrowing, is an error.
C++20 adds designated initializers for aggregates, so you name the members. The order must match the declaration order.
Here verbose keeps its default, retries is 5 and timeoutMs is 250. This pattern replaces long argument lists and "options" constructors, and adding a field with a default does not break existing callers.
Before C++20, Point p(1.0, 2.0) failed for aggregates because there was no constructor. C++20 allows parentheses for aggregate initialization, which makes std::make_unique<Point>(1.0, 2.0) and emplace_back(1.0, 2.0) work. Braces remain the better habit, because narrowing is allowed in the parenthesized form.
Adding any user-declared constructor, a private data member or a virtual function turns the type into a non-aggregate, so brace lists then go to its constructors instead.
You can check with std::is_aggregate_v<T> from <type_traits> (C++17).
| Feature | Aggregate | Class with constructors |
|---|---|---|
| Brace list sets members directly | yes | no, calls constructor |
| Designated initializers (C++20) | yes | no |
| Can enforce invariants | no | yes |
| Private data allowed | no | yes |
Use aggregates for plain data bundles such as options and results. Use a class with constructors and private members when the type must protect an invariant.
Yes. Non-virtual member functions, static members and default member initializers do not stop a type from being an aggregate.
They use their default member initializer if they have one. Otherwise they are value-initialised, which means zero for built-in types.
Yes, for every member the constructor's initializer list does not mention.
No. They are standard from C++20, although some compilers accepted them earlier as an extension.
Use std::is_aggregate_v<T>, available since C++17.
Default member initializers keep sensible defaults next to the member declaration, and aggregates let plain structs be built from brace lists with no constructors. C++20 adds designated initializers and parenthesized aggregate initialization. Choose an aggregate for simple data and a constructor-based class when an invariant must be protected.