When using std::bind
to bind a member function, the first argument is the objects this
pointer. However it works passing the object both as a pointer and not.
See for example the following program:
#include <iostream>
#include <functional>
struct foo
{
void bar(int v) { std::cout << "foo::bar - " << v << '\n'; }
};
int main()
{
foo my_foo;
auto f1 = std::bind(&foo::bar, my_foo, 1);
auto f2 = std::bind(&foo::bar, &my_foo, 2);
f1();
f2();
}
Both clang and GCC compiles this without complaints, and the result works for both binds:
foo::bar - 1 foo::bar - 2
I have been trying to wrap my head around the specification (section 20.8.9) but it's one of the places where it's far from clear to me.
Should only one be correct, or are both correct?
std::move
with aunique_ptr
to transfer ownership into the callable object returned bybind
. It doesn't work withweak_ptr
because you can't dereference aweak_ptr
– Jonathan Wakely