Skip to content

[clang] std::bit_cast of a string literal is not a constant expression #63686

@eisenwave

Description

@eisenwave

The following code should succeed, but fails to compile:

#include <bit>

// error: constexpr variable 'x' must be initialized by a constant expression
constexpr auto x = std::bit_cast<int>("xyz");
error: constexpr variable 'x' must be initialized by a constant expression
constexpr auto x = std::bit_cast<int>("xyz");
               ^   ~~~~~~~~~~~~~~~~~~~~~~~~~
note: subexpression not valid in a constant expression
      return __builtin_bit_cast(_To, __from);
             ^
note: in call to 'bit_cast("xyz")'
constexpr auto x = std::bit_cast<int>("xyz");
                   ^
1 error generated.

Putting the string literal into a char[4] first solves the problem, but it should work either way.

See Compiler Explorer

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"constevalC++20 consteval

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions