7

The implementation of boost::lower_bound (found here) in Range 2.0 takes its argument by value.

Why is this? std::lower_bound takes its argument by const ref - see here

wreckgar23
  • 1,025
  • 9
  • 22

2 Answers2

1

While it is difficult to know for sure the reason for this, there are two things to keep in mind:

  • The general reason for passing by value is when you end up making a copy in the function. Also, passing by value can potentially invoke the move constructor on prvalues/xvalues and the copy constructor on lvalues.

  • In the recent versions of the boost library boost::lower_bound uses std::lower_bound in its implementation. Boost 1.59 has the following implementation for the overloads of boost::lower_bound mentioned in your link:

    template< class ForwardRange, class Value >
    inline BOOST_DEDUCED_TYPENAME range_iterator<const ForwardRange>::type
    lower_bound( const ForwardRange& rng, Value val )
    {
        BOOST_RANGE_CONCEPT_ASSERT(( ForwardRangeConcept<const ForwardRange> ));
        return std::lower_bound(boost::begin(rng), boost::end(rng), val);
    }

    template< range_return_value re, class ForwardRange, class Value >
    inline BOOST_DEDUCED_TYPENAME range_return<const ForwardRange,re>::type
    lower_bound( const ForwardRange& rng, Value val )
    {
        BOOST_RANGE_CONCEPT_ASSERT(( ForwardRangeConcept<const ForwardRange> ));
        return range_return<const ForwardRange,re>::
            pack(std::lower_bound(boost::begin(rng), boost::end(rng), val),
                 rng);
    }


P.W
  • 26,289
  • 6
  • 39
  • 76
  • Range algorithms were introduced in version [1.43](https://www.boost.org/doc/libs/1_49_0/libs/range/doc/html/range/upgrade/upgrade_from_1_42.html). I looked in the [1.43 implementation](http://sourceforge.net/projects/boost/files/boost/1.43.0/) and they use `std::lower_bound` also – wreckgar23 Apr 03 '19 at 10:02
  • 1
    @wreckgar23: So, the second point is not relevant here. Let's see if someone else can provide a more complete answer for this. – P.W Apr 03 '19 at 10:07
1

This has now been fixed by this issue.

There may be historical reasons for taking the argument by value. See this answer about function objects passed by value to standard algorithms.

wreckgar23
  • 1,025
  • 9
  • 22