4

I'm puzzled by this fragment of (C++14) code I wrote:

struct B {};
struct C     { int m; };
struct D : B { int m; };

int main() {
    C c = { 1 }; // this works
    D d = { 1 }; // this doesn't work
}

I'm fine writing a constructor for D myself, but I can't find a good explanation for why the struct D is no longer initializable with an initializer list. All I changed was make it inherit from a completely empty class -- I suppose I somehow made it behave less struct-like.

How exactly does my compiler handle the structs C and D differently?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Lynn
  • 10,425
  • 43
  • 75

3 Answers3

5

It works for C because it is an aggregate and therefore it is using aggregate initialization but D is not an aggregate because it has a base class. The obvious work-around as you mention is to write a constructor.

This is covered in the draft C++ standard section 8.5.1 Aggregates [dcl.init.aggr] with emphasis mine:

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

There is a proposal: Extension to aggregate initialization to remove that restriction. As chris points out this was accepted by Evolution Working Group but as far as I understand now needs to accepted by Core as well.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • 2
    According to [this trip report](https://botondballo.wordpress.com/2015/11/09/trip-report-c-standards-meeting-in-kona-october-2015/), the proposal was accepted at Kona. – chris Dec 11 '15 at 19:43
  • @chris I did not see the changes applied to the latest draft so I was not sure, although I thought I remembered reading that it was accepted from that trip report. – Shafik Yaghmour Dec 11 '15 at 19:49
  • @chris ok, now I get it. It was accepted by EWG, so as I understand it. It now has to go to CWG before it can be applied to the draft standard. – Shafik Yaghmour Dec 11 '15 at 19:50
3

D is no longer an aggregate type so it cannot be directly initialized with an initialization list. If you provide a constructor then it will work.

struct B {};
struct C     { int m; };
struct D : B { int m; D(int m_) : m(m_) {} };

int main() 
{
    C c = { 1 }; // this works
    D d = { 1 }; // this works
}

Live Example

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
-1

struct c is a POD while struct D is an object. Class initializer rules apply because of the inheritance.