I have this code:
constexpr int log2(const unsigned int x) {
return x < 4 ? 1 : 1 + log2(x / 2);
}
int main() {
bitset<log2(2)> foo;
int bar[log2(8)];
cout << log2(8) << endl;
}
This works fine in gcc: https://ideone.com/KooxoS
But when I try on visual-studio-2017 I get these errors:
error C2975:
_Bits
: invalid template argument forstd::bitset
, expected compile-time constant expression
note: see declaration of_Bits
error C2131: expression did not evaluate to a constant
note: failure was caused by call of undefined function or one not declaredconstexpr
note: see usage oflog2
Obviously log2
is constexpr
so I assume this is just a bug in visual-studio-2017. Is there a way I can work around this bug?
It looks like your project includes the standard std::log2
function which the compiler confuses with the your log2
function. This can happen even if you don't #include <cmath>
because standard headers are allowed to include any other standard headers. This is also another example of using namespace std;
backfiring.
One solution is to rename your constexpr
function to something else :
#include <bitset>
#include <iostream>
using namespace std;
constexpr int logTwo(const unsigned int x) {
return x < 4 ? 1 : 1 + logTwo(x / 2);
}
int main() {
bitset<logTwo(2)> foo;
int bar[logTwo(8)];
cout << logTwo(8) << endl;
}
Edit : It seems that using namespace std;
may be unrelated in this case. The standard log2
function may be available at the global namespace anyway.