3

From C++ Draft Standard n3337:

8.5.1 Aggregates [dcl.init.aggr]

1 An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no brace-or-equalinitializers 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).

Say I have two classes:

struct ABase
{
   int value;
};

struct A : public ABase {};

Based on the standard, ABase is an aggregate type while A is not. That means, it's OK to create an instance of ABase using:

ABase a1{10};

but it's not OK to create an instance of A using:

A a2{20};

A is trivially derived from ABase and could potentially have been treated as an aggregate. My question is what sorts of pitfalls one, a user of the language as well as an implementor of a compiler, can run into by treating A as an aggregate type?

Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 1
    The initializer syntax may get (even more) complicated, otherwise I think you have a point. – Quentin Nov 11 '14 at 23:11
  • IMHO it should b changed to ` no base classes or all base classes are aggregate` – SwiftMango Nov 12 '14 at 00:28
  • 1
    Are you asking [Why does having a base class disqualify a class from being aggregate?](https://stackoverflow.com/questions/19846819/why-does-having-a-base-class-disqualify-a-class-from-being-aggregate) (If so, I propose it as a possible duplicate.) –  Nov 12 '14 at 00:43
  • 1
    @remyabel, yes, it is a duplicate. – R Sahu Nov 12 '14 at 04:12
  • In your particular example, you are better of with an alias. – Walter Nov 12 '14 at 09:16
  • None of the 3 answers to that other question are satisfactory, though: there appears no obvious reason. I suspect that the standard committee wanted to avoid the work needed to implement what you want. – Walter Nov 12 '14 at 09:26
  • @Walter I agree unfortunately the extra attention did not draw a better answer – Shafik Yaghmour Nov 12 '14 at 12:55

1 Answers1

0

Apparently my reputation does not allow me to post a comment and I am not sure that it is OK to post this as it can be interpreted as subjective, but here is a reason that "a user of the language" may find this concept troublesome.

Order of initialization. If a class has aggregates as bases as well as non-static data members, what order to they get initialized in? Base classes before non-static data members? And of course, how about when the base classes have base classes or with multiple base classes. Then there is of course virtual inheritance, etc. It may even come as a surprise to a novice programmer that class members in constructor mem-initializer-lists are initialized in declaration order rather than list order. I believe that allowing further confusion with initialization (in this case of aggregates) would make the language harder to learn. Given that one of the goals for C++11 Bjarne Stroustrup lists is "Making the language easier to teach and learn", adding confusion is not to be taken lightly. Of course, I realize that there would be no ambiguity and that's why I avoided using that word, it would just not have an order that everyone would necessarily immediately assume correctly.

I guess my view is that little is gained by allowing base classes and it could lead to confusion. It would likely necessitate careful wording in the standard to allow this.

  • I was hoping to get some answers from people who are more familiar with the C++ standard development process. What you say makes sense if a derived class adds non-static member variables. In the case of the example that I presented, that is not the case. – R Sahu Nov 12 '14 at 05:10