4

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.

3
  • 2
    Drop QACPP in favor of some other tool? Commented Apr 2, 2019 at 21:01
  • Is it a function? What does the signature look like? Commented Apr 2, 2019 at 21:01
  • I slightly modified the question, if you see it now, the above lines are called inside the constructor of a template class T and compared with other template types T1, T2 to assign a particular value for a member variable of the template class T, based on the matching if condition. Commented Apr 2, 2019 at 21:06

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Will it be any different if I tell that the actual code does not have if (std::is_same<T, int>::value) but has if (std::is_same<T1, T2>::value) ? However I the code analyzer shows warning in both cases.
There's no difference. This is because std::is_same<T1, T2>::value is always a compile time evaluable constant expression.
Yea, I guess you're right... so there is nothing that can be done to fix such warnings ? NOTE: I use PRQA Source Code Analyzer v2.3.0.93377 Framework.
@Rookie_Coder2318; Other than changing the warnings or refactoring the code (which I wouldn't do on the grounds that it would be an imperfect tail wagging a reasonably coded dog), no. Static analysers have their place but I don't think the goal is to eliminate all warnings.
2

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) ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.