This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of New status.

4539. Undefined behavior in std::bit_cast should be diagnosed during constant evaluation

Section: 22.11.3 [bit.cast] Status: New Submitter: Jan Schultke Opened: 2026-03-10 Last modified: 2026-03-13

Priority: Not Prioritized

View all issues with New status.

Discussion:

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.

22.11.3 [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 7.7 [expr.const] paragraph 21 bullet 2.3:

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

Similarly, 22.11.3 [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.

Proposed resolution:

This wording is relative to N5032.

  1. Modify 22.11.3 [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 (6.9.1 [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 (6.8.2 [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 (6.8.5 [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.