7
我想要一个函数BindFirst
,它绑定函数的第一个参数,而不必使用std ::占位符明确地知道/声明函数的参数。我希望客户端代码看起来像这样。绑定函数的第一个参数而不知道它的参数
#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
?
我可以知道为什么'的std :: decay'是用过的? – billz
@billz因为这里我们想存储传递给'BindFirst'的参数的副本(可能是移动构造的)。你当然不想存储引用,他们的常量/不稳定性在这里是你感兴趣的。说,对于'T && = int &&'你想存储'int' –