#Passing a std::function by declaration / name

26 messages · Page 1 of 1 (latest)

glass hound
#

Given that my function takes in a std::function<void(void)>, how could I pass in a function by declaration / name?
As in

void start(std::chrono::milliseconds interval, std::function<void(void)> func);
void ChaosModController::Start() {
    _timer->start(_intervalTime, Interval);  // <-- Making this work
}

void ChaosModController::Interval() {

}
native stirrupBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

dense epoch
#

is ChaosModController a class or a namespace?

#

If class, is Interval a static or non static member function?

glass hound
#

It's a class and non-static 😄

dense epoch
#

So non static member function sort of implcitly have a first parameter being a pointer to the object it is being acted on.
I think the type is something like void (ChaosModController::*)(void) or some shit... function types are weird and also not really relevant to what the solution is.
It fix the problem, you need to bind an instance of the object to the function method using std::bind (or bind_front if you don't want to bind all arguments)
In this case std::bind(&ChaosModController::Interval, ChaosModControllerPointer)

#

This creats a new function object which takes void and returns void while passing the correct object pointer

glass hound
#

And what's the trade-off to just use an anon function?

dense epoch
#
#include <functional>

void ChaosModController::Start() {
    _timer->start(
        _intervalTime,
        std::bind(&ChaosModController::Interval, this)
    );  // <-- Making this work
}
#

What do you mean anon function
Like... a lambda?

#

I'll be honest, the difference probably minor, esoteric, and highly depended on complicated stdlib code.
If you are concerned, measure.
Otherwise, they are probably similar

glass hound
#

But no, even with that I need the context of "this"! Which needs a different function signature

#

So yeah, bind it is I guess

dense epoch
#

that is a lambda, and to make it work, you have to add "this" to the capture list (inside the square brackets)

#

I think bind would be marginally better

glass hound
#

Passing the this-reference in the square brackets would require passing the this-type (chaosmodcontroller) into the parameters, right?

dense epoch
#

What does that even mean?
Also this is a pointer, so you would have to do this->Interval();

glass hound
#

I understand [] being for the captured context, () being the lambda function parameters, { } being the scope DFrido_panik

#

But I didnt read into it, so dont mind me being wrong

tired goblet
#

Noooo, not std::bind notlikethis

#
_timer->start(_intervalTime, [this] { Interval(); });
#

It was probably already said, but ChaosModController::Interval requires a ChaosModController to be called and std::function<void()> doesn't have one. The lambda does.

dense epoch
tired goblet
#

It's more complicated and less flexible than a lambda and also I never learned to use it.

#

Using std::bind is fine. Teaching std::bind is not ideal.