Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get this pointer from std::function?

Since std::function can hold member functions, so it must store a pointer to the object instance somewhere.

How can I fetch the this pointer from a std::function that holds a member function?

like image 936
danijar Avatar asked Apr 01 '13 16:04

danijar


1 Answers

An object of type std::function holds a callable object. A pointer to member function is a kind of callable object; it can be called with an argument of the appropriate class type, plus any additional arguments that it needs. For example:

struct S {
    void f(int);
};
std::function<void(S, int)> g(&S::f);

To call it, pass an object of type S:

S s;
g(s, 3);

Note that the std::function object does not hold an S object; it's only when you call it that the function pointer gets bound to an object.

like image 152
Pete Becker Avatar answered Oct 18 '22 12:10

Pete Becker