Boost-range не работает с C ++ 1y init-capture изменяемая лямбда

Я хочу вычислить поэлементную разницу двух векторов, используя Boost.Range а также C ++ 1й лямбда с init-capture. Более простой случай вычитания фиксированного (то есть первого) элемента одного вектора работает. Однако, когда я пытаюсь вычислить «векторизованную разницу», увеличивая итератор во втором диапазоне (и делая лямбду изменчивый), Я получаю ошибку компилятора. Пример кода (обратите внимание, что я не использовал обобщенные лямбда-выражения, чтобы 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";
}

Живой пример. Здесь и G ++, и Clang жалуются на

нет типа с именем «тип» в
‘boost :: mpl :: eval_if, boost :: result_of] :: __ lambda1 (const int&)>,
boost :: mpl :: identity> :: f_ {он же структура
boost :: result_of] :: __ lambda1 (const int&)>}»
typedef имя типа f _ :: type type;

Вопрос: что здесь происходит?

1

Решение

Просто у closure-типов нет вложенных typedef, которые boost::mpl видимо требует. Это работает, если вы преобразуете лямбда-выражение в 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";
}

Живая демоверсия.

1

Другие решения

Других решений пока нет …