constexpr auto f(uint8_t *x) {
return std::bit_cast<char *>(x);
}
https://godbolt.org/z/K3f9b9GGs constexpr auto f(uint8_t *x) {
return std::bit_cast<char *>(x);
}
https://godbolt.org/z/K3f9b9GGsHere the `constexpr` keyword means the function might be called in a constant-evaluated context. f doesn't need to have all its statements be able to be evaluated in constexpr, only those which are actually used are. You need to explicitly instantiate a constexpr variable to test this.
cppreference is very clear* about this, regarding bit_cast: https://en.cppreference.com/w/cpp/numeric/bit_cast
The consteval specifier declares a function or function template to be an immediate function, that is, every potentially-evaluated call to the function must (directly or indirectly) produce a compile time constant expression.
It's possible that the compiler just doesn't bother as long as you aren't actually calling the function.
Ah that’s what bitcast is for, neat!