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, writing 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 initialize the aggregates using curly braces. The sequence of the initializers in the curly braces has 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.

 

Rainer D 6 P2 540x540Modernes C++ Mentoring

Be part of my mentoring programs:

 

 

 

 

Do you want to stay informed about my mentoring programs: Subscribe via E-Mail.

Designated Initializers

Designated initializers enable it to initialize members of a class type using their name directly. For a union, only one initializer can be provided. As for aggregate initialization, the sequence of initializers in the curly braces has 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 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

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 rationale 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 the order. Array designators conflict with ​lambda-expression​ syntax. Nested designators are seldom used." The paper argues 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 will write about the new C++20 specifiers consteval and constinit and their differences from const and constexpr

 

Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, and Rob North.

 

Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, and Slavko Radman.

 

 

My special thanks to Embarcadero CBUIDER STUDIO FINAL ICONS 1024 Small

 

My special thanks to PVS-Studio PVC Logo

 

My special thanks to Tipi.build tipi.build logo

 

My special thanks to Take Up Code TakeUpCode 450 60

 

Seminars

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

Bookable (Online)

German

Standard Seminars (English/German)

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

  • C++ - The Core Language
  • C++ - The Standard Library
  • C++ - Compact
  • C++11 and C++14
  • Concurrency with Modern C++
  • Design Pattern and Architectural Pattern with C++
  • Embedded Programming with Modern C++
  • Generic Programming (Templates) with C++

New

  • Clean Code with Modern C++
  • C++20

Contact Me

Modernes C++,

RainerGrimmDunkelBlauSmall

 

 

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

Stay Informed about my Mentoring

 

Mentoring

English 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

Course: The All-in-One Guide to C++20

Course: Master Software Design Patterns and Architecture in C++

Subscribe to the newsletter (+ pdf bundle)

All tags

Blog archive

Source Code

Visitors

Today 4497

Yesterday 4550

Week 4497

Month 26171

All 12104380

Currently are 181 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments