std::bit_cast is a C++20 function template in the <bit> header that reinterprets the object representation of one value as another type of the same size, without undefined behaviour. It replaces the unsafe habits of pointer casts and union type punning, it is constexpr in most cases, and compilers turn it into a plain register move or a no-op.
The "Bitwise Cast Library" in the question is not a separate library. It is this one function, defined in <bit> alongside the other bit utilities.
The call only compiles when both conditions hold:
sizeof(To) == sizeof(From).To and From are trivially copyable.If the types do not meet these rules, you get a compile error instead of silent misbehaviour. The result is a new object whose bytes are copied from the source, so no object is "viewed through" another type and no aliasing rules are broken.
Support is available in the current releases of GCC, Clang and MSVC when you compile with C++20 or later (-std=c++20). Check __cpp_lib_bit_cast if you need a feature test.
This works for IEEE 754 formats, which are what mainstream platforms use. The numeric result depends on the platform's floating-point representation and, for multi-byte integers, on its endianness.
Before C++20, developers wrote one of these:
The first violates strict aliasing. The second is allowed in C but not in C++, although some compilers accept it as an extension. The only portable pre-C++20 option was std::memcpy, which is still correct and is what std::bit_cast is equivalent to:
| Technique | Defined in C++? | constexpr | Notes |
|---|---|---|---|
reinterpret_cast on pointers | No (aliasing) | No | Avoid for type punning |
| Union member read | No | Limited | Compiler extension only |
std::memcpy | Yes | No | Verbose, needs an lvalue |
std::bit_cast | Yes | Yes | Works on temporaries and in constant expressions |
Because std::bit_cast is constexpr, you can examine floating-point values at compile time:
isNegative(-0.0f) is true even though -0.0f < 0.0f is false, which is a case where inspecting the sign bit is the only reliable test (the standard library also offers std::signbit).
In a constant expression the call fails to compile if either type contains a union, a pointer, a pointer to member, or a volatile member, because the compiler cannot represent those bytes at compile time.
The types do not need to be arithmetic. Any trivially copyable pair of the same size works:
On a little-endian machine the second line prints 44332211. Padding bytes in a struct have unspecified values in the result, so avoid structs with padding when exact bits matter.
std::bit_cast<std::uint64_t>(1.0f) does not compile. Convert deliberately or choose a matching type.std::endian from <bit> to branch on byte order, and std::byteswap (C++23) to swap bytes.std::bit_cast<std::uintptr_t>(ptr) is allowed at runtime, but it is not usable in constant expressions.static_cast<int>(1.5f) converts the value, while std::bit_cast<int>(1.5f) copies the raw bits.No. reinterpret_cast reinterprets a pointer or reference to existing memory and can break aliasing rules. std::bit_cast copies the bits into a new object of another type and is well defined.
No. Optimising compilers typically generate the same code for both, often a single register move.
Include <bit> and compile as C++20 or later.
No. The sizes must be identical, otherwise the program is ill-formed. Use memcpy of a smaller size or shift and mask logic for partial copies.
std::bit_cast<To>(from) is the standard, safe way to reinterpret an object's bits in C++20 and later. It requires equal sizes and trivially copyable types, works in constexpr code, and replaces reinterpret_cast punning, union tricks and most memcpy calls. Mind endianness and padding when you depend on the exact bit layout.