views:

275

answers:

3

Hello

I've got a very strange g++ warning when tried to compile following code:

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

Output:

$ 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

It doesn't have any sence to me at all. How should I interpret it ? I don't see what's wrong with the code posted.

Forget to specify compiler details:

$ gcc --version
gcc (GCC) 4.4.2 20091027 (Red Hat 4.4.2-7)
A: 

Which version of g++? g++ 4.3.2 compiles this without complaint.

Richard Pennington
Silly me, I see: 4.4.2.
Richard Pennington
+1  A: 

Try to change this:

return (x < a.x || (!(a.x < x) && y < a.y));

into:

return (x < a.x || (a.x == x && y < a.y));

I also compiled this using your version and my version under g++ 3.4.2 and is all ok.

Dr.Optix
+5  A: 

gcc 4.4 has a bug where std::map breaks incorrectly warns about strict-aliasing rules.

http://gcc.gnu.org/bugzilla/show%5Fbug.cgi?id=39390

Your code is valid C++. Strict aliasing merely allows a subset of optimizations that are enabled by default when using -O3.

Your solution is to compile with -fno-strict-aliasing or a different version of gcc.

If you're curious about what strict aliasing is, that has been asked here.

Shmoopty