![]() |
VOOZH | about |
C++20 introduces Designated Initializers, which let you initialize struct or array members by name instead of position. This improves clarity and allows values to be set in any order. The feature works with specific aggregate (collection) types that follow simple structural rules.
Year : 2023 Month : 4 Day : 24
struct_type obj_name = {
.member1 = value1, .member2 = value2, member3 = value3, ...... .memberN = valueN
};
Where,
Month : 4 Year : 2023
Now, imagine if we were to update the struct definition by adding a new member, which is not the last member.
Month : 2023 Year : 0 Day : 4
However, with Designated Initializers, we can simply add the new member and update its value without affecting initialization code. This can save time and effort while also reducing the chances of introducing bugs due to human error.
Month : 4 Year : 2023 Day : 24