当比较双打容器时,为什么没有警告 - wfloat -equal?

发布于 2025-01-25 03:40:45 字数 1233 浏览 2 评论 0原文

如果使用GCC或CLANG使用Compiler选项-wfloat-Equal,则浮点/双值的平等比较会引起警告。但是,当比较容器(例如std :: vectorstd :: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舂唻埖巳落 2025-02-01 03:40:45

GCC 默认情况下未报告系统标头的警告。可以通过添加-Wsystem-Header编译器标志来获得所需的行为。

引用文档

-wsystem-Headers

在系统标头文件中发现的构造中打印警告消息。 通常会抑制系统标头的警告,假设它们通常不会表明实际问题,并且只会使编译器输出更难阅读。使用此命令行选项告诉GCC从系统标头发出警告,好像它们发生在用户代码中...

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:

-Wsystem-headers

Print warning messages for constructs found in system header files. Warnings from system headers are normally suppressed, on the assumption that they usually do not indicate real problems and would only make the compiler output harder to read. Using this command-line option tells GCC to emit warnings from system headers as if they occurred in user code...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文