[英]How to use std::bind with std::function in order to pass method as a callback?
I found this answer on SO我在 SO 上找到了这个答案
https://stackoverflow.com/a/40944576/5709159 https://stackoverflow.com/a/40944576/5709159
I made everything like in answer, but I get an error我在回答中做了所有事情,但我收到一个错误
there is my code有我的代码
Method where I need to pass my callback我需要传递回调的方法
/*static*/ void Utils::copy_files(std::function<void(int, int)> progress_callback,
std::string const & path_from,
std::string const & path_to)
{
....
}
My callback implementation我的回调实现
void TV_DepthCamAgent::progress_callback(int count, int copied_file)
{
...
}
Usage用法
void TV_DepthCamAgent::foo()
{
...
auto callback = std::bind(&TV_DepthCamAgent::progress_callback,
this);
shared::Utils::copy_files(callback, path_from_copy, path_to_copy);
...
}
There is an error that I get我得到了一个错误
SE0312 no suitable user-defined conversion from "std::_Binder" to "std::function" exists SE0312 不存在从“std::_Binder”到“std::function”的合适的用户定义转换
Error C2664 'void shared::Utils::copy_files(std::function,const std::string &,const std::string &)': cannot convert argument 1 from 'std::_Binder' to 'std::function'错误 C2664“void shared::Utils::copy_files(std::function,const std::string &,const std::string &)”:无法将参数 1 从“std::_Binder”转换为“std::function” '
What am I doing wrong?我究竟做错了什么?
You miss placeholders:你错过了占位符:
auto callback = std::bind(&TV_DepthCamAgent::progress_callback,
this,
std::placeholders::_1,
std::placeholders::_2);
but simpler, IMO, is to use lambda:但更简单的,IMO,是使用 lambda:
auto callback = [this](int count, int copied_file){
return this->progress_callback(count, copied_file);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.