|
Boost : |
Subject: Re: [boost] [Review:Algorithms] Order of args to clamp
From: Dave Abrahams (dave_at_[hidden])
Date: 2011-09-25 10:08:36
on Sat Sep 24 2011, "Jeffrey Lee Hellrung, Jr." <jeffrey.hellrung-AT-gmail.com> wrote:
> On Sat, Sep 24, 2011 at 7:06 PM, Dave Abrahams <dave_at_[hidden]> wrote:
>
>>
>> on Sat Sep 24 2011, Andrew Sutton <asutton.list-AT-gmail.com> wrote:
>>
>> > Define it in terms of a single type and let conversions happen at the
>> > call type.
>>
>> +1. The point is that you probably don't really understand what it
>> means to order different types, and the theorems required to make it
>> semantically well-founded are complicated enough that users of the
>> algorithms won't be able to know whether they're using it properly.
>>
>
> I don't agree with this reasoning. If "you" (impersonal) don't really
> understand what it means to order objects of different types, why would you
> (or anyone else) define an operator< between objects of those types?
That's not really the point.
> Stated differently, I would think that the existence of an operator<
> comparing objects of different types implies (more often than not) an
> ordering between objects of those types.
What does "an ordering between objects of [two different] types" mean,
please?
> And if a user implicitly requests the use of this operator< through a
> call to clamp, I don't see why the interface should prohibit that.
So what does your flexible version of clamp *do*? Please show me the
documentation.
> And regarding the 4-argument clamp, here we're replacing operator< with a
> user-provided comparison function object, so, again, I think it's
> appropriate to assume that the user knows what he or she is doing if
> comparing objects of different types.
Really? I think you put far too much faith in programmers knowing what
they're doing... most of us write down what we can get away with and are
satisfied if it "seems to work." But again, that isn't the point. The
point isn't to protect programmers from themselves, but to write and
document a meaningful algorithm.
Here's an exercise:
1. Write down the documentation for both the multi-type and single-type
"clamp" algorithms. Describe both the concept requirements on the
algorithm parameters, and the result of each algorithm (without
making reference to its implementation).
2. Demonstrate that the multi-type algorithm does what the single type
algorithm does when passed only a single argument type.
3. Imagine yourself wanting to clamp an int. Compare the documentation
and reasoning you'd have to go through to satisfy yourself that the
algorithm does what you want in each case.
-- Dave Abrahams BoostPro Computing http://www.boostpro.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk