Reputation: 91
When I use nested if....else statement with
if (std::is_same<T, T1>::value)
{
// do something
}
else if (std::is_same<T, T2>::value)
{
// do something else
}
.
.
.
else
{
// print error
}
I get a QACPP static code analyzer's compiler warning qacpp-4.2.1-4090 with message "The condition in this 'if' statement is constant." How do I fix this compiler warning in gnu++11 standards ?
NOTE: I'm not an expert at C++ so please excuse me if the question sounds amateur.
Upvotes: 4
Views: 218
Reputation: 58500
The coding tool is literally asking you to abandon good practices like:
if (static_condition) {
// code that is effectively compiled out when static_condition is false
}
and instead use something like:
#if static_condition
// code that is compiled out when static_condition is false
#endif
which can't even be done in the exact situation you have there, and is an inferior practice.
This is not a good, productive diagnostic, except in cases when static_condition
is clearly unconditional, like if (false) ...
.
If the diagnostic tool is otherwise valuable, find out how to suppress that diagnostic in specific files or lines of code. Maybe it supports some directive that can be put into comments to squelch some diagnostics.
// @foo-bar tool: disable-warning(13125)
if (static_condition) ...
Upvotes: 2
Reputation: 234635
For a particular instantiation of T
, the if
conditionals are constant. In other words std::is_same<T, int>::value
&c. are constant expressions.
But that may well be a necessity if you've set the code out in this perfectly legitimate way. (You might be able to refactor to a static polymorphism technique using template specialisation if obviating the static analyser warning is important.)
Seems that the static analyser is being over-zealous to me.
Upvotes: 3