1

Boost.Rangeinit-capture で C++1y ラムダを使用して、2 つのベクトルの要素ごとの差を計算したいと考えています。1 つのベクトルの固定 (つまり最初の) 要素を減算する単純なケースが機能します。ただし、反復子を 2 番目の範囲で増やして (そしてラムダを可変にして) 「ベクトル化された差」を計算しようとすると、コンパイラ エラーが発生します。サンプル コード (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_ {aka struct boost::result_of] に 'type' という名前の型がありません: :__lambda1(const int&)>}' typedef typename f_::type type;

質問: 何が起こっているのですか?

4

1 に答える 1

2

boost::mplクロージャー型には、明らかに必要なネストされた typedef がないというだけです。ラムダ式を次のように変換すると機能します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";
}

ライブデモ。

于 2014-02-12T10:18:02.713 に答える