Why can't I std::bit_cast the contents of a string literal?

Viewed 238

While experimenting with compile-time string manipulation, I've encountered a strange phenomenon:

#include <bit>

constexpr const char str[4] = "abc";

// error: constexpr variable 'x' must be initialized by a constant expression
constexpr auto x = std::bit_cast<int>("xyz");
// OK
constexpr auto y = std::bit_cast<int>(str);

See Compiler Explorer.

How come bit-casting is permitted for a char[] which is stored in a global variable, but not for string literals? std::bit_cast does not modify its source and the result is a value, so there is no potential of modifying the storage of string literals.

0 Answers
Related