当比较双打容器时,为什么没有警告 - wfloat -equal?
如果使用GCC或CLANG使用Compiler选项-wfloat-Equal
,则浮点/双值的平等比较会引起警告。但是,当比较容器(例如std :: vector
或std :: tuple
)的float或double值时,没有提出此类警告。
)
#include <tuple>
#include <vector>
#include <assert.h>
int main() {
double d = 1.2;
std::tuple<double, double> t_d{1.2, 3.14};
std::tuple<double, double> t_d_2{1.2, 3.14};
std::vector<double> v_d{1.2, 3.14};
std::vector<double> v_d_2{1.2, 3.14};
// this causes a warning, like "warning: comparing floating-point with '==' or '!=' is unsafe [-Wfloat-equal]":
assert(d == 1.2);
// but why no warning from -Wfloat-equal here?
assert(t_d == t_d_2);
// no warning here either:
assert(v_d == v_d_2);
// all of these cause warnings as expected:
assert(std::get<0>(t_d) == 1.2);
assert(std::get<0>(t_d) == std::get<0>(t_d_2));
assert(v_d[0] == 1.2);
assert(v_d[0] == v_d_2[0]);
return 0;
}
示例代码(也at https://godbolt.org/z/yp8v8hts3 对于这些容器比较?更重要的是,我该怎么做才能真正获取这些警告?
If I use the compiler option -Wfloat-equal
with GCC or Clang, equality comparisons of float/double values cause a warning. However, when comparing containers (like std::vector
or std::tuple
) of float or double values, no such warning is raised.
Example code (also at https://godbolt.org/z/YP8v8hTs3):
#include <tuple>
#include <vector>
#include <assert.h>
int main() {
double d = 1.2;
std::tuple<double, double> t_d{1.2, 3.14};
std::tuple<double, double> t_d_2{1.2, 3.14};
std::vector<double> v_d{1.2, 3.14};
std::vector<double> v_d_2{1.2, 3.14};
// this causes a warning, like "warning: comparing floating-point with '==' or '!=' is unsafe [-Wfloat-equal]":
assert(d == 1.2);
// but why no warning from -Wfloat-equal here?
assert(t_d == t_d_2);
// no warning here either:
assert(v_d == v_d_2);
// all of these cause warnings as expected:
assert(std::get<0>(t_d) == 1.2);
assert(std::get<0>(t_d) == std::get<0>(t_d_2));
assert(v_d[0] == 1.2);
assert(v_d[0] == v_d_2[0]);
return 0;
}
Why are the warnings omitted for these container comparisons? And more importantly, what can I do to actually get these warnings as well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
GCC 默认情况下未报告系统标头的警告。可以通过添加
-Wsystem-Header
编译器标志来获得所需的行为。引用文档:
live demo:“ noreferrer”> https://godbolt.org/z/s6rexszj6
clang 似乎采用了相同的方法,请参见 https://clang.llvm.org/docs/usersmanual.html#controllling-diarostics-diarostics-ingnostics-ingnostics-instem-system-Headers =“ https://clang.llvm.org/docs/usersmanual.html#options-to-control-eror-error-error-and-warning-messages” usermanual.html#options-to-control-error-error-and-warning-messages 。
实时演示: https://godbolt.org/z/n9xy8rcm8
GCC doesn't report warnings for system headers by default. The desired behavior may be obtained by adding
-Wsystem-header
compiler flag.Quotation from the documentation:
Live demo: https://godbolt.org/z/s6rExszj6
Clang seemingly adopted the same approach, see https://clang.llvm.org/docs/UsersManual.html#controlling-diagnostics-in-system-headers and https://clang.llvm.org/docs/UsersManual.html#options-to-control-error-and-warning-messages.
Live demo: https://godbolt.org/z/n9xY8rcM8