Boost logo

Boost :

Subject: Re: [boost] Boost.Algorithm design question
From: Dave Abrahams (dave_at_[hidden])
Date: 2011-10-29 01:49:43


on Fri Oct 07 2011, "Peter Dimov" <pdimov-AT-pdimov.com> wrote:

> Dave Abrahams wrote:
>> > Bottom line, you'll add a conversion and a bunch of unnecessary
>
>> > requirements and end up with the exact same specification as before,
>> > namely, returns the first i for which *i == value.
>>
>> I claim that "finds the first element in [first, last) that equals
>> value" is a better way to say it.
>
> I know, and I question this claim. Why is it better, when it says the
> exact same thing, while needing to impose unnecessary requirements to
> arrive at it?
>
> The current specification (ignoring the conversion issue for now, as
> it's separate) says:
>
> - when T is EqualityComparable, returns the first element that equals value;
> - otherwise, returns the first element *i for which *i == value.
>
> Your preferred replacement says:
>
> - when T is EqualityComparable, returns the first element that equals value;
> - otherwise, the behavior is undefined.
>
> Do tell me why it's better.

Here's another reason why the first formulation might not be such a hot
idea: it rules out some obvious implementations that really ought to be
OK. For example,

      template <class InputIterator, class T>
      InputIterator find(InputIterator i, InputIterator j, T value)
      {
          while (i != j && !(*i == value))
              ++i;
          return i;
      }

(this is essentially equivalent to the implementation of find in the
original STL).

-- 
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