Passing non-static member function as std::function [duplicate] - c++

This question already has answers here:
Using generic std::function objects with member functions in one class
(6 answers)
Closed 4 years ago.
I have a similar situation:
class A {
void callingFunction() {
calledFunction(passedFunction);
}
void calledFunction(std::function<void(int)> foo) {
}
void passedFunction(int arguments) {
}
};
The compiler error is
error: invalid use of non-static member function
How do I achieve this without making the passedFunction static?
Doing this:
calledFunction(std::bind(&A::passedFunction, this);
Creates this error:
error:static assertion failed: Wrong number of arguments foor pointer-to-member
Does it mean I have to provide all the arguments in the callingFunctionwhen passing the passedFunction? This is not possible as the arguments for the passedFunction are specified in the calledFunction

You can write a lambda capturing this, on which passedFunction could be called.
calledFunction([this](int v) { this->passedFunction(v); });

Related

Passing a member function to a base class constructor results in "invalid use of non-static function..." [duplicate]

This question already has answers here:
How can I pass a member function where a free function is expected?
(9 answers)
how to pass a non static-member function as a callback?
(8 answers)
Pass Member Function as Parameter to other Member Function (C++ 11 <function>) [duplicate]
(2 answers)
Closed 4 months ago.
I need to change the prototype of a function pointer of a class. So, I was hoping inheriting it and doing the following would work, but it doesn't ("invalid use of non-static member function 'void B::myIntCallback(unsigned int)"):
class A {
public:
typedef void (*intCallback_t)(unsigned int myInt);
A(intCallback_t intCallback) {}
};
class B : A {
public:
typedef void (*charCallback_t)(unsigned char myChar);
B(charCallback_t charCallback) : A(this->myIntCallback) {
charCallback_ = charCallback;
}
private:
charCallback_t charCallback_;
void myIntCallback(unsigned int myInt) {
charCallback_((unsigned char)myInt);
}
};
Does anybody know how I can solve this? I can't change class A.
You are trying to pass a member function (B::myIntCallback) to a function pointer argument. Since the member function needs an object to be called on, his would require some kind of capturing, e.g. a lambda capturing this or an std::bind expression. Unfortunately, neither is possible with a plain function pointer, see also Passing capturing lambda as function pointer.
If possible, you may want to consider changing the class A to accept either a std::function or a template argument as the type of the callback. See also Should I use std::function or a function pointer in C++?.

Does std::function keeps a reference to captured variables [duplicate]

This question already has answers here:
C++11 lambda implementation and memory model
(2 answers)
What is a lambda expression in C++11?
(10 answers)
Closed 2 years ago.
I'd like to understand why the following works:
class Foo {
public:
using Callback = std::function<void(std::string& string)>;
void setCallback(Callback&& callback) { windowInfo = callback; };
private:
Callback windowInfo;
};
class Bar {
Bar() { foo->setCallback([this](std::string& e) { sayHello(e); });}
void sayHello(std::string& name) { //do something }
Foo* foo;
};
Foo::setCallback asks for a function with no captures, however, I'm passing a lambda that captures Bar. I would expect the compiler to complain void(Bar, string) != void(string). Since I'm passing in a member function, it must implicitly pass the $this parameter as the first argument. Making the function signatures not equivalent.
EDIT
Why is that when I redefine using EventCallback as using EventCallback = void(Event& event); suddently this doesn't work anymore "no viable conversion from Application.cpp:10:34 to Foo::Callback aka (...)
Is std::function acting as the "glue" holding a reference to Bar but exposing the correct signature when requested?

Vector of member functions [duplicate]

This question already has answers here:
Using generic std::function objects with member functions in one class
(6 answers)
Closed 5 years ago.
What is the approach to have a vector to schedule method calls of a class. I would like to have a vector of methods, something like this:
class Model {
using task_t = std::function<void()>;
std::vector<std::vector<task_t>> _frameTasks;
void add_function() { _frameTasks[3].push_back(&(this->function()); }
void function() {std::cout << "hi"; }
void step() {
for (auto task : _frameTasks[3]) task();
}
}
But the compiler complains that:
error: ISO C++ forbids taking the address of a bound member function to form a pointer to member function.
How could I solve this issue or what is the right approach?
&(this->function()) is applying & to the result of the call to the function() member function. The default operator& requires an lvalue, however, the expression this->function() is not an lvalue.
Writing &Model::function won't work either because it is a pointer to a member function (as opposed to a pointer to a function). Its type is void (Model::*)(), i.e.: a pointer to a Model's (non-static) member function that takes no parameters and returns nothing.
You need some way to specify the Model object on which the function() member function will be called.
Since C++11 you can achieve that by simply using a lambda expression:
void add_function() {
_frameTasks[3].push_back(
[this](){ add_function(); }
);
}

C++ Arduino: assign a Function to function pointer [duplicate]

This question already has answers here:
Function pointer to member function
(8 answers)
Class member function pointer
(3 answers)
Closed 6 years ago.
I have a class that contains two similar non-static functions, I want to assign one of them on the fly to a function pointer:
A.h:
class A{
private:
void (*funcPtr)(int);
void do_sth(int i);
void do_sth_else(int i);
}
A.cpp:
A::A(int i){
if(i == 1)
A::funcPtr = A::do_sth; // or &A::do_sth;
else
A::funcPtr = A::do_sth_else; // or &A::do_sth_else;
}
But I get such error:
error: cannot convert 'A::do_sth' from type 'void (A::)(int)' to type 'void (*)(int)'
I read multiple similar issues, But cannot apply their solutions on my problem.
These are member functions. Either make funcPtr a member pointer by qualifying it with class name, or make do_sth(), do_sth_else() static member functions. I'd suggest using & in front of function name when taking the address of it.

unexpected compilation error when passing by reference anonymous instance [duplicate]

This question already has answers here:
How come a non-const reference cannot bind to a temporary object?
(11 answers)
Closed 7 years ago.
Let's say I have a struct :
struct structazauras
{
string bla;
}
And some function (in my case this function is actually a constructor of some class but I don't think this is the issue):
void myfunc(structazauras& mystruct) { }
then some where i call myfunc :
..
myfunc(structazauras());
..
I get an error:
no matching function for call to myfunc(structazauras) candidates are myfunc(structazauras&)
If I change the code to :
structazauras tmp;
myfunc(tmp);
It will work fine.
I feel that he (the compiler) has a problem passing a reference to the anonymous instance if structazauras, but why ? the anonymous instance exist on the stack of the calling function.
That is because you cannot bind a temporary to a non-const reference. Mark your reference as const and it will work.
Also, you are using a standard C++ keyword (struct) in the definition
void myfunc(structazauras& struct) { }
Change the name to something else. Or maybe you meant
void myfunc(struct structazauras&) { }
but the additional struct is superfluous in C++.