I get this warning while compiling the program below with g++-4.4.3: $ g++ -O2 -Wall test.cc test.cc: In function ¡®int main()¡¯: test:8: warning: dereferencing pointer ¡®<anonymous>¡¯ does break strict-aliasing rules /usr/include/c++/4.4/bits/stl_list.h:214: note: initialized from here
I don't see anything suspicious in the following program, but there should be a reason for the warning. I asked on comp.lang.c++.moderated: http://groups.google.com/group/comp.lang.c++.moderated/msg/fa12790662f5279d but I couldn't get a satisfactory explanation, and someone suggested this might be a bug of gcc. I have searched for an existing report of this bug and found many similar ones, but the sample codes didn't look very relevant to this problem, or the others compiled fine without the warning. Sorry if this is a duplicate; please feel free to mark this as a duplicate and point me to the existing one in that case. /* 1 */ #include <list> /* 2 */ /* 3 */ struct X /* 4 */ { /* 5 */ struct P /* 6 */ { /* 7 */ const void* p; /* 8 */ bool operator==(const P& o) const { return p == o.p; } /* 9 */ }; /* 10 */ /* 11 */ typedef std::list<P> PL; /* 12 */ /* 13 */ struct I /* 14 */ { /* 15 */ PL::const_iterator itr; /* 16 */ bool operator==(I o) const { return *itr == *o.itr; } /* 17 */ bool operator!=(I o) const { return !(*this == o); } /* 18 */ I& operator++() { ++itr; return *this; } /* 19 */ }; /* 20 */ /* 21 */ PL list; /* 22 */ /* 23 */ I begin() const { I i = {list.begin()}; return i; } /* 24 */ I end() const { I i = {list.end()}; return i; } /* 25 */ }; /* 26 */ /* 27 */ int main() /* 28 */ { /* 29 */ X x; /* 30 */ for (X::I it = x.begin(); it != x.end(); ++it) { } /* 31 */ } Line 214 of /usr/include/c++/4.4/bits/stl_list.h looks like this: /* 182 */ /** /* 183 */ * @brief A list::const_iterator. /* 184 */ * /* 185 */ * All the functions are op overloads. /* 186 */ */ /* 187 */ template<typename _Tp> /* 188 */ struct _List_const_iterator /* 189 */ { /* ... */ /* 210 */ // Must downcast from List_node_base to _List_node to get to /* 211 */ // _M_data. /* 212 */ reference /* 213 */ operator*() const /* 214 */ { return static_cast<_Node*>(_M_node)->_M_data; } /* ... */ /* 260 */ }; $ g++ -v Using built-in specs. Target: i486-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu Thread model: posix gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) $ uname -a Linux localhost 2.6.32-21-generic #32-Ubuntu SMP Fri Apr 16 08:10:02 UTC 2010 i686 GNU/Linux -- Summary: dereferencing pointer ¡®<anonymous>¡¯ does break strict- aliasing rules Product: gcc Version: 4.4.3 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: musiphil at bawi dot org http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43978