P3969R0 — Fixing std::bit_cast of types with padding bits
(9 items)
LEWG
This paper addresses the undefined behavior that arises when using std::bit_cast to convert types with padding bits (e.g., x87 long double) to types without padding bits (e.g., __int128), where the current specification makes such specializations equivalent to std::unreachable regardless of the argument. Two solutions are presented: a two-function solution that makes the degenerate form of std::bit_cast ill-formed and adds a new std::bit_cast_zero_padding function that treats source padding bits as zero, or a single-function solution that changes std::bit_cast itself to treat padding bits as zero, applied as a DR against C++20.
-
Section 1 (Introduction), first note block, code sample — Code constructs std::array
directly from a long double value without calling std::bit_cast; the accompanying comment describes bit_cast behavior, so the call is missing. [1] -
Section 3 (Implementation experience), note block — Claims std::has_unique_object_representations_v
is true on IEEE 754 implementations; it is false because +0.0 and -0.0 have distinct representations but compare equal. The "false positive" argument reverses the actual trait value. [2] - Section 1 (Introduction), note block — "padding paddings" is a corrupted duplication; should be "padding bits". [3]
- Section 2.3.4, code sample, comment on line 3 — "there are not padding bits" is ungrammatical; should be "there are no padding bits". [4]
- Section 2.3 (Can't you clear padding bits before bit-casting?) — "aproach" is misspelled; should be "approach". [5]
-
HTML
element — "typeswith" is missing a space; should be "types with". [6]
References — Anthropic Citations API
[1]
"Fixing std::bit_cast of typeswith padding bits "
"
[2]
"If those padding paddings in `From` are all mapped onto `std::byte` or `unsigned char` objects within `To`, the behavior is well-defined."
"If those padding paddings in `From` are all mapped onto `std::byte` or `unsigned char` objects within `To`, the behavior is well-defined."
[3]
"auto bytes = std::array(0.0L);"
"auto bytes = std::array
[4]
"`std::has_unique_object_representations_v` is `true` despite `float` not having padding bits. These false positives make it not suitable for detecting the presence of padding bits."
"`std::has_unique_object_representations_v
[6]
"// and there are not padding bits in a byte array."
"// and there are not padding bits in a byte array."
Summary: P3969R0 proposes changes to std::bit_cast so that bit-casting types with padding bits produces unspecified (not undefined) values in the padding positions, enabling safe round-tripping of object representations through byte arrays. Six items were found spanning a missing function call in a code example, an incorrect trait claim, textual corruptions, and surface errors.
Pipeline: Discovery (Anthropic Opus + Citations API) → Verification Gate (OpenRouter Opus) → Report Writer (OpenRouter Opus)
Provenance: All references are machine-verified character positions from the Anthropic Citations API — deterministic, exact substrings, not model-generated quotes.
Provenance: All references are machine-verified character positions from the Anthropic Citations API — deterministic, exact substrings, not model-generated quotes.