首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用std::map发出警告:取消引用指针‘<anonymous>’确实破坏了严格的混叠规则。

使用std::map发出警告:取消引用指针‘<anonymous>’确实破坏了严格的混叠规则。
EN

Stack Overflow用户
提问于 2013-03-20 10:12:40
回答 1查看 2.4K关注 0票数 2

我已经将一个例子简化为下面所示的代码。在编译时,这提供了以下内容:

代码语言:javascript
复制
$ 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

,这个警告告诉我什么?我能做些什么?

代码语言:javascript
复制
#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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-20 12:08:29

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15520642

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档