c++11 struct initialization compilation error

In C++11, when you use non static data member initialization at the point of declaration like you do here:

struct SS {int a = 0; int s = 2;};

you make the class a non-aggregate. This means you can no longer initialize an instance like this:

SS s{1,2};

To make this initialization syntax work for a non-aggregate, you would have to add a two-parameter constructor:

struct SS 
{
  SS(int a, int s) : a(a), s(s) {}
  int a = 0; 
  int s = 2;
};

This restriction has been lifted in C++14.

Note that you may want to add a default constructor for the class. The presence of a user-provided constructor inhibits the compiler generated default one.

See related reading here.


Use of a default member initializer renders the class/struct a non-aggregate:

§ 8.5.1 Aggregates

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).

Semantics differ for aggregates and non-aggregates:

Aggregates (e.g., arrays and structs):

Initialize members/elements beginning-to-end.

Non-aggregates:

Invoke a constructor.

v.push_back(SS{1, 2}); // Error, it tries to call SS constructor

Which means you need a constructor now:

struct SS 
{
  SS(int a, int s) : a(a), s(s) 
  {
  }
  int a = 0; 
  int s = 2;
};

Tags:

C++

C++11