On Fri, Jul 30, 2021 at 11:00:26AM -0400, Jason Merrill wrote:
> Patch attached.

LGTM (which would mean I'll need to replace that particular test union
with a different one which will have just a non-std-layout member of the
anon struct, see below), but I guess we want a testcase for this, e.g.
struct E { };
struct X { int a; struct : public E { short b; long c; }; long long d; };
union Y { int a; struct : public E { short b; long c; }; long long d; };
will do it.

> > But standard layout means that even all the non-static members of the struct
> > need to be standard-layout, that seems an unnecessary requirement for
> > anon structures to me.
> 
> Good point.
> 
> But then, if the anonymous struct is non-standard-layout, that should make
> the enclosing class non-standard-layout as well, so we should never need to
> consider in the pointer-interconv code whether the anonymous struct is
> standard-layout.

For non-std-layout anon struct in a non-union class sure.
But, for non-std-layout anon struct in a union, while it makes the union
also non-std-layout, pointer-interconvertibility doesn't care about
std-layout, even in non-std-layout unions address of each of the union member
is pointer-interconvertible with the address of the whole union object.
But when we recurse into the anon struct, the rule for non-union classes
applies and it only handles std-layout.

So something like:
struct D { int x; private: int y; };
union Y { int a; struct { short b; long c; D z; }; long long d; };

static_assert (std::is_pointer_interconvertible_with_class (&Y::a));
static_assert (!std::is_pointer_interconvertible_with_class (&Y::b));
static_assert (!std::is_pointer_interconvertible_with_class (&Y::c));
static_assert (std::is_pointer_interconvertible_with_class (&Y::d));

D is not std-layout, therefore the anon-struct is not std-layout either
and neither is union Y, for &Y::a and &Y::d the union
pointer-interconvertibility rule applies and both are
pointer-interconvertible, for the anon-struct it is not std-layout and
therefore address of the artificial member with anon-struct type
is not pointer-interconvertible with address of the b member.

        Jakub

Reply via email to