Designated Initializers with Private Members

Can designated initializers be used with types that have private or protected members?

Designated initializers, introduced in C++20, are a convenient way to initialize objects by specifying the member names. However, they can only be used with aggregate types, which do not include private or protected members.

Let's consider an example with an aggregate type:

#include <iostream>

struct Vec3 {
  int x{0};
  int y{0};
  int z{0};
};

int main() {
  Vec3 vector{.x = 1, .y = 2, .z = 3};  
  std::cout << "x=" << vector.x
    << ", y=" << vector.y
    << ", z=" << vector.z;
}
x=1, y=2, z=3

In this case, Vec3 is an aggregate type because it has no private members, non-public base classes, or user-defined constructors. This allows us to use designated initializers to initialize its members.

However, if Vec3 had private or protected members, it would no longer be considered an aggregate, and designated initializers would not work:

#include <iostream>

struct Vec3 {
private:
  int x{0};
  int y{0};
  int z{0};

public:
  Vec3(int a, int b, int c)
    : x{a}, y{b}, z{c} {}

  void Log() {
    std::cout << "x=" << x
      << ", y=" << y
      << ", z=" << z << '\n';
  }
};

int main() {
  Vec3 vector{.x = 1, .y = 2, .z = 3}; 
  vector.Log();
}
error: 'Vec3': designated initialization can only be used to initialize aggregate class types

This code will not compile because Vec3 is no longer an aggregate type due to the private members. Designated initializers cannot be used in this scenario.

To initialize types with private or protected members, you must use constructors. Constructors allow you to control how the members are initialized and can enforce encapsulation rules.

Here is how you can modify the Vec3 struct to use a constructor instead:

#include <iostream>

struct Vec3 {
 private:
  int x{0};
  int y{0};
  int z{0};

 public:
  Vec3(int a, int b, int c)
    : x{a}, y{b}, z{c} {}

  void Log() {
    std::cout << "x=" << x
      << ", y=" << y
      << ", z=" << z;
  }
};

int main() {
  Vec3 vector{1, 2, 3};  
  vector.Log();
}
x=1, y=2, z=3

In summary, designated initializers are great for aggregate types but cannot be used with types that have private or protected members. For such types, constructors are the appropriate way to handle initialization.

List, Aggregate, and Designated Initialization

A quick guide to creating objects using lists, including std::initializer_list, aggregate and designated initialization

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

List Initialization with Private Members
Can we use list initialization for classes with private or protected members?
Aggregate Initialization for Nested Structures
How does aggregate initialization work for complex types with nested structures?
Using Initializer Lists in Custom Containers
How can we use std::initializer_list to initialize custom container types?
Aggregate Initialization with Inheritance
How does aggregate initialization interact with inheritance and derived classes?
Heterogeneous Initializer Lists
Can std::initializer_list be used with heterogeneous data types in any way?
List Initialization with constexpr and constinit
How does list initialization interact with constexpr and constinit specifiers in C++20?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant