[Bug libstdc++/71780] std::map::operator[] crashes with std::vector of objects used as key
redi at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Thu Jul 7 00:07:00 GMT 2016
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71780
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution|--- |INVALID
Severity|major |normal
--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
bool Small::operator<(const Small &rhs) const
{
if (_field1 < rhs._field1) return(true);
if (_field2 < rhs._field2) return(true);
if (_field3 < rhs._field3) return(true);
return(false);
}
This is not a valid StrictWeakOrdering, so it's undefined behaviour to put
those objects into a std::map. This is a bug in your code, not GCC.
Consider:
#include <cassert>
class Small {
public:
Small() {}
~Small() {}
bool operator<(const Small &rhs) const;
int _field1;
int _field2;
int _field3;
};
bool Small::operator<(const Small &rhs) const
{
if (_field1 < rhs._field1) return(true);
if (_field2 < rhs._field2) return(true);
if (_field3 < rhs._field3) return(true);
return(false);
}
int
main()
{
Small a, b;
a._field1 = 1;
a._field2 = 2;
a._field3 = 0;
b._field1 = 2;
b._field2 = 1;
b._field3 = 0;
assert( ! ((a < b) && (b < a)) );
}
The assertion fails.
https://www.sgi.com/tech/stl/StrictWeakOrdering.html
More information about the Gcc-bugs
mailing list