【问题标题】:std::bind as an argument to std::bindstd::bind 作为 std::bind 的参数
【发布时间】: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&lt;void(ASwatterFly*)&gt; &gt; 转换为 std::function&lt;void(void)&gt; &gt;。这个 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&lt;void(ASwatterFly*)&gt;(std::bind(&amp;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


【解决方案1】:

正如我在我的一个问题 cmets 中提到的,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 来包装绑定,而不必为多重映射中的每个新绑定创建一个变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-17
    • 2014-03-25
    • 2013-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多