【问题标题】:Conversion of boost placeholders with arithmetic to std使用算术将 boost 占位符转换为 std
【发布时间】:2020-09-08 15:51:37
【问题描述】:

我正在转换一个使用 boost 和占位符来转换现有逻辑的地图的 C++ 项目:

inline const std::vector<uint32_t> average(const std::pair<uint16_t, std::vector<uint32_t> >& left,
                                           const std::pair<uint16_t, std::vector<uint32_t> >& right)
{
    // Ejector rates should be symmetrical
    assert(left.second.size() == right.second.size());
    std::vector<uint32_t> result;
    result.reserve(left.second.size());
    namespace bl = boost::lambda;
    // Walk both, do funny thing with each element in turn. Stuff into result.
    std::transform(left.second.begin(), left.second.end(), right.second.begin(), std::back_inserter(result), (bl::_1 + bl::_2) / 2);
    return result;
}

我想将 boost 引用替换为 std:

inline const std::vector<uint32_t> average(const std::pair<uint16_t, std::vector<uint32_t> >& left,
                                           const std::pair<uint16_t, std::vector<uint32_t> >& right)
{
    // Ejector rates should be symmetrical
    assert(left.second.size() == right.second.size());
    std::vector<uint32_t> result;
    result.reserve(left.second.size());
    namespace bl = boost::lambda;
    // Walk both, do funny thing with each element in turn. Stuff into result.
    std::transform(left.second.begin(), left.second.end(), right.second.begin(), std::back_inserter(result),
        (std::placeholders::_1 + std::placeholders::_2) / 2);
    return result;
}

我明白了:

error C2784: 'std::_Deque_const_iterator<_Mydeque> std::operator +(_Deque_const_iterator<_Mydeque>::difference_type,std::_Deque_const_iterator<_Mydeque>)' : could not deduce template argument for 'std::_Deque_const_iterator<_Mydeque>' from 'std::_Ph<2>'
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\deque(555) : see declaration of 'std::operator +'

在包含以下内容的行上:

(std::placeholders::_1 + std::placeholders::_2) / 2);

这样做的正确方法是什么?

【问题讨论】:

  • 按照与other question 相同的替换逻辑,即[&amp;](uint32_t const lhs, uint32_t const rhs) { return (lhs + rhs) / 2; }
  • @JeJo 我确实没有。只是一个习惯:)

标签: c++ boost std placeholder


【解决方案1】:

使用 lambda。

std::transform(left.second.begin(), left.second.end(), right.second.begin(), std::back_inserter(result),
        [](auto a, auto b){ return (a + b) / 2; });

【讨论】:

    【解决方案2】:

    只需使用 lambda。 一般情况下你可以使用const auto refs来避免参数的处理:

    std::transform(left.second.begin(), left.second.end(), right.second.begin(), std::back_inserter(result), [] (auto const &a, auto const &b) { return (a+b)/2; });
    

    对于像 int 这样的小数字类型,您可以按值传递它们并仅使用 const auto 或明确指定类型:

    std::transform(left.second.begin(), left.second.end(), right.second.begin(), std::back_inserter(result), [] (uint32_t a, uint32_t b) { return (a+b)/2; });
    

    【讨论】:

    • 好的,谢谢!已经在编辑我的答案以显示这种情况)))但是由于您的评论,我重新检查并编辑了一些模棱两可的术语。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 2019-03-24
    • 2012-06-25
    • 2014-03-27
    • 1970-01-01
    相关资源
    最近更新 更多