2

我已将案例简化为如下所示的代码。编译时,这给出了以下内容:

$ 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); 
}

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

4

1 回答 1

3

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

于 2013-03-20T12:08:29.073 回答