为什么 `boost::lower_bound` 按值获取参数?

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

Range 2.0 中 boost::lower_bound(找到 here)的实现按值获取其参数。

这是为什么? std::lower_bound 通过 const ref 获取参数 - 参见 here

虽然很难确定其中的原因,但有两点需要牢记:

  • 按值传递的一般原因是当您最终在函数中进行复制时。此外,按值传递可能会调用 prvalues/xvalues 上的移动构造函数和左值上的复制构造函数。

  • 在最新版本的 boost 库中 boost::lower_bound 在其实现中使用了 std::lower_bound。 Boost 1.59 对 link 中提到的 boost::lower_bound 的重载有以下实现:

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


此问题现已修复 by this issue

按价值来论证可能有历史原因。请参阅 this answer 关于按值传递给标准算法的函数对象。