Re: Strict weak ordering
On 2007-10-08 11:24:52 -1000, bb <muralibala68@gmail.com> said:
I think the condition should be:
(this->m1_ < rhs.m1_) || ((this->m1_ == rhs.m1_) && (this->m2_ <
rhs.m2_))
I do agree. However, if there are going to be more member variables,
then operator< is going to become unreadable. I guess there is a
simpler way?
If the comparison involves all the members, then you have to write code
that looks at all the members. It's only unreadable if you write it
that way. <g>
Having said that, if you really do have complex objects that require a
total order, look at tr1's tuple. It does comparisons for you. (It's
also in std for the next standard, but your comiler probably doesn't do
that yet)
#include <tuple>
typedef std::tr1::tuple<int, int> my_type;
my_type obj1(2, 3);
my_type obj2(3, 2);
Now, obj1 < obj2 is well-defined, and obj2 < obj1 is also well-defined.
And that operator< defines a total ordering.
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)