Liu*_*ibo 7 c++ std stdbind c++11
我有api功能f_api(std::function<void(int)> func),现在我有我的流程类
class Func {
public:
void operator()(int i) {
// do some work
}
int operator()(int i, int j) {
// do some other work
}
};
Run Code Online (Sandbox Code Playgroud)
我想用Func ff(int i)传递给f_api来完成工作; 所以我使用std :: bind
Func f;
std::function<void(int)> func = std::bind(&Func::operator(), &f, std::placeholders::_1);
f_api(func);
Run Code Online (Sandbox Code Playgroud)
但问题是,如何指出Func::operator()()我想要绑定哪个?我可以通过它的名称给出一个成员函数,但是当这个成员函数有多个不同的签名重新加载函数时,如何处理它呢?请问std :: bind能找到最适合调用的函数吗?C++太复杂了......
最小可验证的案例:
#include <iostream>
#include <functional>
using namespace std;
class Func {
public:
void operator()(int i) {
// do some work
cout << "i is: " << i << endl;
}
int operator()(int i, int j) {
// do some other work
}
};
void f_api(function<void(int)> f) {
f(3);
}
int main () {
Func f;
std::function<void(int)> func = std::bind(&Func::operator(), &f, std::placeholders::_1);
f_api(func);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译错误:
a.cpp: In function ‘int main()’:
a.cpp:23:91: error: no matching function for call to ‘bind(<unresolved overloaded function type>, Func*, const std::_Placeholder<1>&)’
std::function<void(int)> func = std::bind(&Func::operator(), &f, std::placeholders::_1);
^
a.cpp:23:91: note: candidates are:
In file included from a.cpp:2:0:
/usr/include/c++/4.8/functional:1655:5: note: template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__or_<std::is_integral<typename std::decay<_Tp>::type>, std::is_enum<typename std::decay<_Tp>::type> >::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...)
bind(_Func&& __f, _BoundArgs&&... __args)
^
/usr/include/c++/4.8/functional:1655:5: note: template argument deduction/substitution failed:
a.cpp:23:91: note: couldn't deduce template parameter ‘_Func’
std::function<void(int)> func = std::bind(&Func::operator(), &f, std::placeholders::_1);
^
In file included from a.cpp:2:0:
/usr/include/c++/4.8/functional:1682:5: note: template<class _Result, class _Func, class ... _BoundArgs> typename std::_Bindres_helper<_Result, _Func, _BoundArgs>::type std::bind(_Func&&, _BoundArgs&& ...)
bind(_Func&& __f, _BoundArgs&&... __args)
^
/usr/include/c++/4.8/functional:1682:5: note: template argument deduction/substitution failed:
a.cpp:23:91: note: couldn't deduce template parameter ‘_Result’
std::function<void(int)> func = std::bind(&Func::operator(), &f, std::placeholders::_1);
^
Run Code Online (Sandbox Code Playgroud)
在我的情况下,它有点不同,因为我class Func无法分配因为该类的一个成员字段不可分配,所以在编译时我会得到一个稍微不同的错误.
Sto*_*ica 16
你可以为所有重载做一个令人讨厌的漫长道路.通过铸造:
using sig1 = void (Func::*)(int);
using sig2 = void (Func::*)(int, int);
std::bind(static_cast<sig1>(&Func::operator()), &f, std::placeholders::_1);
Run Code Online (Sandbox Code Playgroud)
或者,std::bind如果你有lambda ,你可以认识到并不是那么有用:
std::function<void(int)> func = [&](int i) { f(i); };
Run Code Online (Sandbox Code Playgroud)
Dav*_*e S 10
Func是一个函数对象.不要使用成员函数指针,只需将整个对象交给绑定,并在使用绑定时让它解析,或者在这种情况下将其添加到std::function.
Func f;
std::function<void(int)> func = std::bind(f, std::placeholders::_1);
Run Code Online (Sandbox Code Playgroud)
甚至更好,只需将f指定给 std::function
Func f;
std::function<void(int)> func = f;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
692 次 |
| 最近记录: |