How to get this pointer from std :: function?

Since std::function can contain member functions, so it should store a pointer to an instance of the object somewhere.

How can I get the this pointer from std::function , which contains a member function?

+5
c ++ this this-pointer std-function
Apr 01 '13 at 16:53
source share
1 answer

An object of type std::function contains the called object . A pointer to a member function is a kind of called object; it can be called with an argument of the corresponding class type, as well as 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 has no S object; this is only when you call it so that the function pointer binds to the object.

+8
Apr 01 '13 at 17:05
source share



All Articles