Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does `boost::lower_bound` take its argument by value?

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

like image 401
wreckgar23 Avatar asked Apr 02 '19 13:04

wreckgar23


2 Answers

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);
    }


like image 111
P.W Avatar answered Sep 21 '22 03:09

P.W


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.

like image 23
wreckgar23 Avatar answered Sep 24 '22 03:09

wreckgar23