[英]How to write “nested if…else statement for constants” without warnings, in gnu++11 standard?
When I use nested if....else statement with 当我使用嵌套的if .... else语句时
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." 我得到一个QACPP静态代码分析器的编译器警告qacpp-4.2.1-4090,并带有消息“此条件'if'语句是常量。” How do I fix this compiler warning in gnu++11 standards ?
如何在gnu ++ 11标准中修复此编译器警告?
NOTE: I'm not an expert at C++ so please excuse me if the question sounds amateur. 注意:我不是C ++的专家,所以如果问题听起来很业余,请原谅。
For a particular instantiation of T
, the if
conditionals are constant. 对于
T
的特定实例化, if
条件是常数。 In other words std::is_same<T, int>::value
&c. 换句话说,
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. 似乎静态分析仪对我过度热心。
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) ...
. 这不是一个好的,有效的诊断,除非
static_condition
显然是无条件的,例如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) ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.