|
Boost : |
From: Howard Hinnant (howard.hinnant_at_[hidden])
Date: 2007-02-06 14:06:15
On Feb 2, 2007, at 10:36 AM, Thorsten Ottosen wrote:
> David Abrahams wrote:
>> Thorsten Ottosen <thorsten.ottosen_at_[hidden]> writes:
>>
>>
>>> Hi,
>>>
>>> I couldn't find a way to do this. Maybe I have overlooked something.
>>> Anyway, making sure that operator<, operator== does the right
>>> thing is
>>> pretty important.
>>
>>
>> Strict weak ordering uses a single operations and does not relate two
>> operations. Thus you can test that < imposes a strict weak ordering,
>> but it can have nothing to do with ==. What you're testing below
>> would need some other name.
>
> Right. I guess both could be useful.
One approach is to use a debugging compare predicate which adapts
another predicate and adds a test. Something like:
template <class Compare>
struct debug_comp
{
Compare comp_;
debug_comp() {}
explicit debug_comp(const Compare& c) : comp_(c) {}
template <class Tp, class Up>
bool operator()(const Tp& x, const Up& y)
{
bool r = comp_(x, y);
if (r)
assert(!comp_(y, x));
return r;
}
};
Demo:
std::sort(v.begin(), v.end(), debug_comp<std::less<A> >());
It isn't an exhaustive test, but does tend to catch such bugs in the
wild. A disadvantage of this approach is that it may upset some
algorithms which only require comp(x,y) but not comp(y,x) to be valid.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk