给定一个函数,f(x, y, z)我们可以绑定x到0,获取一个函数g(y, z) == f(0, y, z).我们可以继续这样做并获得h() = f(0, 1, 2).
用C++语法
#include <functional>
#include <iostream>
void foo(int a, long b, short c)
{
std::cout << a << b << c << std::endl;
}
int main()
{
std::function<void(int, long, short)> bar1 = foo;
std::function<void(long, short)> bar2 = std::bind(bar1, 0, std::placeholders::_1, std::placeholders::_2);
std::function<void(short)> bar3 = std::bind(bar2, 1, std::placeholders::_1);
std::function<void()> bar4 = std::bind(bar3, 2);
bar4(); // prints "012"
return 0;
}
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.
现在说我想做同样的事情 …