|
|
|
|
|
by MaulingMonkey
1521 days ago
|
|
`constexpr` isn't "compile time". It's potentially compile time at best. Debug builds in particular will go out of their way to evaluate things at runtime, presumably so you can set breakpoints and step through code, even when it's completely pointless. I have seen the following function show up in a profiler: template < bool value >
bool constexpr IsEnabled() { return value; }
This was used to silence warnings about unconditional branches in a macro. There are ways to force such functions to be evaluated at compile time, but they're pretty awkward, and limited to integral types (you can't use them on string_view s): #define EVAL_AT_COMPILE_TIME(x) std::integral_constant<decltype(x), x>::value
const bool x = EVAL_AT_COMPILE_TIME(IsEnabled<true>());
|
|
C++20 has “consteval“: https://en.cppreference.com/w/cpp/language/consteval