加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 如何解释g警告

发布时间:2020-12-16 09:25:42 所属栏目:百科 来源:网络整理
导读:在尝试编译以下代码时,我收到了一个非常奇怪的警告: #include map#include setclass A {public: int x; int y; A(): x(0),y(0) {} A(int xx,int yy): x(xx),y(yy) {} bool operator (const A a) const { return (x a.x || (!(a.x x) y a.y)); }};struct B {
在尝试编译以下代码时,我收到了一个非常奇怪的警告:

#include <map>
#include <set>

class A {
public:

    int x;
    int y;

    A(): x(0),y(0) {}
    A(int xx,int yy): x(xx),y(yy) {}

    bool operator< (const A &a) const {
        return (x < a.x || (!(a.x < x) && y < a.y));
    }
};

struct B {
    std::set<A> data;
};

int
main()
{
    std::map<int,B> m;

    B b;

    b.data.insert(A(1,1));
    b.data.insert(A(1,2));
    b.data.insert(A(2,1));

    m[1] = b;

    return 0;
}

输出:

$g++ -Wall -W -O3 t.cpp -o /tmp/t
t.cpp: In function ‘int main()’:
t.cpp:14: warning: dereferencing pointer ‘__x.52’ does break strict-aliasing rules
t.cpp:14: warning: dereferencing pointer ‘__x.52’ does break strict-aliasing rules
/usr/lib/gcc/i686-redhat-linux/4.4.2/../../../../include/c++/4.4.2/bits/stl_tree.h:525: note: initialized from here

对我来说根本没有任何意义.我该如何解读?我没有看到发布的代码有什么问题.

忘记指定编译器详细信息:

$gcc --version
gcc (GCC) 4.4.2 20091027 (Red Hat 4.4.2-7)

解决方法

gcc 4.4有一个错误,其中std :: map中断错误警告严格别名规则.

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39390

您的代码有效C.严格别名仅允许使用-O3时默认启用的优化子集.

您的解决方案是使用-fno-strict-aliasing或不同版本的gcc进行编译.

如果你对什么是严格混叠感到好奇,that has been asked here.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读