Designated Initializers

Contents[Show]

Designated initialization is an extension of aggregate initialization and empowers you to directly initialize the members of a class type using their names.

 TimelineCpp20

Designated initialization is a special case of aggregate initialization. Writing about designated initialization means, therefore, to write about aggregate initialization.

Aggregate Initialization

First: what is an aggregate. Aggregates are arrays and class types. A class type is a class, a struct, or a union.

With C++20, the following condition must hold class types:

  • no private or protected non-static data members
  • no user-declared or inherited constructors
  • no virtual, private, or protected base classes
  • no virtual member functions

The next program exemplifies aggregate initialization.

 

// aggregateInitialization.cpp

#include <iostream>

struct Point2D{
    int x; 
    int y;
};

class Point3D{
public:
    int x;
    int y;
    int z;
};

int main(){
    
    std::cout << std::endl;
    
    Point2D point2D{1, 2};        // (1)
    Point3D point3D{1, 2, 3};     // (2)

    std::cout << "point2D: " << point2D.x << " " << point2D.y << std::endl;
    std::cout << "point3D: " << point3D.x << " " << point3D.y << " " << point3D.z << std::endl;
    
    std::cout << std::endl;

}

 

(1) and (2) directly initializes the aggregates using curly braces. The sequence of the initializers in the curly braces have to match the declaration order of the members.

 aggregateInitialization

Based on aggregate initialization in C++11, we get designed initializers in C++20. So far, only the Microsoft compiler support designated initializers completely.

Designated Initializers

Designated initializers enable it to directly initialize members of a class type using their name. For a union, only one initializer can be provided. As for aggregate initialization, the sequence of initializers in the curly braces have to match the declaration order of the members.

 

// designatedInitializer.cpp

#include <iostream>

struct Point2D{
    int x;
    int y;
};

class Point3D{
public:
    int x;
    int y;
    int z;
};

int main(){
    
    std::cout << std::endl;
    
    Point2D point2D{.x = 1, .y = 2};          // (1)
    Point3D point3D{.x = 1, .y = 2, .z = 3};  // (2)

    std::cout << "point2D: " << point2D.x << " " << point2D.y << std::endl;
    std::cout << "point3D: " << point3D.x << " " << point3D.y << " " << point3D.z << std::endl;
    
    std::cout << std::endl;

}

(1) and (2) use designated initializers to initialize the aggregates. The initializers such as .x or .y are often called designators.

 

designatedInitializer

The members of the aggregate can already have a default value. This default value is used when the initializer is missing. This does not hold for a union.

 

// designatedInitializersDefaults.cpp

#include <iostream>

class Point3D{
public:
    int x;
    int y = 1; 
    int z = 2;
};

void needPoint(Point3D p) {
     std::cout << "p: " << p.x << " " << p.y << " " << p.z << std::endl;
}

int main(){
    
    std::cout << std::endl;
    
    Point3D point1{.x = 0, .y = 1, .z = 2};     // (1)
    std::cout << "point1: " << point1.x << " " << point1.y << " " << point1.z << std::endl;
    
    Point3D point2;                             // (2)
    std::cout << "point2: " << point2.x << " " << point2.y << " " << point2.z << std::endl;
    
    Point3D point3{.x = 0, .z = 20};            // (3)
    std::cout << "point3: " << point3.x << " " << point3.y << " " << point3.z << std::endl;
    
    // Point3D point4{.z = 20, .y = 1}; ERROR   // (4) 
    
    needPoint({.x = 0});                        // (5)
    
    std::cout << std::endl;

}

 

(1) initializes all members,  but (2) does not provide a value for the member x. Consequently, x is  not initialized. It is fine if you only initialize the members who don't have a default value such as in (3) or (5). The expression (4) would not compile because z and y are in the wrong order.

designatedInitializerDefaults

Designated initializers detect narrowing conversion. Narrowing conversion is a conversion of a value including the loss of its precision.

// designatedInitializerNarrowingConversion.cpp

#include <iostream>

struct Point2D{
    int x;
    int y;
};

