From d4224eb9ac8b7b548d2ee8f12ee50d0f90516a85 Mon Sep 17 00:00:00 2001 From: Hong Xu Date: Tue, 14 Oct 2025 23:03:39 -0700 Subject: [PATCH] C.183: Mention bit_cast as a C++20+ solution --- CppCoreGuidelines.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 5ea72070c..6e30889c0 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -9032,7 +9032,9 @@ 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 conclusive 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. +Modern C++ introduced `std::byte` (C++17) and `std::bit_cast` (C++20) to facilitate operations on raw object representations. +Use `reinterpret_cast` along with `std::byte` instead of `unsigned char` or `char` for these operations. +For `std::bit_cast`, if there is no value of the return type (the `To` type) that can represent the value produced, the behavior is undefined. ##### Enforcement