The following code (as small as I can get but I'm not sure its minimal)
#include <set>
struct X{
unsigned a;
X(){}
bool operator<(const X& i)const { return a < i.a; }
};
struct R{
std::set<X> getSet() const{
std::set<X> result;
result.insert(X());
return result;
}
};
void bug(){
for(unsigned x = 0; x < 2; ++x){
std::set<X> pts = R().getSet();
}
}
Produces the following warnings:
bug-check2.cpp: In function void bug():
bug-check2.cpp:6: warning: dereferencing pointer __x.22 does break
strict-aliasing rules
/usr/include/c++/4.4/bits/stl_tree.h:525: note: initialized from here
bug-check2.cpp:6: warning: dereferencing pointer __x.22 does break
strict-aliasing rules
/usr/include/c++/4.4/bits/stl_tree.h:525: note: initialized from here
When compiled with:
g++ -Wall -O3 -c bug-check2.cpp
With version 4.4.1 on Ubuntu 9.10 (Sorry for the old version, it would take
hours to recompile g++ and I already have put several into reducing the
test-case.)
I will be attaching the g++ verbose output, the preprocessed source, and the
original source.
Unusual symptoms:
1. Making getSet static or a global function removes the warning.
2. Reducing the number of loop iterations below 2 reduces or removes the
warning.
3. Removing the constructor for X removes one of the warnings.
4. Changing X::operator< to return a constant value removes the warning
5. Changing X::operator< to {return a < b; } where b is a global variable
removes the warning
6. Removing the result.insert(X()) line removes the warnings
7. Making result a global variable removes the warnings.
--
Summary: Strict aliasing warning in stl_tree (returning a copy of
a set from a member function in a loop)
Product: gcc
Version: 4.4.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: eric_moyer at yahoo dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45202