2

コールバックとして入力する必要のある式がありstd::transformますが、そのための別のメソッドを記述したくありません。渡される唯一の引数がタイプであると仮定する式を表現_1.second->pair().first == rしたいboost::lambda_1std::pair

私は一般的な速記ファンクターを持っておりutil::shorthand::pair_secondutil::shorthand::pair_firstペアの2番目と1番目のアイテムを返します。

私はできますboost::bind(util::shorthand::pair_second, _1)が、それから何をしますか?式の残りの部分を実装する方法は?

-1.secondタイプのテンプレートですConnection<T>。C++11は使用できません。

4

1 に答える 1

2

Boost.Lambdaバインド式を見てください。それらを使用して、メンバー関数とメンバーデータをバインドできます。

使用しているタイプに関する十分な情報がありませんが、次のようなものが機能するはずです。

bind(&std::pair<S, R>::first,
     bind(&Connection<T>::pair,
          bind(&std::pair<U, Connection<T> >::second, _1))) == r

注:bindこのサンプルのは、boost::lambda名前空間にあるものであり、Boost.Bindではありません。

編集:完全でコンパイル可能な例:

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>

// http://stackoverflow.com/questions/12026884/expressing-1-second-pair-first-r-in-boostlambda/

typedef int S;
typedef int R;

template <typename T>
struct Connection
{
    std::pair<S, R> pair() const {
        return std::make_pair(0, 0);
    }
};

int main()
{
    using namespace boost::lambda;

    typedef int T;
    typedef int U;

    std::vector<std::pair<U, Connection<T> > > vec;
    vec.push_back(std::make_pair(3, Connection<T>()));
    std::vector<bool> res(vec.size());

    int r = 0;
    std::transform(vec.begin(), vec.end(), res.begin(),
        bind(&std::pair<S, R>::first,
            bind(&Connection<T>::pair,
                bind(&std::pair<U, Connection<T> >::second, _1))) == r);

    std::vector<bool>::iterator it, end = res.end();
    for (it = res.begin(); it != end; ++it) {
        std::cout << ' ' << *it;
    }
    std::cout << std::endl;
}
于 2012-08-19T13:50:23.737 に答える