Aggregate Initialization for Classes with Non-Static Member Initializers
In C , aggregate initialization is a convenient syntax for initializing members of a class or struct without explicitly calling a constructor. However, the use of in-class member initializers raises the question of whether the class/struct remains an aggregate.
C 11 and Aggregate Status
According to the C 11 standard, having in-class member initializers prevents a class/struct from being considered an aggregate. This is due to the fact that member initializers resemble user-defined constructors, which are not allowed in aggregates.
struct A { int a = 3; int b = 3; }; A a{0, 1}; // Invalid for C++11
C 14 and Aggregate Initialization
However, in C 14, this restriction was lifted. Aggregate initialization is now permitted for classes/structs with non-static member initializers. This change was made to address developer feedback and simplify the understanding of aggregate initialization.
struct A { int a = 3; int b = 3; }; A a{0, 1}; // Valid for C++14
G Support for C 14 Aggregates
Note that G version 5.0 and later support C 14 aggregate initialization for classes/structs with non-static member initializers. However, older versions of G may not recognize this syntax.
The above is the detailed content of Can Classes with Non-Static Member Initializers Be Aggregate Initialized in C ?. For more information, please follow other related articles on the PHP Chinese website!