0

I have the following code:

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))
    }
});

I have a bind inside of another bind to convert from std::function<void(ASwatterFly*)> > to std::function<void(void)> >. This snippet of code won't compile. However, if I move the bind into a variable and pass it like this:

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)
    }
});

it works. Why do I have to pass in the second bind as a variable in this case for it to compile?

3
  • I realized that my issue is probably that std::bind can't deduce what my return type is when it's used in the first example. So now I tried to cast the std::bind like so: std::function<void(ASwatterFly*)>(std::bind(&ASwatterLevelBase::ShootAndScootMisdirection, this, std::placeholders::_1, FVector2D(0.f, 0.f), 8)) However, I get this error: error C4868: compiler may not enforce left-to-right evaluation order in braced initializer list How can I explicitly cast the std::bind?
    – Rael
    Feb 8, 2017 at 0:32
  • Although now that I think of it, the first std::bind is evaluated correctly, so I'm not even sure now.
    – Rael
    Feb 8, 2017 at 0:42
  • Alright, it would seem that the answer to this question would be the issue I'm having, and would explain why the first example doesn't work, because I need to wrap it in a std::function if it's a bind that's an argument in a bind. But that leads back to my previous comment with regards to casting; how would I wrap the bind in an std::function so that it would compile?
    – Rael
    Feb 8, 2017 at 3:20

1 Answer 1

0

So as I mentioned in one of my question's comments, the answer to this question was the reasoning behind why this was happening. What I wanted to do was be able to add in bind arguments inside of a bind without creating new variables everytime. I came up with a suitable solution by doing this:

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.
});

So I can just use DummyFunc to wrap the bind without having to create a variable for each new bind in the multimap.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.