From f9f3422ac5fd4339c720e4d5dde01088500caba3 Mon Sep 17 00:00:00 2001 From: Gabriel Dos Reis Date: Mon, 1 May 2017 11:05:32 -0700 Subject: [PATCH] Fix C.183. --- CppCoreGuidelines.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 ???