6

I'm wetting my feet with C++11 and am really confused why this doesn't work:

template <class T>
struct A {
  size_t size() const { return sizeof(T); }
};

struct B : A<B> {
  int x;
  int y;
};

B var {1, 5};

I'm using gcc 4.8.2 and get an error saying:

no matching function for call to 'B(<brace-enclosed initializer list>)'

It works just fine when I don't derive from A, so does the derivation somehow change the POD-ness of my struct B?

eddi
  • 49,088
  • 6
  • 104
  • 155

1 Answers1

11

Aggregate-initialization requires your type to be an aggregate. An aggregate cannot have base classes:

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

David G
  • 94,763
  • 41
  • 167
  • 253
  • Ah, so the second part of [this answer](http://stackoverflow.com/questions/4178175/what-are-aggregates-and-pods-and-how-why-are-they-special/7189821#7189821) doesn't apply. Thanks. – eddi May 06 '15 at 20:08
  • 2
    This [could change in C++17](http://stackoverflow.com/questions/4178175/what-are-aggregates-and-pods-and-how-why-are-they-special/27511360#comment47403725_27511360) there is currently a proposal being considered. – Shafik Yaghmour May 06 '15 at 22:08