Date: Mon, 26 Dec 2022 00:27:25 -0500
On Sun, Dec 25, 2022 at 11:23 PM Matthew House via Std-Discussion
<std-discussion_at_[hidden]> wrote:
>
> On Sun, Dec 25, 2022 at 9:42 PM Jason McKesson via Std-Discussion
> <std-discussion_at_[hidden]> wrote:
> > OK, but... a reference to a base class subobject is also a reference
> > to the most derived-object, right?
>
> I don't think this is correct. A standard-layout class object is
> pointer-interconvertible with its base class subobjects (6.8.4
> [basic.compound] para. 4), so they share the same address. But a
> reference to a base class subobject is logically distinct from a
> reference to the most derived object, and in general one cannot be
> used where the other is expected.
The reason I believe that a reference to a base class subobject is
also a reference to the derived object is because of the way the
strict aliasing rule is worded.
[basic.lval]/11 talks about accessing "the stored value of an object
through a glvalue whose type is not
similar (7.3.5) to one of the following types". Among them is the
dynamic type of the object. So you would have to have a glvalue of a
base class type accessing the stored value of the dynamic type of the
object.
So a reference to a base class subobject "knows" that the actual type
it refers to is the dynamic type of the object.
<std-discussion_at_[hidden]> wrote:
>
> On Sun, Dec 25, 2022 at 9:42 PM Jason McKesson via Std-Discussion
> <std-discussion_at_[hidden]> wrote:
> > OK, but... a reference to a base class subobject is also a reference
> > to the most derived-object, right?
>
> I don't think this is correct. A standard-layout class object is
> pointer-interconvertible with its base class subobjects (6.8.4
> [basic.compound] para. 4), so they share the same address. But a
> reference to a base class subobject is logically distinct from a
> reference to the most derived object, and in general one cannot be
> used where the other is expected.
The reason I believe that a reference to a base class subobject is
also a reference to the derived object is because of the way the
strict aliasing rule is worded.
[basic.lval]/11 talks about accessing "the stored value of an object
through a glvalue whose type is not
similar (7.3.5) to one of the following types". Among them is the
dynamic type of the object. So you would have to have a glvalue of a
base class type accessing the stored value of the dynamic type of the
object.
So a reference to a base class subobject "knows" that the actual type
it refers to is the dynamic type of the object.
Received on 2022-12-26 05:29:56