This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug libstdc++/71780] std::map::operator[] crashes with std::vector of objects used as key
- From: "redi at gcc dot gnu.org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Thu, 07 Jul 2016 00:07:21 +0000
- Subject: [Bug libstdc++/71780] std::map::operator[] crashes with std::vector of objects used as key
- Auto-submitted: auto-generated
- References: <bug-71780-4@http.gcc.gnu.org/bugzilla/>
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