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.