Closed Eisenwave closed 4 weeks ago
https://github.com/cplusplus/papers/issues/592 does not resolve this issue.
https://github.com/cplusplus/papers/issues/1703 does not resolve this issue.
I think this is blocked on P1839. It's currently even unclear where the object representation of an object is located. (P1839R5 clarifies that the object representation occupies the same storage of the object).
Hmm yeah, P1839 lists this as a known issue:
The current proposed wording allows
reinterpret_cast
orstd::launder
tounsigned char*
to produce a pointer to the first element of an object representation. The same cannot be said forstd::byte*
.
It doesn't appear to resolve that known issue though.
P1839 should (even if it currently does not) address this. This is too large for a core issue. If you feel P1839 is stalled, please contact its author and/or submit your own paper to address the specification holes around object representations.
Reference (section label): [basic.types.general]
Issue description
The following code has well-defined behavior, and changes the value of an object by modifying its first byte in-place.
An object representation is a sequence of
unsigned char
objects, the first of which logically has to exist at the same address as the object. Therefore,std::launder
can be used to obtain a pointer to the first byte of the object representation, and [basic.types.general] paragraph 4 states:Because the value is determined directly by the object representation, and because
std::uint32_t
has no padding bits, this code has well-defined behavior. The value ofx
should change accordingly when its object representation is being modified.It is undesirable to support direct modifications of the object representation; there are already better mechanisms for type punning such as
std::bit_cast
.Note that obtaining a pointer to the object representation raises a few additional problems:
std::launder
ifx
is anunsigned char
obtain a pointer tox
, or to a byte of its object representation?std::launder
to an array that provides storage?Suggested resolution
(none yet)