【发布时间】:2017-02-07 21:25:03
【问题描述】:
我有以下代码:
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)> >。这个 sn-p 的代码不会编译。但是,如果我将绑定移动到一个变量中并像这样传递它:
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)
}
});
它有效。为什么在这种情况下我必须将第二个绑定作为变量传递才能编译?
【问题讨论】:
-
我意识到我的问题可能是 std::bind 在第一个示例中使用时无法推断出我的返回类型。所以现在我尝试像这样强制转换 std::bind:
std::function<void(ASwatterFly*)>(std::bind(&ASwatterLevelBase::ShootAndScootMisdirection, this, std::placeholders::_1, FVector2D(0.f, 0.f), 8))但是,我收到了这个错误:error C4868: compiler may not enforce left-to-right evaluation order in braced initializer list如何显式转换 std::bind? -
虽然现在想起来,第一个 std::bind 的评估是正确的,所以我现在还不确定。
-
好吧,this question 的答案似乎是我遇到的问题,并且会解释为什么第一个示例不起作用,因为我需要将它包装在 std ::function 如果它是一个绑定,它是绑定中的一个参数。但这又回到了我之前关于选角的评论;我如何将绑定包装在 std::function 中以便编译?
标签: c++ function-pointers multimap std-function stdbind