Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Work around disregarded constexpr in Visual Studio?

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 for std::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 declared constexpr
note: see usage of log2

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?

like image 384
Jonathan Mee Avatar asked May 11 '26 07:05

Jonathan Mee


1 Answers

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;
}

Demo

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.

like image 89
François Andrieux Avatar answered May 13 '26 22:05

François Andrieux



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!