Title
Undefined behavior in `std::bit_cast` should be diagnosed during constant evaluation
Status
new
Section
[bit.cast]
Submitter
Jan Schultke

Created on 2026-03-10.00:00:00 last changed 2 weeks ago

Messages

Date: 2026-03-13.11:42:37

Proposed resolution:

This wording is relative to N5032.

  1. Modify [bit.cast] as indicated:

    template<class To, class From>
      constexpr To bit_cast(const From& from) noexcept;
    

    -1- Constraints: […]

    -2- Mandates: Neither `To` nor `From` are consteval-only types ([basic.types.general]).

    -3- Constant When: `To`, `From`, and the types of all subobjects of `To` and `From` are types `T` such that: […]

    -4- Returns: An object of type `To`. Implicitly creates objects nested within the result ([intro.object]). Each bit of the value representation of the result is equal to the corresponding bit in the object representation of from. Padding bits of the result are unspecified. 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. If there are multiple such values, which value is produced is unspecified. 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]). 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. 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:

    […]

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

    -?- Remarks: A function call expression that has undefined or erroneous behavior as described above is not a core constant expression.

Date: 2026-03-10.00:00:00

Consider the following example with undefined behavior, which assumes that `sizeof(int)` equals `4`:

struct alignas(4) E { };
constexpr auto x = std::bit_cast<int>(E{});

The example compiles with MSVC without any error or warning, where `x` has the value `0`, which is valid if the UB is library UB; other compilers reject it.

[bit.cast] paragraph 4, bullet 2 explicitly states:

Otherwise, if b is indeterminate, the behavior is undefined.

The fact that undefined behavior is called out explicitly within library wording suggests that this is "library UB", not "core UB" that would prevent the initializer from being a constant expression because it violates the requirement of [expr.const] paragraph 21 bullet 2.3:

no constituent value of scalar type is an indeterminate or erroneous value ([basic.indet]),

Similarly, [bit.cast] paragraph 4 states that forming a value representation that is not valid for the type is undefined, which may also be seen as "library UB". This can occur in bit_cast<bool>(char{2}) if `2` is not a valid representation of `bool`.

During the 2026-03-10 LEWG telecon discussing P3969R0, LEWG polled whether this UB should be diagnosed.

POLL: The undefined behavior in `bit_cast` should be diagnosable during constexpr.
Attendance: 19
Authors' opinion: N/A
Outcome: No objection to unanimous consent.

History
Date User Action Args
2026-03-13 11:42:37adminsetmessages: + msg16021
2026-03-10 00:00:00admincreate