简体   繁体   English

在gnu ++ 11标准中,如何在没有警告的情况下编写“嵌套if ... else语句的常量”?

[英]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.

相关问题 为什么将语言标准从-std = gnu ++ 98升级到-std = gnu ++ 11 - Why promote language standard from -std=gnu++98 to -std=gnu++11 C++ std=c++11 和 std=gnu++11 之间的标准正则表达式区别 - C++ standard regex difference between std=c++11 and std=gnu++11 CUDA 8使用-std = gnu ++ 11编译错误 - CUDA 8 compile errors with -std=gnu++11 使用-std = gnu ++ 11开关进行编译时std :: equal错误 - std::equal error while compiling with -std=gnu++11 switch 如何在 IMPINJ Reader R420 的 octane etk 示例嵌入文件的给定 make 文件中添加 -std=c++11 或 -std=GNU++11? - How to add -std=c++11 or -std=GNU++11 in the given make file of octane etk sample embedded file of IMPINJ Reader R420? g ++-警告:扩展的初始化程序列表仅在-std = c ++ 11或-std = gnu ++ 11中可用 - g++ - warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 -std = c ++ 11和-std = gnu ++ 11之间有什么区别? - What are the differences between -std=c++11 and -std=gnu++11? 非静态数据成员初始值设定项仅适用于 -std=c++11 或 -std=gnu++11 - Non-static data member initializers only available with -std=c++11 or -std=gnu++11 警告:扩展初始值设定项列表仅适用于 -std=c++11 或 -std=gnu++11 [默认启用] - warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default] Linux gnu ++ 11,在运行时获得“启用多线程以使用std :: thread:不允许操作” - Linux gnu++11, getting “Enable multithreading to use std::thread: Operation not permitted” on runtime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM