質問

はどのように後押し::バインド配列の添え字で動作するのですか?ここでは、私が達成しようとしているものです。アドバイスしてください。

[servenail:C ++ PROGS] $ G ++ -v
/usr/lib/gcc/i386-redhat-linux/3.4.6/specs
からスペックを読みます で構成:../configure --prefix =は/ usr --mandir =は/ usr / share / manを--infodir =を/ usr / share /情報--enable-共有--enable-スレッド= POSIX --disable-チェック--with-システムのzlib --enable -__ cxa_atexit --disable-libunwindの-例外--enable-javaの-AWT = gtkの--host = i386-のredhat-Linuxの
スレッドモデル:POSIX
gccのバージョン3.4.6 20060404(Red Hatの3.4.6-3)

  

[servenail:C ++ PROGS] $猫のT-array_bind.cpp

#include <map>
#include <string>
#include <algorithm>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <iostream>

using namespace std;
using namespace boost;
using namespace boost::lambda;

class X
{
public:
    X(int x0) : m_x(x0)
    {
    }

    void f()
    {
        cout << "Inside function f(): object state = " << m_x << "\n";
    }

private:
    int m_x;
};

int main()
{
    X x1(10);
    X x2(20);
    X* array[2] = {&x1, &x2};

    map<int,int> m;
    m.insert(make_pair(1, 0));
    m.insert(make_pair(2, 1));

    for_each(m.begin(),
             m.end(),
             array[bind(&map<int,int>::value_type::second, _1)]->f());
}
  

[servenail:C ++ PROGS] $ G ++ -o T-array_bindのT-array_bind.cpp      T-array_bind.cpp: '関数で `int型のmain():       T-array_bind.cpp:40:エラー:
に '[]演算子' の不一致   「配列[ブースト::ラムダ::バインド(のconst   ARG1 = INTとARG1&、constのARG2&)[   std ::ペア:: *、ARG2 =   ブースト::ラムダ:: lambda_functor>](((CONSTブースト::ラムダ:: lambda_functor>&)(+ブースト::ラムダ:::: _ 1)))] '

どうもありがとうございました。

役に立ちましたか?

解決

チャールズは説明しているように、

、ブースト::バインドは、関数オブジェクトではなく整数を返します。関数オブジェクトは、各メンバーのために評価されます。少しヘルパー構造体は、問題を解決します。

struct get_nth {
    template<class T, size_t N>
    T& operator()( T (&a)[N], int nIndex ) const {
        assert(0<=nIndex && nIndex<N);
        return a[nIndex];
    }
}

for_each(m.begin(),
         m.end(),
         boost::bind( 
             &X::f,
             boost::bind( 
                 get_nth(), 
                 array, 
                 bind(&map<int,int>::value_type::second, _1) 
             )
         ));

編集:私は、配列のn番目の要素を返すようにファンクタを変更しました。

他のヒント

boost::bindの戻り値は、配列の添字として使用することができます。のないの整数型であるため、

あなたのコードがコンパイルされない理由があります。むしろ、boost::bind()演算子を使用して呼び出すことができる不特定型の実装定義関数オブジェクトを返す。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top