Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::bit_cast

      From cppreference.com
      <cpp‎ |numeric
       
       
      Utilities library
       
       
      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

      • does not correspond to a bit in the value representation ofFrom (i.e. it corresponds to a padding bit), or
      • corresponds to a bitof an object that(until C++26)for which the smallest enclosing object(since C++26) is not within itslifetime, or
      • has anindeterminate value.

      A 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)


      The result does not otherwise contain any indeterminate or erroneous values.

      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:

      • Ifu is ofuninitialized-friendly type,u has an indeterminate value if any of the bits in its value representation are indeterminate, or otherwise has an erroneous value.
      • Otherwise, ifb is indeterminate, the behavior is undefined.
      • Otherwise, the behavior iserroneous, and the result is as specified above.
      (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:

      • is not a union type;
      • is not a pointer type;
      • is not a pointer to member type;
      • is not a volatile-qualified type; and
      • has no non-static data member of reference type.

      Contents

      [edit]Parameters

      from - the source of bits for the return value

      [edit]Return value

      An object of typeTo whose value representation is as described above.

      [edit]Possible implementation

      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;}

      [edit] Notes

      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 macroValueStdFeature
      __cpp_lib_bit_cast201806L(C++20)std::bit_cast

      [edit] Example

      Run this code
      #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

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      CWG 2482
      (P1272R4)
      C++20it was unspecified whether UB would occur when involving indeterminate bitsspecified

      [edit]See also

      implicitly creates objects in given storage with the object representation reused
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/numeric/bit_cast&oldid=178980"

      [8]ページ先頭

      ©2009-2025 Movatter.jp