mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2024-03-22 13:30:58 +08:00
Fix C.183.
This commit is contained in:
parent
918a5695c7
commit
f9f3422ac5
|
@ -8024,11 +8024,11 @@ 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<unsigned char*>(&x);
|
||||
cout << p[0] << '\n'; // undefined behavior
|
||||
cout << p[0] << '\n'; // OK; better
|
||||
// ...
|
||||
}
|
||||
|
||||
Accessing the result of an `reinterpret_cast` to a different type from the objects declared type is still undefined behavior,
|
||||
Accessing the result of an `reinterpret_cast` to a different type from the objects declared type is defined behavior (even though `reinterpret_cast` is discouraged),
|
||||
but at least we can see that something tricky is going on.
|
||||
|
||||
##### Note
|
||||
|
@ -8036,6 +8036,8 @@ but at least we can see that something tricky is going on.
|
|||
Unfortunately, `union`s are commonly used for type punning.
|
||||
We don't consider "sometimes, it works as expected" a strong argument.
|
||||
|
||||
C++17 introduced a distinct type `std::byte` to facilitate operations on raw object representation. Use that type instead of `unsigned char` or `char` for these operations.
|
||||
|
||||
##### Enforcement
|
||||
|
||||
???
|
||||
|
|
Loading…
Reference in New Issue
Block a user