Reputation: 12517
I tried to use a constexpr bool
instead of a repeating a pre-processor flag in the following code (simplified):
int main()
{
constexpr bool b
#if SOME_FLAG
=true;
#else
=false;
#endif
bool a=foo(1);
if (b && a) bar(2);
}
https://godbolt.org/z/ear8qYxYY
The problem is the line if (b && a)
where Visual Studio 2019 with warnings enabled (and preprocessor flag not defined) say:
warning C4127: conditional expression is constant note: consider using 'if constexpr' statement instead
Gcc and Clang seems to accept it.
I know that I can avoid the warning by using if (a && b)
instead (without generating worse code), but it looks a bit odd.
I also understand that I could write if constexpr(a) if (b)
, but it would look even weirder and risk problems, and similarly for ignoring or silencing the warning.
Is there a better way to avoid the warning for Visual Studio?
Upvotes: 1
Views: 304
Reputation:
Why not
int main()
{
bool a=foo(1);
#if SOME_FLAG
if (a) bar(2);
#endif
}
?
You can also just disable the warning with a #pragma. https://learn.microsoft.com/en-us/cpp/preprocessor/warning?view=msvc-170
Upvotes: 2