使用std::map给出警告:取消引用指针' '确实违反了严格的别名规则

时间:2021-07-10 02:14:41

I've reduced a case to the code shown below here. When compiling, this gives the following:

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

$ 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

What is this warning telling me ? What can I do about it ?

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

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

Note, this is on RHEL 6.3, which comes with gcc 4.4.6

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

1 个解决方案

#1


3  

This is a known (and fixed) compiler bug, see this and this. You should update your toolchain.

这是一个已知的(和固定的)编译错误,看看这个和这个。你应该更新你的工具链。

#1


3  

This is a known (and fixed) compiler bug, see this and this. You should update your toolchain.

这是一个已知的(和固定的)编译错误,看看这个和这个。你应该更新你的工具链。