Boost-range不能使用C ++ 1y init-capture mutable lambda

时间:2014-02-12 10:11:30

标签: c++ lambda mutable c++14 boost-range

我想使用Boost.Range C ++ 1y lambdas和init-capture 来计算两个向量的元素差异。减去一个向量的固定(即第一)元素的简单情况起作用。但是,当我尝试通过在第二个范围内增加迭代器来计算“矢量化差异”(并使lambda 可变)时,我得到编译器错误。示例代码(请注意,我没有使用通用lambdas,因此g ++ 4.8和Clang SVN都可以解析此代码):

#include <iostream>
#include <iterator>
#include <vector>
#include <boost/range/algorithm.hpp>
#include <boost/range/adaptors.hpp>

template<class R>
auto delta_beg(R const& rng1, R const& rng2)
{
    using Elem = typename R::value_type;
    return rng1 | boost::adaptors::transformed(
        [first2 = begin(rng2)](Elem const& e) {
        return e - *first2;
    });
}

template<class R>
auto delta_rng(R const& rng1, R const& rng2)
{
    using Elem = typename R::value_type;
    return rng1 | boost::adaptors::transformed(
        [first2 = begin(rng2)](Elem const& e) mutable {
        return e - *first2++;
    });
}

int main()
{
    auto r1 = std::vector<int>{ 8, 10, 12, 15 };
    auto r2 = std::vector<int>{ 1,  2,  9, 13 };

    // prints 7, 9, 11, 14
    boost::copy(delta_beg(r1, r2), std::ostream_iterator<int>(std::cout, ",")); std::cout << "\n";

    // ERROR, should print 7, 8, 3, 2
    boost::copy(delta_rng(r1, r2), std::ostream_iterator<int>(std::cout, ",")); std::cout << "\n";
}

Live Example。这里g ++和Clang都抱怨

  

没有名为'type'的类型   'boost :: mpl :: eval_if,boost :: result_of] :: __ lambda1(const int&amp;)&gt;,   boost :: mpl :: identity&gt; :: f_ {aka struct   boost :: result_of] :: __ lambda1(const int&amp;)&gt;}'        typedef typename f _ :: type type;

问题:发生了什么事?

1 个答案:

答案 0 :(得分:1)

只是闭包类型没有boost::mpl显然需要的嵌套typedef。如果将lambda表达式转换为std::function

,它就可以工作
#include <iostream>
#include <iterator>
#include <vector>
#include <boost/range/algorithm.hpp>
#include <boost/range/adaptors.hpp>

template<class R>
auto delta_beg(R const& rng1, R const& rng2)
{
    using Elem = typename R::value_type;
    std::function<Elem(Elem const&)> f = 
        [first2 = begin(rng2)](Elem const& e) { return e - *first2; };
    return rng1 | boost::adaptors::transformed(f);
}

template<class R>
auto delta_rng(R const& rng1, R const& rng2)
{
    using Elem = typename R::value_type;
    std::function<Elem(Elem const&)> f = 
        [first2 = begin(rng2)](Elem const& e) mutable { return e - *first2++; };
    return rng1 | boost::adaptors::transformed(f);
}

int main()
{
    auto r1 = std::vector<int>{ 8, 10, 12, 15 };
    auto r2 = std::vector<int>{ 1,  2,  9, 13 };

    // prints 7, 9, 11, 14
    boost::copy(delta_beg(r1, r2), std::ostream_iterator<int>(std::cout, ",")); std::cout << "\n";

    // ERROR, should print 7, 8, 3, 2
    boost::copy(delta_rng(r1, r2), std::ostream_iterator<int>(std::cout, ",")); std::cout << "\n";
}

Live demo.