4 ms·
That’s super cool; c++ is always the sharpest tool in the drawer (and by virtue the funnest!).. It’s too bad you still can’t cast a char to a uint8_t though in
by psyclobe 1y ago
That’s super cool; c++ is always the sharpest tool in the drawer (and by virtue the funnest!)..
It’s too bad you still can’t cast a char to a uint8_t though in a constexpr expression.
- vinkelhake 1y ago> It’s too bad you still can’t cast a char to a uint8_t though in a constexpr expression. Uh, what? That has worked fine since the introduction of constexpr in C++11.
- TuxSH 1y agoMaybe they meant reinterpret_cast from/to char to u8, which in this case isn't possible in constexpr.
- epcoa 1y agoWhy would you need to do that though if you can static_cast?
- TuxSH 1y agoYou can't static_cast in this case; https://godbolt.org/z/a1bMbPcaj https://godbolt.org/z/a1bMbPcaj
- lbhdc 1y agoYou can use `std::bit_cast` to do that in constexpr contexts. constexpr auto f(uint8_t *x) { return std::bit_cast<char *>(x); } https://godbolt.org/z/K3f9b9GGs https://godbolt.org/z/K3f9b9GGs
- psyclobe 1y agoAh this was my case! Was trying to constexpr a uint8_t ptr to char * in a constexpr constructor for a string class. Ah that’s what bitcast is for, neat!
- TuxSH 1y agoNo, you can't do that either: https://godbolt.org/z/vzdTMazx7 https://godbolt.org/z/vzdTMazx7 : error: '__builtin_bit_cast' is not a constant expression because 'char' is a pointer type Here 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 https://en.cppreference.com/w/cpp/numeric/bit_cast
- lbhdc 1y agoGood catch. Its weird that it compiles without error as a consteval func.
- TuxSH 1y agoHmm, looking at cppreference: 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.
- lbhdc 1y agoThis compiles in clang 21.1.2 with c++26. #include <cstdint> #include <print> constexpr uint8_t f(char ch) { return static_cast<uint8_t>(ch); } int main() { constexpr uint8_t r = f('a'); std::print("{}", r); }
- psyclobe 1y agoTry consteval , constexpr is a hint. No static casts allowed in constexpr scopes that evaluate as constexpr in c++17 and above (works in c11). Could be wrong I am no expert…
- pjmlp 1y agoIt works perfectly fine with consteval, https://godbolt.org/z/sjGcsh3KP https://godbolt.org/z/sjGcsh3KP What is possible in constexpr contexts has been improving in each revision, the end goal is to support the whole language, eventually. Naturally introducing everything at once would be too hard in a language with such a big ecosystem like C++.
- deleted 1y ago[deleted]
- psyclobe 1y agoGood to know thanks for the correction