C++ Logo

std-discussion

Advanced search

Re: `from_promise` and base classes

From: Matthew House <mattlloydhouse_at_[hidden]>
Date: Mon, 26 Dec 2022 10:51:43 -0500
On Mon, Dec 26, 2022 at 12:29 AM Jason McKesson via Std-Discussion
<std-discussion_at_[hidden]> wrote:
> 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.

That just means that a program can read and modify scalar objects
based on the most derived object. It doesn't follow that a reference
which refers to a base class subobject also refers to any object
containing that base class subobject.

Probably the closest language to what you're looking for is 3.19
[defns.dynamic.type], which defines the dynamic type of a glvalue as
the "type of the most derived object to which the glvalue refers".
However, the Standard otherwise consistently maintains, e.g., in 9.4.4
[dcl.init.ref], a distinction between a glvalue referring to an object
and a glvalue referring to one of its base class subobjects.

How I'd read it is that for each glvalue that refers to an object,
there is the "object to which the glvalue refers", with a type similar
to its static type, and the "most derived object to which the glvalue
refers", with a type similar to its dynamic type. Unless some part of
the Standard explicitly mentions a dynamic type or most derived
object, it should be understood as meaning the "object to which the
glvalue refers", which can be a base class subobject distinct from the
most derived object.

Received on 2022-12-26 15:51:56