Is there a way in c++ to get a different overload called based on the runtime/compile time constness of an input? My version(12) of MSVC can\'t do this using constexpr. Reading
constexpr can only be applied to variables and functions, but not function parameters (details on cppreference). Furthermore, you cannot overload the function on whether it is computed at compile or run-time, i.e. this is not valid:
constexpr int Flip4(int n) {
return ((n & 0xFF) << 24) | ((n & 0xFF00) << 8) | ((n & 0xFF0000) >> 8) | ((n & 0xFF000000) >> 24);
}
inline int Flip4(int n) {
return _byteswap_uint64(n);
}
One way is to give the functions different names and call them accordingly.
Just as a side note,
A
constexprspecifier used in a function declaration impliesinline.
So you don't need to declare your constexpr function inline
Also, don't forget that constexpr functions are only guaranteed to be evaluated at compile-time if they are used in a context required at compile time. So to force it you would need to write:
constexpr int a = Flip4('abcd');