[Bug libstdc++/60519] Debug mode should check comparators for irreflexivity

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Sun Apr 12 20:59:00 GMT 2015


--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to François Dumont from comment #2)
> So yes it doubles the number of comparisons which is definitely a

Well actually your patch doesn't double the number, because you only do the
reverse check when the first check returns true.

> performance hint but your patch on the other hand expect to detect an
> implementation issue on only 1 use case so it can miss many kind of wrong
> implementation on special instances.

The most common mistake I want this to detect is simply trying to define
something equivalent to operator<= instead of operator< (see PR 59391 that I
linked to) and that will be detected for every value, so you only need to check
the first one. i.e. I want to check for irreflexivity.

Your patch checks for antisymmetry instead, which is also required for a strict
weak order, but is a different property.

Checking for antisymmetry is necessary to check broken orders like this:

bool cmp(const pair<int,int>& l, const pair<int,int>& r)
{ return l.first < r.first || l.second < r.second; }

Your patch also only works for C++11, but there's no reason this shouldn't work
for C++03 too. Which algorithms benefit from handling heterogeneous types in
these checks?

More information about the Gcc-bugs mailing list