这两者有什么区别吗?还是我的安全,以取代所有出现boost::bind的std::bind在我的代码,从而消除对加速的依赖?
当我想创建一个用于包装work(..)成员的std :: function时,我遇到了一个让我疲惫的编译错误.
示例代码:
class C{
public:
C(){
std::function<void(void) > f = std::bind(&C::work,
this,
std::bind(&C::step1, this),
std::bind(&C::step2, this));
QList<decltype(f)> lst;
lst.append(f);
.....
}
private:
void work(std::function<bool()> fn1, std::function<bool()> fn2 ) {
if (fn1()) {
QTimer::singleShot(1, fn2);
}
else {
QTimer::singleShot(5, fn1);
}
}
bool step1(){return true;}
bool step2(){return true;}
};
Run Code Online (Sandbox Code Playgroud)
编译错误:
main.cpp:49: erreur : conversion from 'std::_Bind_helper<false, void (C::*)(std::function<bool()>, std::function<bool()>), C* const, std::_Bind<std::_Mem_fn<bool (C::*)()>(C*)>, std::_Bind<std::_Mem_fn<bool (C::*)()>(C*)> >::type {aka std::_Bind<std::_Mem_fn<void (C::*)(std::function<bool()>, std::function<bool()>)>(C*, std::_Bind<std::_Mem_fn<bool (C::*)()>(C*)>, std::_Bind<std::_Mem_fn<bool (C::*)()>(C*)>)>}' to non-scalar type 'std::function<void()>' requested …Run Code Online (Sandbox Code Playgroud) bind将函数对象创建bind为函数的参数时,我遇到了问题.
我有两个函数fa和fb.fa将函数作为参数.我将fa函数(fb在这种情况下)绑定到一个闭包中.我用两种不同的方式来做,如下所示.第一个工作,第二个不编译.
(我知道,我不需要绑定标记的行,因为没有参数.但当然这只是显示行为的最小例子,在实践中我将绑定参数fb)
void fa(function<void()> f){
f();
}
void fb(){
}
void test_which_compiles() {
auto bb = fb; // *** QUESTIONABLE LINE ***
auto aa = std::bind(fa, bb);
aa();
}
void test_which_fails() {
auto bb = std::bind(fb); // *** QUESTIONABLE LINE ***
auto aa = std::bind(fa, bb);
aa(); // COMPILE ERROR HERE
}
Run Code Online (Sandbox Code Playgroud)
调用时发生编译错误aa().
因此,当我将一个函数对象绑定为一个本身由bind创建的参数时,编译器似乎不接受.
编译错误是一长串我不认为与该行直接相关的东西.我只给出第一行:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/type_traits:3476:13:错误:没有匹配函数来调用'__invoke'__invoke(_VSTD :: declval <_Fp>(),_ VSTD :: declval …