【发布时间】:2016-02-16 21:10:22
【问题描述】:
我想要一个函数BindFirst 绑定函数的第一个参数,而无需使用 std::placeholders 明确知道/声明函数的数量。我希望客户端代码看起来像这样。
#include <functional>
#include <iostream>
void print2(int a, int b)
{
std::cout << a << std::endl;
std::cout << b << std::endl;
}
void print3(int a, int b, int c)
{
std::cout << a << std::endl;
std::cout << b << std::endl;
std::cout << c << std::endl;
}
int main()
{
auto f = BindFirst(print2, 1); // std::bind(print2, 1, std::placeholders::_1);
auto g = BindFirst(print3, 1); // std::bind(print3, 1, std::placeholders::_1, std::placeholders::_2);
f(2);
g(2,3);
}
知道如何实现BindFirst 吗?
【问题讨论】:
标签: c++ c++11 functional-programming c++14 bind