Open
Description
Reference:
Example, bad
union Pun { int x; unsigned char c[sizeof(int)]; };The idea of
Pun
is to be able to look at the character representation of anint
.void bad(Pun& u) { u.x = 'x'; cout << u.c[0] << '\n'; // undefined behavior }If you wanted to see the bytes of an
int
, use a (named) cast:void if_you_must_pun(int& x) { auto p = reinterpret_cast<std::byte*>(&x); cout << p[0] << '\n'; // OK; better // ... }Accessing the result of a
reinterpret_cast
from the object's declared type tochar*
,unsigned char*
, orstd::byte*
is defined behavior. (Usingreinterpret_cast
is discouraged, but at least we can see that something tricky is going on.)
The expression cout << p[0]
does not compile because there is no operator<<
overload that accepts a std::byte
. This makes sense—std::byte
represents raw binary data, not a printable character. To print its value, we can convert it to a suitable type using std::to_integer
:
void if_you_must_pun(int& x)
{
auto p = reinterpret_cast<std::byte*>(&x);
- cout << p[0] << '\n'; // OK; better
+ cout << std::to_integer<char>(p[0]) << '\n'; // OK; better
// ...
}
Metadata
Metadata
Assignees
Labels
No labels