Let's imagine I define a naive function reference wrapper like follows :
struct wrapper {
std::function<void(void)>& fct;
wrapper(std::function<void(void)>& _fct) : fct{_fct}{
fct();
};
};
Of course, in reality it is more complex than this, but I simplified it to illustrate the problem.
Then, I would like to create my test object which contains the function reference wrapper :
struct test {
wrapper myWrapper;
test() : myWrapper{std::bind(&test::boom, this)}{};
void boom() {
std::cout << "boom()" << std::endl;
}
};
At test
object instanciation, I get the following error (try yourself here) :
cannot bind non-const lvalue reference of type 'std::function<void()>&' to an rvalue of type 'std::function<void()>'
It is understandable, since the std::bind
object is a temporary (rvalue) for which I cannot take a reference (I also tried my way without std::bind
, without better results). On the other hand, being able to take a reference to a non-static member function as a std::function
seems like something easy to do, but I can't wrap my head around it.
Is there any simple way to take a reference to a non-static member function as a std::function&
?