Files
2025-10-25 03:02:53 +03:00

115 lines
3.3 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[bit.cast]
# 22 General utilities library [[utilities]](./#utilities)
## 22.11 Bit manipulation [[bit]](bit#cast)
### 22.11.3 Function template bit_cast [bit.cast]
[🔗](#lib:bit_cast)
`template<class To, class From>
constexpr To bit_cast(const From& from) noexcept;
`
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L15691)
*Constraints*:
- [(1.1)](#1.1)
sizeof(To) == sizeof(From) is true;
- [(1.2)](#1.2)
is_trivially_copyable_v<To> is true; and
- [(1.3)](#1.3)
is_trivially_copyable_v<From> is true[.](#1.sentence-1)
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L15699)
*Mandates*: Neither To nor From are consteval-only types ([[expr.const]](expr.const "7.7Constant expressions"))[.](#2.sentence-1)
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L15703)
*Constant When*: To, From, and the types of all subobjects
of To and From are types T such that:
- [(3.1)](#3.1)
is_union_v<T> is false;
- [(3.2)](#3.2)
is_pointer_v<T> is false;
- [(3.3)](#3.3)
is_member_pointer_v<T> is false;
- [(3.4)](#3.4)
is_volatile_v<T> is false; and
- [(3.5)](#3.5)
T has no non-static data members of reference type[.](#3.sentence-1)
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L15715)
*Returns*: An object of type To[.](#4.sentence-1)
Implicitly creates objects nested within the result ([[intro.object]](intro.object "6.8.2Object model"))[.](#4.sentence-2)
Each bit of the value representation of the result
is equal to the corresponding bit in the object representation
of from[.](#4.sentence-3)
Padding bits of the result are unspecified[.](#4.sentence-4)
For the result and each object created within it,
if there is no value of the object's type corresponding to the
value representation produced, the behavior is undefined[.](#4.sentence-5)
If there are multiple such values, which value is produced is unspecified[.](#4.sentence-6)
A bit in the value representation of the result is indeterminate if
it does not correspond to a bit in the value representation of from or
corresponds to a bit
for which the smallest enclosing object is not within its lifetime or
has an indeterminate value ([[basic.indet]](basic.indet "6.8.5Indeterminate and erroneous values"))[.](#4.sentence-7)
A bit in the value representation of the result is erroneous
if it corresponds to a bit
for which the smallest enclosing object has an erroneous value[.](#4.sentence-8)
For each bit b in the value representation of the result
that is indeterminate or erroneous,
let u be the smallest object containing that bit enclosing b:
- [(4.1)](#4.1)
If u is of unsigned ordinary character type or std::byte type,u has an indeterminate value
if any of the bits in its value representation are indeterminate, or
otherwise has an erroneous value[.](#4.1.sentence-1)
- [(4.2)](#4.2)
Otherwise, if b is indeterminate, the behavior is undefined[.](#4.2.sentence-1)
- [(4.3)](#4.3)
Otherwise, the behavior is erroneous, and the result is as specified above[.](#4.3.sentence-1)
The result does not otherwise contain any indeterminate or erroneous values[.](#4.sentence-10)