当使用 std::function 保存 std::binder 的返回值时,需要注意所有被 placeholder 占用的参数位置都不应该再出现再 std::function 的模板列表里。
c++ - std::function and std::bind return value - Stack Overflow
using namespace std::placeholders;
std::function<bool(int)> f;
std::function<bool(int,int)> f1;
bool func(int i ,int j){
return i>j;
}
int k = 10;
f = std::bind(func,k,_2); //正确
f1 = std::bind(func,k,_2); //编译报错
f1 = std::bind(func,k,10); //正确,因为没有 placeholder 消耗参数列表位置
placeholder 会消耗掉 参数列表的位置,因此使用 std::function 保存 bind 返回值的时候需要再模板列表中去掉相应位置(placeholder位置)的参数类型。