Saturday, 17 August 2013

Why will std::sort crash if the comparison function is not as operator

Why will std::sort crash if the comparison function is not as operator

#include <algorithm>
struct A
{
A()
: a()
{}
bool operator <(const A& other) const
{
return a <= other.a;
}
int a;
};
int main()
{
A coll[8];
std::sort(&coll[0], &coll[8]); // Crash!!!
}
If I change return a <= other.a; to return a < other.a; then the program
runs as expected without any exception.
Why?

No comments:

Post a Comment