I was trying to write a log function in C++ that is evaluated at compile time. I would like for the code to compile and give an answer only when the input is a power of 2. To accomplish I need some way to perform checks within a constexpr function during compile time.
I would like the following behavior:
constexpr int a = logBase2(16); // should equal 4
constexpr int b = logBase2(42); // should not compile, input is not a power of 2
constexpr int c = logBase2(-1); // should also not compile
I tried the following code:
constexpr bool isPowerOf2(int x) {
return (x > 0) && ((x & (x - 1)) == 0);
}
constexpr int logBase2(int x) {
static_assert(isPowerOf2(x), "Input must be a power of 2");
int result = 0;
while ((x & 1) == 0) {
x >>= 1;
result++;
}
return result;
}
Of course this doesn’t compile, because the parameter x is not guaranteed to be constexpr: constexpr just means that the function can be evaluated at compile time.
I would need something like this:
constexpr bool isPowerOf2(int x) {
return (x > 0) && ((x & (x - 1)) == 0);
}
constexpr int logBase2(constexpr int x) { // x must be able to be evaluated at compile time
static_assert(isPowerOf2(x), "Input must be a power of 2");
int result = 0;
while ((x & 1) == 0) {
x >>= 1;
result++;
}
return result;
}
But this isn’t valid C++. Is there any way to accomplish this or something analogous?