C++ 对使用std::bind和Concurrency::parallel_时编译失败
在将一些使用boost库的旧代码迁移到C++11时遇到了一个奇怪的编译错误。具体来说,使用Visual Studio 2013 Professional Update 5 以下示例代码未能编译,错误为C3848。但是,如果将对std::bind的调用更改为C++ 对使用std::bind和Concurrency::parallel_时编译失败,c++,c++11,boost,ppl,C++,C++11,Boost,Ppl,在将一些使用boost库的旧代码迁移到C++11时遇到了一个奇怪的编译错误。具体来说,使用Visual Studio 2013 Professional Update 5 以下示例代码未能编译,错误为C3848。但是,如果将对std::bind的调用更改为boost::bind,则代码将按预期编译 #include< functional > #include< ppl.h > //#include< boost/bind.hpp > class foo {
boost::bind
,则代码将按预期编译
#include< functional >
#include< ppl.h >
//#include< boost/bind.hpp >
class foo {
public:
bool function() const {
return true;
}
};
int main(int argc, char* argv[]) {
foo f;
Concurrency::parallel_for(0, 5, std::bind(&foo::function, &f));
//Concurrency::parallel_for(0, 5, boost::bind(&foo::function, &f));
return 0;
}
#包括
#包括
//#包括
福班{
公众:
布尔函数()常量{
返回true;
}
};
int main(int argc,char*argv[]){
福福;
并发::并行_for(0,5,std::bind(&foo::function,&f));
//并发::并行_for(0,5,boost::bind(&foo::function,&f));
返回0;
}
我原以为std::bind
和boost::bind
完全可以互换,但事实似乎并非如此
有人能建议如何使用
std::bind
编译上述示例吗?他们都是在VS2015上编译的。在某些情况下,std::bind
并不能替代boost::bind
,因为后者会重载逻辑和关系运算符,而前者不会。还有一个boost::protect
,它阻止对嵌套的bind
进行评估,而std
版本没有这样的等价物。但是这些差异都不适用于您的示例@Grigoriy Chudnov试图使用std::bind(&foo::function,std::cref(f))代码>如建议的那样,但编译器仍返回错误C3848。是否有理由不在此处使用lambda?