【发布时间】:2020-05-07 03:19:59
【问题描述】:
考虑下面的一些简单代码:
int f1(int a) {
std::cout << a << std::endl;
}
int main (int agrc, char* argv[]) {
std::function<int(int)> f = std::bind(&f1, std::placeholders::_1);
f(123);
return 0;
}
我已经阅读了一些关于 std::function 和 std::bind 的文档,但仍然不明白它是如何工作的。
编译器显示 std::bind 的调用返回一个 _Bind_helper 类型的对象,但是,我没有看到 std::function 类具有输入类型为 _Bind_helper 的构造函数,那么 std::function xxx = std 如何::绑定 xxx 工作?
【问题讨论】:
-
std::function有一个构造函数模板,采用任何可调用的仿函数类型。 -
顺便提一下:这里没有必要使用
std::bind。std::function<int(int)> f(f1);将做完全相同的事情,而没有std::bind增加的开销。
标签: c++ function c++11 stl bind