C++ Logo

std-discussion

Advanced search

Re: What is an access in [res.on.objects]?

From: Edward Catmur <ecatmur_at_[hidden]>
Date: Fri, 21 Oct 2022 16:45:58 +0100
On Fri, 21 Oct 2022 at 16:32, <language.lawyer_at_[hidden]> wrote:

> >>>>> According to a non-normative note in
> >>>> https://eel.is/c++draft/defns.access,
> >>>>>
> >>>>> "Only glvalues of scalar type can be used to access objects. ...
> >>>> Attempts to read or modify an object of class type typically invoke a
> >>>> constructor or assignment operator; such invocations do not themselves
> >>>> constitute accesses, although they may involve accesses of scalar
> >>>> subobjects."
> >>>>>
> >>>>> The question is, can objects of class type be accessed?
> >>>>
> >>>> Can, in some sense. But it would be UB. E.g.
> >>>>
> >>>> struct S
> >>>> {
> >>>> char buf[sizeof(int)];
> >>>> } s;
> >>>>
> >>>> reinterpret_cast<int&>(s) = 0; // kinda accesses `s`, but this
> violates
> >>>> strict aliasing, so the behavior is actually undefined
> >>>>
> >>>
> >>> It accesses `s`'s storage, sure.
> >>
> >> What do you mean by explicitly writing Ā«storageĀ»? You disagree with that
> >> the expression accesses `s`?
> >>
> >
> > Yes, since `s` is not a glvalue of scalar type.
>
> But reinterpret_cast<int&>(s) is. And it denotes the `s` object. Per
> [expr.ass]/2, the referred-to object is accessed.
>

Why does it denote `s`? That looks to me like it ends the lifetime of `s`.

>> But a read would not have undefined behavior.
> >>
> >> [basic.lval]/11 apply uniformly to both kinds of access: reads and
> writes.
> >>
> >
> > Oh oops, didn't spot that the reinterpret_cast was to int. If it was to a
> > storage-like type, that would be well-defined, no?
>
> No.
>

Why would [basic.types.general] not hold?

Received on 2022-10-21 15:46:11