I'm working with std::bind but I still don't get how it works when we use it with member class functions.
If we have the following function:
double my_divide (double x, double y) {return x/y;}
I understand perfectly well the next lines of code:
auto fn_half = std::bind (my_divide,_1,2); // returns x/2
std::cout << fn_half(10) << '\n'; // 5
But now, with the following code where we have a bind to member function I have some questions.
struct Foo {
void print_sum(int n1, int n2)
{
std::cout << n1+n2 << '\n';
}
int data = 10;
};
Foo foo;
auto f = std::bind(&Foo::print_sum, &foo, 95, _1);
f(5);
-
Why the first argument is a reference? I'd like to get a theoretical explanation.
-
The second argument is a reference to the object and it's for me the most complicated part to understand. I think it's because
std::bindneeds a context, am I right? Is always like this? Hasstd::bindsome sort of implementation to require a reference when the first argument is a member function?
Aucun commentaire:
Enregistrer un commentaire