Boost logo

Boost :

From: Martin Bonner (martin.bonner_at_[hidden])
Date: 2006-07-11 08:15:21


----Original Message----
From: boost-bounces_at_[hidden]
[mailto:boost-bounces_at_[hidden]] On Behalf Of Peter Dimov Sent:
11 July 2006 12:21 To: boost_at_[hidden]
Subject: Re: [boost] Please don't define a fake "operator<"justfor
orderedcontainers

> Martin Bonner wrote:
>> ----Original Message----
>> From: boost-bounces_at_[hidden]
>> [mailto:boost-bounces_at_[hidden]] On Behalf Of Peter Dimov
>>
>>> Daryle Walker wrote:
>>>
>>>> [1] If you make a fake order for "std::complex<>", would you
>>>> compare real components then imaginary components, or would you use
>>>> magnitude then angle?
>>>
>>> Real, then imaginary. Compare with:
>>
>> Interesting. I would have chosen magnitude and then angle. Why
>> components.
>
> There are two reasons for that. First, composite types use
> lexicographical ordering by default, and std::complex is
> (de facto) a (real, imag) pair.
> Second, the magnitude/angle ordering has the property that if you
> have three numbers a, b, c, where a and b are very close to one
> another but not to c, it is possible to have a < c and c < b.

That is true for your comparison too. Consider a=-1, b=+1, c=10000i.

>>> "If you make a FAKE order for std::string, would you compare left to
>>> right or right to left?"
>>
>> I don't think that is a legitimate comparison. In my world view,
>> strings have a natural order but complex numbers don't.
>
> Possibly. So you define "fake" as "unnatural", and define "natural" as
> "feels natural to me".
> This approach can work but it's a bit subjective, isn't it?
Yes.

> Consider the progression:
>
> complex<double>
> struct { double x, double y; }
> pair<double, double>
> tuple<double, double>
> vector<double>
> vector<char>
> string
>
> If we take the "feel" of operator< out of the equation, where should
> we draw the line, and based on what?

complex<double> should not have an operator <() because it models a
mathematical concept (complex number) for which "less than" does not
make sense.

struct, pair, and tuple should not have an operator < because they are
essentially unordered collections of data (I know that the elements of
pair are called first and second, but it seems to me that they are
essentially arbitrary labels).

vector is an interesting case. Some vectors are used to store lists of
things (in which case ordering makes sense), but others are used to
model things like mathematical vectors (in which case we are back to the
complex case, and < does not make sense).

string definitely should have operator <() because it models the real
world concept (string of letters and other characters) for which "less
than" definitely makes sense.

-- 
Martin Bonner
Martin.Bonner_at_[hidden]
Pi Technology, Milton Hall, Ely Road, Milton, Cambridge, CB4 6WZ,
ENGLAND Tel: +44 (0)1223 203894

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk