TLDR: How does std::bind()
actually work when calling a member function with an instance of a class or a this
pointer of the class?
Notes:
this
is implicitly used as the first argument when calling a member functionstd::placeholder
works(instance.*ptr)()
Here is the code:
#include <functional>
#include <iostream>
struct Test {
void test(const std::string &info) { std::cout << info << std::endl; }
};
int main() {
Test test;
// call with instance
std::bind(&Test::test, std::placeholders::_1, "call with instance")(test);
// call with `this`
std::bind(&Test::test, std::placeholders::_1,
"call with `this` pointer")(&test);
}
This code works fine on my platform. So, I guess, either std::bind()
has done the job to distinguish an instance and a pointer, or I misunderstood something.
As this document mentions, when f
of INVOKE(f, t1, t2, ..., tN)
is a pointer to member function of class T
:
t1
is an instance of class T
or base class of T
, it will be equivalent to: (t1.*f)(t2, ..., tN)
t1
is a specialization of std::reference_wrapper
, it will be equivalent to: t1.get().*f
(*t1).*f
For more details, please read the doc.