|
|
bit_cast (C++20) | ||||
(C++23) | ||||
Integral powers of2 | ||||
(C++20) | ||||
(C++20) | ||||
(C++20) | ||||
(C++20) | ||||
Rotating | ||||
(C++20) | ||||
(C++20) | ||||
Counting | ||||
(C++20) | ||||
(C++20) | ||||
(C++20) | ||||
(C++20) | ||||
(C++20) | ||||
Endian | ||||
(C++20) |
Defined in header <bit> | ||
template<class To,class From> constexpr To bit_cast(const From& from)noexcept; | (since C++20) | |
Obtain a value of typeTo
by reinterpreting the object representation ofFrom
. Every bit in thevalue representation of the returnedTo
object is equal to the corresponding bit in theobject representation offrom. The values of padding bits in the returnedTo
object are unspecified.
If there is no value of typeTo
corresponding to the value representation produced, the behavior is undefined. If there are multiple such values, which value is produced is unspecified.
A bit in the value representation of the result isindeterminate if it
From
(i.e. it corresponds to a padding bit), orA bit in the value representation of the result iserroneous if it corresponds to a bit for which the smallest enclosing object has anerroneous value. | (since C++26) |
For each bit in the value representation of the result that is indeterminate, the smallest object containing that bit has an indeterminate value; the behavior is undefined unless that object is of anuninitialized-friendly type. The result does not otherwise contain any indeterminate values. | (until C++26) |
For each bitb in the value representation of the result that is indeterminate or erroneous, letu be the smallest object enclosingb:
| (since C++26) |
This overload participates in overload resolution only ifsizeof(To)== sizeof(From) and bothTo
andFrom
areTriviallyCopyable types.
This function template isconstexpr if and only if each ofTo
,From
and the types of all subobjects ofTo
andFrom
:
Contents |
from | - | the source of bits for the return value |
An object of typeTo
whose value representation is as described above.
To implementstd::bit_cast
, ignoring the fact that it'sconstexpr,std::memcpy can be used, when it is needed, to interpret the object representation as one of another type:
template<class To,class From>std::enable_if_t< sizeof(To)== sizeof(From)&&std::is_trivially_copyable_v<From>&&std::is_trivially_copyable_v<To>, To>// constexpr support needs compiler magicbit_cast(const From& src)noexcept{ static_assert(std::is_trivially_constructible_v<To>,"This implementation additionally requires ""destination type to be trivially constructible"); To dst;std::memcpy(&dst,&src, sizeof(To));return dst;}
reinterpret_cast (or equivalentexplicit cast) between pointer or reference types shall not be used to reinterpret object representation in most cases because of thetype aliasing rule.
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_bit_cast | 201806L | (C++20) | std::bit_cast |
#include <bit>#include <cstdint>#include <iostream> constexprdouble f64v=19880124.0;constexprauto u64v= std::bit_cast<std::uint64_t>(f64v);static_assert(std::bit_cast<double>(u64v)== f64v);// round-trip constexprstd::uint64_t u64v2= 0x3fe9000000000000ull;constexprauto f64v2= std::bit_cast<double>(u64v2);static_assert(std::bit_cast<std::uint64_t>(f64v2)== u64v2);// round-trip int main(){std::cout<<"std::bit_cast<std::uint64_t>("<<std::fixed<< f64v<<") == 0x"<<std::hex<< u64v<<'\n'<<"std::bit_cast<double>(0x"<<std::hex<< u64v2<<") == "<<std::fixed<< f64v2<<'\n';}
Possible output:
std::bit_cast<std::uint64_t>(19880124.000000) == 0x4172f58bc0000000std::bit_cast<double>(0x3fe9000000000000) == 0.781250
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 2482 (P1272R4) | C++20 | it was unspecified whether UB would occur when involving indeterminate bits | specified |
implicitly creates objects in given storage with the object representation reused (function template)[edit] |