C++ Logo


Advanced search

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

From: language.lawyer_at <language.lawyer_at_[hidden]>
Date: Fri, 21 Oct 2022 20:32:28 +0500
>>>>> 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.

>> 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?


Received on 2022-10-21 15:32:34