Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 aggregate initialization for classes with non-static member initializers

Is it allowed in standard:

struct A
{
  int a = 3;
  int b = 3;
};

A a{0,1}; // ???

Is this class still aggregate? clang accepts this code, but gcc doesn't.

like image 640
Bikineev Avatar asked Nov 25 '14 03:11

Bikineev


People also ask

Where can a non-static reference member variable of a class be initialized?

Member initialization Non-static data members may be initialized in one of two ways: 1) In the member initializer list of the constructor.

What is non-static class member in C++?

Non-static data members are the variables that are declared in a member specification of a class.

What is aggregate initialization in C++?

Explanation. Aggregate initialization initializes aggregates. It is a form of list-initialization (since C++11) or direct initialization (since C++20). An aggregate is one of the following types: array type.

What is Nsdmi C++?

In Visual Studio 2013 we shipped an implementation of non-static data member initialization (hereby referred to as “NSDMI”), which is a feature that allows code such as the following: class C { int n = 42; };


1 Answers

In C++11 having in-class member initializers makes the struct/class not an aggregate — this was changed in C++14, however. This is something I found surprising when I first ran into it, the rationale for this restriction is that in-class initializers are pretty similar to a user defined constructor but the counter argument is that no one really expects that adding in-class initializers should make their class/struct a non-aggregate, I sure did not.

From the draft C++11 standard section 8.5.1 Aggregates (emphasis mine going forward):

An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no brace-or-equal initializers for non-static data members (9.2), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).

and in C++14 the same paragraph reads:

An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).

This change is covered in N3605: Member initializers and aggregates which has the following abstract:

Bjarne Stroustrup and Richard Smith raised an issue about aggregate initialization and member-initializers not working together. This paper proposes to fix the issue by adopting Smith's proposed wording that removes a restriction that aggregates can't have member-initializers.

This comment basically sums up the reluctance to allowing them to be aggregates:

Aggregates cannot have user-defined constructors and member-initializers are essentially some kind of user-defined constructor (element) (see also Core Defect 886). I'm not against this extension, but it also has implications on what our model of aggregates actually is. After acceptance of this extension I would like to know how to teach what an aggregate is.

The revised version N3653 was adopted in May 2013.

Update

emsr points out that G++ 5.0 now supports C++14 aggregates with non-static data member initializers using either std=c++1y or -std=c++14:

struct A { int i, j = i; };
A a = { 42 }; // a.j is also 42

See it working live.

like image 124
Shafik Yaghmour Avatar answered Oct 19 '22 13:10

Shafik Yaghmour