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

2

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

Whitman answered 2/4, 2019 at 13:42 Comment(4)
It relies on the compiler to eliminate unnecessary copies, it seems.Upturn
Bug report? Seems like an easy mistake to make.Onerous
@Onerous will do. The library is at least five years old though (probably older) so I was expecting that there was an implementation reason. Will update the question if I hear backWhitman
Added an issue hereWhitman
W
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.

Whitman answered 14/2, 2020 at 17:57 Comment(0)
A
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);
    }


Albion answered 3/4, 2019 at 5:26 Comment(2)
Range algorithms were introduced in version 1.43. I looked in the 1.43 implementation and they use std::lower_bound alsoWhitman
@wreckgar23: So, the second point is not relevant here. Let's see if someone else can provide a more complete answer for this.Albion
W
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.

Whitman answered 14/2, 2020 at 17:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.