diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 4c49955..420aed9 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -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(&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 ???