2017-02-07 14 views
0

私は次のコードを持っている:のstd ::はstdの引数としてバインド::バインド

static std::vector<std::multimap<float, std::function<void(void)> > > LoadedDelegates; 

std::function<void(FVector2D, std::function<void(ASwatterFly*)>)> ShooterFunc = [this](FVector2D Input, std::function<void(ASwatterFly*)> InputFly) 
{ 
    this->RemainingFlies.Add(this->MakeShooterFly(Input, InputFly)); 
}; 

LoadedDelegates.push_back(std::multimap<float, std::function<void(void)> > 
{ 
    { 
     3.f, 
     std::bind(ShooterFunc, FVector2D(1.f, 0.f), std::bind(&ASwatterLevelBase::ShootAndScootMisdirection, this, std::placeholders::_1, FVector2D(0.f, 0.f), 8)) 
    } 
}); 

を私はstd::function<void(ASwatterFly*)> >からstd::function<void(void)> >に変換するために別のバインドの内部バインドを持っています。このコードスニペットはコンパイルされません。しかし、バインドを変数に移動して、次のように渡すと:

static std::vector<std::multimap<float, std::function<void(void)> > > LoadedDelegates; 

std::function<void(FVector2D, std::function<void(ASwatterFly*)>)> ShooterFunc = [this](FVector2D Input, std::function<void(ASwatterFly*)> InputFly) 
{ 
    this->RemainingFlies.Add(this->MakeShooterFly(Input, InputFly)); 
}; 

std::function<void(ASwatterFly*)> TestFunc = std::bind(&ASwatterLevelBase::ShootAndScootMisdirection, this, std::placeholders::_1, FVector2D(0.f, 0.f), 8); 

LoadedDelegates.push_back(std::multimap<float, std::function<void(void)> > 
{ 
    { 
     3.f, 
     std::bind(ShooterFunc, FVector2D(1.f, 0.f), TestFunc) 
    } 
}); 

これが機能します。この場合、2番目のバインドを変数として渡してコンパイルする必要があるのはなぜですか?

+0

私の問題はおそらくstd :: bindは、最初の例で使用したときの戻り値の型を推定できないことに気付きました。今度はstd :: bindを以下のようにキャストしようとしました: 'std :: function (std :: bind(&ASwatterLevelBase :: ShootAndScootMisdirection、this、std :: placeholders :: _ 1、FVector2D 'エラーC4868:ブレースされたイニシャライザリストでコンパイラが左から右の評価順序を強制しない ' 明示的にstdをキャストするにはどうすればよいですか? ::バインド? – Rael

+0

今私はそれについて考えていますが、最初のstd :: bindは正しく評価されているので、私は今も確信していません。 – Rael

+0

申し訳ありませんが、[この質問](http://stackoverflow.com/questions/25841857/vc2013-function-from-bind-not-compiling/25842919#25842919)の回答が私の問題になるようです最初の例がうまくいかない理由を説明します。バインドの引数であるバインドの場合は、std :: functionでラップする必要があるからです。しかし、それはキャスティングに関する私の以前のコメントに戻る。私はどのようにstd ::のバインドをラップして、それはコンパイルされるだろうか? – Rael

答えて

0

私の質問のコメントの1つで説明したように、this questionへの回答は、これがどうして起こったのかという理由になりました。私がしたかったのは、毎回新しい変数を作成せずにバインドのバインド引数をバインドの中に追加できることでした。

static std::vector<std::multimap<float, std::function<void(void)> > > LoadedDelegates; 

std::function<void(FVector2D, std::function<void(ASwatterFly*)>)> ShooterFunc = [this](FVector2D Input, std::function<void(ASwatterFly*)> InputFly) 
{ 
    this->RemainingFlies.Add(this->MakeShooterFly(Input, InputFly)); 
}; 

std::function<void(ASwatterFly*)> DummyFunc; 

LoadedDelegates.push_back(std::multimap<float, std::function<void(void)> > 
{ 
    { 
     3.f, 
     std::bind(ShooterFunc, FVector2D(1.f, 0.f), DummyFunc = std::bind(&ASwatterLevelBase::ShootAndScootMisdirection, this, std::placeholders::_1, FVector2D(0.f, 0.f), 8)) 
    }, 
    { 
     6.f, 
     std::bind(ShooterFunc, FVector2D(-0.2f, 1.f), DummyFunc = std::bind(&ASwatterLevelBase::LaserShooter, this, std::placeholders::_1, this->GetVecFromScreenPercentage(0.15f, 0.9f), 1, 0.f, 0.f, 3.f, 260.f, 0.f, FLinearColor(15.f, 3.f, 3.f))) 
    }, 
    //etc. 
}); 

だから私はちょうどマルチマップ内の各新しいバインドのための変数を作成することなく、バインドをラップするDummyFuncを使用することができます。私はこれを行うことによって、適切な解決策を考え出しました。

関連する問題