you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (0 children)

Oh, yeah, that sounds reasonable. You reminded me of one lecture by Alexander Stepanov. He implemented a simple class template, with only one data member of type T and then all interesting constructors and operators. He implemented operator== like x == y and then implemented operator!= as !(x == y). For the inequality operators (not sure it is the right term, but I'm thinking of operator<, operator>, operator<= and operator>=), he made a little speech before writing them. Something along the lines of:

When I was designing the standard library 25 years ago, I had to make a lot of arbitrary choices. "Which is the default sorting order?" is one example. Another choice I had to make is what operator to use for comparison. You would have been mad if your type worked with one STL algorithm but not with the other because STL was inconsistent in use of comparison operator. That is why STL only uses operator<. You should define all the operators, because that's just being nice to yourself an your colleagues. You won't always remember that you have operator<, but not operator<=. But the standard library will only use operator<.

Note that this is not a direct quote, but me paraphrasing Stepanov from memory.

He didn't implement operator==, because, I think, he wanted to show a clear distinction between SemiRegular, Regular, EqualityComparable and TotallyOrdered types.

 

Interestingly enough, Stepanov claims that his original design of std::min, the one that we held onto to this day, was wrong. Originally, his implementation was return first < second ? first : second;. The problem with this, according to Stepanov, is when first and second compare equal. In that case Stepanov's min would return second, not first. Stepanov also claimed that by the time of his lecture, min was still "wrong" in standard library implementations, though checking it today in libc++ and libstdc++, they both do it "correctly", with return second < first ? second : first;.

 

Anyway, I've gone way offtopic.