Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::function to member function

#include <functional>  struct A {     int func(int x, int y)     {         return x+y;     } };  int main() {      typedef std::function<int(int, int) > Funcp;     A a;     //Funcp func = std:::bind(&A::func, &a);     Funcp func = std::bind(&A::func, a, std::placeholders::_1);       return 0; } 

I am getting errors in both of the above bind functions:

 error C2825: '_Fty': must be a class or namespace when followed by '::' 

Where is the syntax error? I am using visual studio 2010

like image 246
hidayat Avatar asked Mar 01 '11 11:03

hidayat


People also ask

How do you call a function from a member function in C++?

Case 2: You want the class to have an instance, you first need to create an object so that the class has an identity, once that is done you can use the object his methods. Myclass m; m. printInformation(); // Or, in the case that you want to use pointers: Myclass * m = new Myclass(); m->printInformation();

How do you call a pointer to a member function?

Using a pointer-to-member-function to call a function Calling the member function on an object using a pointer-to-member-function result = (object. *pointer_name)(arguments); or calling with a pointer to the object result = (object_ptr->*pointer_name)(arguments);

Does std :: function allocate?

Many std::function implementations will avoid allocations and use space inside the function class itself rather than allocating if the callback it wraps is "small enough" and has trivial copying.

Is std :: function slow?

If it is small, like 3-5 CPU instructions then yes std::function will make it slower, because std::function is not inlined into outer calling code. You should use only lambda and pass lambda as template parameter to other functions, lambdas are inlined into calling code.


1 Answers

Funcp func =      std::bind(&A::func, &a, std::placeholders::_1, std::placeholders::_2); 
like image 113
ltjax Avatar answered Oct 02 '22 16:10

ltjax