C++C++14C++17C++20Learn C++

What Is Aggregate Member Initialization In C++?

What Is Aggregate Member Initialization In C++

The Aggregate Member Initialization is one of the features of C++. This feature is improved and modernized with C++11, C++14 and C++20. With this feature, objects can initialize an aggregate member from braced-init-list. In this post, we explain what the aggregate member initialization is and what were the changes to it in modern C++ standards.

What is aggregate member initialization in modern C++?

Aggregate initialization initializes aggregates. Since C++11, aggregates are a form of listed initializations. Since C++20 they are direct initializations. An aggregate could be an array or class type (a class, a struct, or a union). 

Here is the general syntax,

In C++11 and above, we use without = as below,

In C++20, there are 3 new options that we can use,

How to use aggregate member initialization in modern C++?

C++14 provides a solution to problems in C++11 and above, for example in C++14, consider we have x, y coordinates in a struct, we can initialize them as below in a new xy object,

In modern C++, consider that we have a struct that has a, b, c members. We initialize first two members as below,

We can directly initialize as below too,

In C++17 and above, we can use this st_y as a base and we can add a new member to a new struct, then we can initialize as below,

What restrictions are there for the aggregate member initialization in C++?

If we consider the C++17 standard, an aggregate initialization can NOT be applied to a class type if it has one of the below,

  • private or protected non-static data members,
  • a constructor that is user-provided, inherited, or explicit constructors (explicitly defaulted or deleted constructors are allowed),
  • base class or classes (virtual, private, protected),
  • virtual member functions

If we consider the C++20 standard, an aggregate initialization can NOT be applied to a class type if it has one of the below,

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

Is there a full example of aggregate member initialization in C++?

Here is a full example that explains simply most used features of aggregate member initialization,

and the output will be as below,

For more information about this feature, please see https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3651.pdf

What Is Aggregate Member Initialization In C++ The C++ Builder Logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here



Class type shuld

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?