Designated Initializers in C++ 20
Last Updated :
14 May, 2023
With C++20, we get a convenient way of initializing data members. The new feature is called Designated Initializers and it might be familiar to C programmers. In other words, Designated Initializers are a new feature that has been introduced in C++20. It allows developers or programmers to initiate data members of a struct or an array in any order they want, providing a more readable and flexible way of initializing data members.
In C++20, a Collection type is defined as an array type or a class type that meets certain criteria. A Collection type shouldn't have any private or protected direct non-static data members, user-declared or inherited constructors, virtual, private, or protected base classed or virtual member functions. This definition helps to identify and distinguish Collection types from other types in C++.
Syntax:
struct_type obj_name = {
.member1 = value1, .member2 = value2, member3 = value3, ...... .memberN = valueN
};
Where,
- struct_type: The name of the struct type.
- obj_name: The name of the object being initialized.
- member1 to memberN: The names of the data members being initialized.
- value1 to valueN: The values being assigned to the corresponding data members.
- We use the dot operator (.) followed by the member name to specify the member we want to initialize.
Why to use Designated Initializers?
- Designated Initializers increase readability.
- It is a convenient way of initializing data members.
- Designated Initializers help to identify and distinguish Collection types from other types in C++.
Example:
C++
// C++ Program to initialize Structure Date with dt
// Designated Initializer
#include <iostream>
using namespace std;
// Create Structure
struct Date {
int year;
int month;
int day;
};
int main()
{
Date dt{ .year = 2023, .month = 4, .day = 24 };
cout << "Year : " << dt.year << "\n";
cout << "Month : " << dt.month << "\n";
cout << "Day : " << dt.day;
return 0;
}
OutputYear : 2023
Month : 4
Day : 24
Advantages of Designated Initializers
Designated Initializers in C++20 offer several benefits:
- Readability and Maintainability: By using designated initializers, it's easy to understand which member is being initialized with what value, even if the struct has many members.
- Partial or Subset Initialization: It allows us to initialize only a subset of members in the struct. This can be useful when we don't want to initialize all members of the strict or when we only need to initialize a few members.
- Flexible Initialization: It allows us to initialize the members of a struct or array in any order we want. This can be useful when we want to initialize the members in a specific order or when we want to group related members together.
- Nested Structs and Arrays: Designated Initializers can also be used to initialize nested structs or Arrays, making the initialization of complex data structures more straightforward and easier to read. It can also reduce the amount of boilerplate code required for initialization, as we can directly specify the values for each member instead of creating temporary variables or using multiple assignment statements.
Example 1:
C++
// C++ Program to initialize a struct from a list of values
#include <iostream>
using namespace std;
struct Date {
int month;
int year;
};
int main()
{
// dt.month = 4, dt.year = 2023
Date dt{ 4, 2023 };
cout << "Month : " << dt.month << "\n";
cout << "Year : " << dt.year << "\n";
return 0;
}
OutputMonth : 4
Year : 2023
Now, imagine if we were to update the struct definition by adding a new member, which is not the last member.
Example 2:
C++
#include <iostream>
using namespace std;
struct Date {
int day;
int month;
int year;
};
int main()
{
// dt.day = 4, dt.month = 2023, dt.year = 0
Date dt{ 4, 2023 };
cout << "Month : " << dt.month << "\n";
cout << "Year : " << dt.year << "\n";
cout << "Day : " << dt.day;
return 0;
}
OutputMonth : 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.
Initializing with Designated Initializers
Example:
C++
#include <iostream>
using namespace std;
struct Date {
int day;
int month;
int year;
};
int main()
{
Date dt{ .day = 24, .month = 4, .year = 2023 }; // dt.day = 24, dt.month = 4, dt.year = 2023
cout << "Month : " << dt.month << "\n";
cout << "Year : " << dt.year << "\n";
cout << "Day : " << dt.day;
return 0;
}
OutputMonth : 4
Year : 2023
Day : 24
Example:
C++
// C++ program to initialize a subset of members of a struct
// using designated initializers
#include <iostream>
using namespace std;
struct Rectangle {
int length;
int width;
int height;
};
int main()
{
Rectangle rect = { .length = 10, .width = 5 };
cout << " Length : " << rect.length << "\n";
cout << " Width : " << rect.width << "\n";
cout << " Height : " << rect.height;
return 0;
}
Output Length : 10
Width : 5
Height : 0
Similar Reads
Aggregate Initialization in C++ 20
C++20 has undergone several upgrades that aim to streamline and enhance the code. Among these improvements lies a remarkable modification that simplifies the initialization process of aggregates, such as arrays, structs, and classes lacking user-declared constructors. In C++20, you can use aggregate
3 min read
std::initializer_list in C++ 11
The std::initializer_list class template was added in C++ 11 and contains many built-in functions to perform various operations with the initializer list. It provides member functions like a size(), begin(), end(), and constructor to construct, iterate, and access elements of the initializer list. T
6 min read
Different Ways to Initialize a Variable in C++
Variables are arbitrary names given to the memory location in the system. These memory locations are addressed in the memory. In simple terms, the user-provided names for memory locations are called variables. Additionally, a data type is used to declare and initialize a variable. Suppose we want to
4 min read
constinit Specifier in C++ 20
The constinit specifier is a new feature that is introduced in C++ 20 that allows us to declare a variable with static storage duration. In this article we will discuss the constinit specifier, its usage, advantages, and limitations. What is constinit Specifier? The constinit specifier is used to ma
3 min read
When do we use Initializer List in C++?
Initializer List is used in initializing the data members of a class. The list of members to be initialized is indicated with constructor as a comma-separated list followed by a colon. Following is an example that uses the initializer list to initialize x and y of Point class.ExampleC++#include <
7 min read
Order of execution in initializer list in C++
Prerequisite: Classes, Constructors, Initializer list In this article, we will discuss the order of execution in the initializer list in C++. Generally, the order of execution is from top to bottom and left to right. But a rare condition arises where this rule fails is when the initializer list is u
2 min read
Different Ways to Initialize an unordered_set in C++
An unordered_set is an associated container available in the C++ Standard Template Library(STL) that is used for unique elements without any specific ordering, it internally uses the working principle of a hashtable to store elements. Different ways to Initialize an unordered_set in C++ Initializati
6 min read
Builder Pattern | C++ Design Patterns
The builder pattern is defined as a creational design pattern that separates the construction of a complex object from its representation, allowing us to create different representations of an object using the same construction process. It's beneficial when an object has many optional properties or
6 min read
Abbreviated Function Templates in C++ 20
Abbreviated Function Template is a new feature introduced in C++20 to simplify the syntax of function template definition. Traditionally, we use function templates to define a function that can take parameters of different types. But sometimes, it might get complicated and hard to understand. In the
3 min read
Inline Variables in C++ 17
An inline variable in C++ is a variable that is declared using an inline specifier. It is an exception to one definition having multiple definitions across various translation units. Inline variables have an external linkage when not declared as static. Syntaxinline data_type variable_name = initial
3 min read