Boost范围不工作与C ++ 1y init捕获可变的lambda [英] Boost-range not working with C++1y init-capture mutable lambda

查看:279
本文介绍了Boost范围不工作与C ++ 1y init捕获可变的lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to compute the element-wise difference of two vectors using Boost.Range and C++1y lambdas with init-capture. The simpler case of subtracting a fixed (i.e. the first) element of one vector works. However, when I try to compute the "vectorized difference" by increasing the iterator over the second range (and making the lambda mutable), I get a compiler error. Sample code (note that I didn't use generalized lambdas so that both g++ 4.8 and Clang SVN can parse this code):

#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";
}

在线示例 。这里g ++和Clang都抱怨

Live Example. Here both g++ and Clang complain about


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

no type named 'type' in 'boost::mpl::eval_if, boost::result_of]::__lambda1(const int&)>, boost::mpl::identity >::f_ {aka struct boost::result_of]::__lambda1(const int&)>}' typedef typename f_::type type;

问题:发生了什么?


Question: what is going on?

推荐答案

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

It's just that closure-types don't have nested typedefs that boost::mpl apparently requires. It works if you convert the lambda expression to 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";
}

在线演示。

这篇关于Boost范围不工作与C ++ 1y init捕获可变的lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