2016-11-15 14 views
0

私のラムダに関しては定型コードがたくさんあります。私はそれが見えるようにしたいどのようにC++は別のラムダに関数の引数を渡します

myClass->event =[](T something,T something2,T something3) 
{ 
    yetAnotherFunction(something,something,something3); 
    //do something else. 
} 

:などの実行時に割り当てられたそのラムダで

class myClass 
{ 
    public: 
    std::function<void(int,int)> event; 
    std::function<void(std::string)> otherEvent; 
    <many more std::function's with different types> 
} 

:ここではさしあたり、粗

でMyClassのは、このようになっていることを前提とすることができます:

void attachFunction(T& source, T yetAnotherFunction) 
{ 
    source = [](...) 
    { 
     yetAnotherFunction(...); 
     //do something else. 
    } 
} 

私はこのような何かを呼び出すことができるように:

attachFunction(myClass->event,[](int a,int b){}); 

attachFunction(myClass->otherEvent,[](std::string something){}); 

私は単にパラメータに沿って通過し、それらが一致することを確認します。

これを関数にラップするにはどうすればいいですか?定義されていない数のパラメータと異なる型があると仮定します。

ありがとうございます!

+0

'eventList'は' map'ですか?そのタイプは何ですか? – Arunmu

+0

ああ、私が編集する悪い例です。 std :: function OnClick – Avalon

+0

のようなランタイムで定義されたラムダを持つクラスを使用しています。 'イベント'とは何ですか?イベントに添付されたラムダは、常に3つのパラメータをとり、内部関数のシグネチャだけが変更されますか? – Arunmu

答えて

0

私はこれを解決することができました。これは私の解決策です。

元の機能は追加で拡張され、元のラムダから以前の機能が消去されます。ここで

は使用例です:

添付イベントを:順番に印刷する必要があります

std::function<void(float,float,float)> fn = [](float a ,float b,float c){}; 
    std::function<void(float,float,float)> additional = [](float a,float b,float c){std::cout << a << b << c << std::endl;}; 

    attachEvent(fn,additional); 
    fn(2.0f,1.0f,2.0f); 

関連する問題