C++20 designated initializers

The C++20 standard provides new ways to initialize aggregates. These are:

  • list initialization with designated initializers, that has the following forms:
    T object = { .designator = arg1 , .designator { arg2 } ... };
    T object { .designator = arg1 , .designator { arg2 } ... };
  • direct initialization, that has the following form:
    T object (arg1, arg2, ...);

In this article, we will see how list initialization with designated initializers work.

C++17 New Rules For auto Deduction From braced-init-list

Initialization of variables in C++ can have several forms: default initialization: std::string s; value initialization: std::string s{}; direct initialization: std::string s(“demo”); copy initialization: std::string s = “demo”; list initialization: std::string s{‘d’, ‘e’, ‘m’, ‘o’}; aggregate initialization: char s[5] = {‘d’, ‘e’, ‘m’, ‘o’}; reference initialization: char& c = s[0]; C++11 introduced a generalized syntax for…