使用std :: map给出警告:解除引用指针'<anonymous>'确实打破了严格别名规则</anonymous>

时间:2013-03-20 10:12:40

标签: c++ linux

我已将案例减少到下面显示的代码。 在编译时,这给出了以下内容:

$ g++  -std=c++0x -O2 -Wall t.cpp
t.cpp: In function ‘int main()’:
t.cpp:20: warning: dereferencing pointer ‘<anonymous>’ does break strict-aliasing rules
t.cpp:19: warning: dereferencing pointer ‘<anonymous>’ does break strict-aliasing rules
/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_tree.h:175: note: initialized from here

这个警告告诉我什么?我该怎么办?

#include <stdio.h>
#include <stdint.h>
struct TimeKey {

    uint64_t time_stamp;
    uint64_t msg_no;

    TimeKey(uint64_t tstamp, uint64_t no) :
        time_stamp(tstamp),
        msg_no(no)
    {}  

    bool operator < (const TimeKey &other) const 
    {   
        if (time_stamp == other.time_stamp)  //line 19
            return msg_no < other.msg_no;    //line 20
        else
            return time_stamp < other.time_stamp;
    }   
};

template <typename T>
class TimeBuffer {
public:
    uint64_t counter;
    std::map<TimeKey, T> messages;
    void AddMsg(uint64_t tstamp, T val) {
        messages[TimeKey(tstamp, counter++)] = val;
     }   
};  

int main(void)
{
    TimeBuffer<int> messages;
    messages.AddMsg(123456, 1); 
}

请注意,这是在RHEL 6.3上,它附带了gcc 4.4.6

1 个答案:

答案 0 :(得分:3)

这是一个已知(和修复)的编译器错误,请参阅thisthis。你应该更新你的工具链。