【发布时间】:2020-10-28 18:24:33
【问题描述】:
我有以下:
void print_str(std::shared_ptr<std::string> str) {
std::cout << str->c_str() << std::endl;
}
int main() {
auto str = std::make_shared<std::string>("Hello");
std::function<void()> f = std::bind(print_str, str);
f(); // correctly print: Hello
return 0;
}
我认为std::bind(print_str, str) 的类型是std::function<void(std::shared_ptr<std::string>)>,但上面的代码运行正常。 std::bind有什么技巧吗?
环境:centos、gcc82
【问题讨论】:
-
不,这不是
std::bind(print_str, str)的类型。毕竟生成的函数没有参数,原函数的唯一参数已经绑定,那么第二个参数从哪里来?
标签: c++ c++11 std-function stdbind