当我试图编译下面的代码时,我得到了一个非常奇怪的g++警告:
#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)发布于 2009-12-27 06:08:53
试着改变这一点:
return (x < a.x || (!(a.x < x) && y < a.y));进入:
return (x < a.x || (a.x == x && y < a.y));我也是在g++ 3.4.2下使用你的版本和我的版本编译的,一切正常。
发布于 2009-12-27 06:05:55
哪个版本的g++?g++ 4.3.2对此进行了编译,没有任何问题。
https://stackoverflow.com/questions/1964463
复制相似问题