class Point3D{
public:
    int x;
    int y;
    int z;
};

int main(){
    
    std::cout << std::endl;
    
    Point2D point2D{.x = 1, .y = 2.5};            // (1)
    Point3D point3D{.x = 1, .y = 2, .z = 3.5f};   // (2)

    std::cout << "point2D: " << point2D.x << " " << point2D.y << std::endl;
    std::cout << "point3D: " << point3D.x << " " << point3D.y << " " << point3D.z << std::endl;
    
    std::cout << std::endl;

}

 

(1) and (2) produce a compile-time error because the initialization .y = 2.5 and .z = 3.5f would cause a narrowing conversion to in.

designatedInitializerNarrowingConversion

Interestingly, designated initializers in C behave differently to designated initializers in C++.

Differences between C and C++

C supports use-cases that are not supported in C++. C allows

  • to initialize the members of the aggregate out-of-order
  • to initialize the members of a nested aggregate
  • to mix designated initializers and regular initializers
  • designated initialization of arrays

The proposal P0329R4 provides self-explanatory examples for these use-cases:

 

struct A { int x, y; };
struct B { struct A a; };
struct A a = {.y = 1, .x = 2}; // valid C, invalid C++ (out of order)
int arr[3] = {[1] = 5};        // valid C, invalid C++ (array)
struct B b = {.a.x = 0};       // valid C, invalid C++ (nested)
struct A a = {.x = 1, 2};      // valid C, invalid C++ (mixed)

 

The rational for this difference between C and C++ is also part of the proposal: "In C++, members are destroyed in reverse construction order and the elements of an initializer list are evaluated in lexical order, so field initializers must be specified in order. Array designators conflict with ​lambda-expression​ syntax. Nested designators are seldom used." The paper continues to argue, that only out-of-order initialization of aggregate is commonly used.

What's next?

Wow! With C++98 we got const, with C++11 constexpr, and with C++20 consteval and constinit.  In my next post, I write about the new C++20 specifiers consteval and constinit and about their differences to const and constexpr

 

Thanks a lot to my Patreon Supporters: Meeting C++, Matt Braun, Roman Postanciuc, Venkata Ramesh Gudpati, Tobias Zindl, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Darshan Mody, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Wolfgang Gärtner, Jon Hess, Christian Wittenhorst, Louis St-Amour, Stephan Roslen, Venkat Nandam, Jose Francisco, Douglas Tinkham, Lakshman, Kuchlong Kuchlong, Avi Kohn, and Serhy Pyton. 

 

Thanks in particular to: Bitwyre Technologies

 

Thanks in particular to:   crp4

 

Seminars

I'm happy to give online-seminars or face-to-face seminars world-wide. Please call me if you have any questions.

Bookable Seminars (Online)

Standard Seminars 

Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.

Contact Me

Modernes C++,

RainerGrimmSmall

 

Comments   

0 #1 Jonathan OConnor 2020-07-09 08:49
Rainer, what happens if Point2 has a constructor, default or non-copy, non-move? Does that disable the use of designated initializers, or can you always use designated initializers, so long as the members are visible at the point of instantiation?
Quote
0 #2 Rainer Grimm 2020-07-10 20:01
Quoting Jonathan OConnor:
Rainer, what happens if Point2 has a constructor, default or non-copy, non-move? Does that disable the use of designated initializers, or can you always use designated initializers, so long as the members are visible at the point of instantiation?

Hello Jonathan,
what a Aggregate is depends on the C++ version (https://en.cppreference.com/w/cpp/language/aggregate_initialization). With C++20 the requirements are quite weak regarding your question: no user-declared or inherited constructors
Quote
0 #3 Jonathan OConnor 2020-07-15 13:36
Rainer, I tried it out on Godbolt, and answered my own question (which was probably not phrased very clearly!). If a struct has any constructors, then you can't initialize an instance using designated initializers.

Example here: https://godbolt.org/z/K9doxx
Quote

My Newest E-Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code

Visitors

Today 6667

Yesterday 10139

Week 37708

Month 6667

All 4627561

Currently are 202 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments