mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2025-12-17 12:44:42 +03:00
Fix C.183.
This commit is contained in:
@@ -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)
|
void if_you_must_pun(int& x)
|
||||||
{
|
{
|
||||||
auto p = reinterpret_cast<unsigned char*>(&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.
|
but at least we can see that something tricky is going on.
|
||||||
|
|
||||||
##### Note
|
##### 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.
|
Unfortunately, `union`s are commonly used for type punning.
|
||||||
We don't consider "sometimes, it works as expected" a strong argument.
|
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
|
##### Enforcement
|
||||||
|
|
||||||
???
|
???
|
||||||
|
|||||||
Reference in New Issue
Block a user