Subject: Re: [std-proposals] Allowing access to object representations
From: Timur Doumler (cpp_at_[hidden])
Date: 2019-08-22 04:20:14
Every major compiler is currently doing the right thing with this code:
int x = 100000;
char ch = *reinterpret_cast<char*>(&x);
which is: to interpret ch as the *first byte* of x (which will be different depending on endianness), and not to interpret this char as somehow holding the value of the whole int object.
Wording has been cited here which can be interpreted as requiring the latter behaviour, rather than the former. I believe we are simply looking at a wording defect. I can not think of any situation where such behaviour (ch represents âthe whole objectâ rather than the first byte) would be useful.
I believe we should clarify the wording such that it becomes unambiguous that ch points to a *byte* of x. We should also clarify that this is not UB, even if 100000 does not fit into a char, as this âfitting the whole object into a char" is not at all what should be going on. We should also fix the wording to allow to access the other bytes of x, not just the first one, without causing UB (by allowing pointer arithmetic and looking at the other addresses inside the char array that provides storage for x).
This would give us a portable way of looking at the bytes of an object (which ought to be possible anyway â this is why we have the âchar and std::byteâ can alias any typeâ rule in the first place).
I think if we would have such a fix, then together with Richardâs P0593 this would be a comprehensive plug for this gaping hole in the language.
What do yâall think?
> On 22 Aug 2019, at 09:17, Jake Arkinstall via Std-Proposals <std-proposals_at_[hidden]> wrote:
> On Thu, 22 Aug 2019, 02:06 sdkrystian via Std-Proposals, <std-proposals_at_[hidden] <mailto:std-proposals_at_[hidden]>> wrote:
> > It does. But the char pointer points to the first byte. Which is platform dependant.
> Don't think so: "the pointer value is unchanged" means exactly what it says, it still points to the int object. It is not specified anywhere that it will point to any char object.
> > Which is a char.
> No, it is never specified that it points to any char object. It points to an int object.
> You have a pointer some int. That int is multiple char widths long. You create a pointer to a char and reinterpret cast the int pointer into a pointer to char. It might truly be a pointer to int, but by this point there is no way for any code accepting that char pointer to know that it is truly a pointer to int, char, or an elephant, but that, for all intents and purposes, it can be treated as a char. Dereferencing the char pointer grants you one char representing the value of the leftmost byte of the integer.
> What in the above is incorrect?
> Im asking specifically because what you're saying directly contradicts an example provided at the bottom of https://en.cppreference.com/w/cpp/language/reinterpret_cast <https://en.cppreference.com/w/cpp/language/reinterpret_cast>, where a char pointer to an int is dereferenced to determine the endianness of the system.
> Std-Proposals mailing list
STD-PROPOSALS list run by herb.sutter at gmail.com
Standard Proposals Archives on Google Groups