我想使用 Boost.Range 和 C++1y lambda 以及 init-capture 来计算两个向量的元素差异。减去一个向量的固定(即第一个)元素的更简单情况是有效的。但是,当我尝试通过在第二个范围内增加迭代器(并使 lambda 可变)来计算"矢量化差异"时,我收到编译器错误。示例代码(请注意,我没有使用通用的 lambda,因此 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都抱怨
中没有名为"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;
问:这是怎么回事?
只是闭包类型没有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";
}
现场演示。