On Wednesday 15 April 2015 21:41:34
omnipote...@gmail.com wrote:
> > That would allow a union with non-static members with differing access to
> > be
> > POD (standard layout + trivial). Did C++98 allow that?
>
> It seems that the C++98 standard carried this text in 9.0.4:
>
> POD-struct is an aggregate class that has no non-static data members of
> type non-POD-struct, non-POD-union (or array of such types) or reference,
> and has no user-declared copy assignment operator and no user-declared
> destructor. Similarly, a POD-union is an aggregate union that has no
> non-static data members of type non-POD-struct, non-POD-union (or array of
> such types) or reference, and has no user-declared copy assignment operator
> and no user-declared destructor. A POD class is a class that is either a
> POD-struct or a POD-union.
>
> Where an aggregate class is defined in 8.5.1:
>
> An aggregate is an array or a class (clause 9) with no user-declared
> constructors (12.1), no private or protected non-static data members
> (clause 11), no base classes (clause 10), and no virtual functions (10.3).
>
> So no. But remember that the purpose of N2342 was to fix the fact that this
> definition was too restrictive. For instance, a union with all private
> members (which is POD now) would not have been POD in C++98.
Right. It relaxed the requirement but still left the same rules for all types
of aggregates, whether they be unions or not.
That said, I can't think of a good reason why different accesses in a union
should cause it to change layout, given the requirement that all members have
the same address. If it doesn't change the layout, it stands to reason it's
the same layout as the standard layout.
A far-fetched scenario would be that the union has extra bookkeeping because
of protected or private members, but I can't fathom a reason where this would
be necessary.