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 << '
'; }
};
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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…