Boost logo

Boost :

Subject: Re: [boost] Boost.Algorithm design question
From: Dave Abrahams (dave_at_[hidden])
Date: 2011-10-06 04:59:59


on Wed Oct 05 2011, Marshall Clow <mclow.lists-AT-gmail.com> wrote:

> So, what do people prefer (and why?):
>
> template<typename InputIterator, typename V>
> bool none_of_equal ( InputIterator first, InputIterator last, V const &val )
> {
> for ( ; first != last; ++first )
> if ( val == *first )
> return false;
> return true;
> }
>
> or
>
> template<typename InputIterator, >
> bool none_of_equal ( InputIterator first, InputIterator last, iterator_traits<InputIterator>::value_type const &val )
> {
> for ( ; first != last; ++first )
> if ( val == *first )
> return false;
> return true;
> }
>
> In the first case, I think there's (possible) conversion from V to
> iterator_traits<InputIterator>::value_type each time through the loop.
> In the second case, there is not.

I prefer the second one.

- It's the one that matches the mental model: I don't know what the
  first algorithm /means/.
- An exact specification for the first one is more complicated
- It's more efficient in the usual case when a conversion would be
  needed

> Of course, the (mythical?) smart compiler would do the conversion once
> and save the result for reuse.

I think you know the compiler would have to be able to verify that the
conversion had no side-effects; not something we're likely to see for
some time.

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