1. Problem It Solves
Designated initializers make aggregate construction readable by naming the members that receive values. It makes an important assumption visible and checkable.
2. Prerequisites
Structs, member declaration order, and list initialization.
You should be able to compile a short program and read its output.
3. Core Idea
An aggregate is a simple open record. A designator is a label placed on an initializer so readers need not remember only positional meaning. Read .member = value as a precise promise; runtime preconditions still belong to the programmer.
4. Minimal Syntax
Settings s{.width = 800, .height = 600, .fullscreen = false};5. How It Works
The program introduces the smallest relevant form of
.member = value.It applies the feature to fixed data while required owners remain in scope.
It prints one result that can be checked against the source.
6. Common Mistakes
C++ designators must follow declaration order and cannot freely skip backward as in some other languages; the type must remain an aggregate.
Also check the required header, C++20 library support, lifetime, and deduced types.
7. When to Use It
Use it when a simple data record has several fields and named initialization improves clarity.
Avoid it when the type protects invariants through constructors or is no longer an aggregate.
8. Simple Example
A settings aggregate receives width, height, and fullscreen values with member names visible at the call site. The companion .cpp uses no input, so its result is easy to reproduce.
Complete sample code
Source file
cpp20/22_designated_initializers_aggregates/main.cpp
// Day 22: Designated Initializers and Aggregate Initialization
#include <iostream>
struct Settings {
int width{};
int height{};
bool fullscreen{};
};
int main() {
Settings settings{
.width = 800,
.height = 600,
.fullscreen = false
};
std::cout << settings.width << 'x' << settings.height << '\n';
std::cout << std::boolalpha << settings.fullscreen << '\n';
}
9. Key Takeaways
.member = valueexpresses the central C++20 idea of this day.The example isolates one behavior with fixed data.
Compiler checks do not replace lifetime and runtime reasoning.
Prefer the smallest interface that states the real requirement.
10. Self-Check Questions
Easy — What is the main job of
.member = valuein the minimal example?Medium — What values do omitted aggregate members receive when value initialization applies?
Hard — Why can adding a user-declared constructor make existing designated initialization fail even if the same member names remain?