Why can't a class trivially be considered an aggregate type?

From C ++ Draft Standard n3337 :

8.5.1 Aggregates [dcl.init.aggr]

1 An aggregate is an array or class (section 9) without user-supplied constructors (12.1), no binding or alignment for non-static data members (9.2), no private or protected non-static data (section 11), no base classes (section 10) and there are no virtual functions (10.3).

Let's say I have two classes:

struct ABase
{
   int value;
};

struct A : public ABase {};

      

According to the standard, it ABase

is an aggregate type, but A

not. This means it's OK to create an instance ABase

using:

ABase a1{10};

      

but fails to instantiate A

using:

A a2{20};

      

A

is trivially derived from ABase

and can potentially be viewed as a collection. My question is, what types of errors, the user of the language as well as the compiler designer can work by treating A

as an aggregate type?

+3


source to share


1 answer


Apparently my reputation doesn't allow me to leave comments and I'm not sure if that's okay because it can be interpreted as subjective, but here's the reason a "language user" might find this concept troublesome.

Initialization order. If a class has both base and non-static data aggregates, what order are they initialized in? Base classes before non-static data members? And of course, how about when base classes have base classes, or with multiple base classes. Then there is of course virtual inheritance, etc. It might come as a surprise to a novice programmer that the members of a class in the mem-initializer-list constructor are initialized in declaration order, not in list order. I believe that further confusion about initialization (in this case, aggregates) would make it harder to learn the language. Given that one of the goals of Bjarne Stroustrup's C ++ 11 lists is to "make learning and learning easier", adding confusion should not be taken lightly. I certainly understand,that there would be no ambiguity and why I avoided using the word, it simply would not have an order that everyone must immediately accept correctly.



I guess my opinion is that there is little that comes out of the base class resolution, and this can be confusing. This will likely require careful language in the standard to allow it.

0


source







All Articles