C++ 将std::bind占位符与boost库一起使用时出现问题
我使用boost库测试框架创建了一个单元测试,在使用std::bind占位符和所述库时遇到了一个问题 如果我显式地使用C++ 将std::bind占位符与boost库一起使用时出现问题,c++,boost,C++,Boost,我使用boost库测试框架创建了一个单元测试,在使用std::bind占位符和所述库时遇到了一个问题 如果我显式地使用std::占位符::+\u 1,它可以正常工作: std::bind(&TestFixture::concatStrings, this, std::placeholders::_1, std::placeholders::_2) 但是,如果我省略std::placeholder::并直接使用\u 1,则会导致编译错误: Error 1 error C2664
std::占位符::
+\u 1,它可以正常工作:
std::bind(&TestFixture::concatStrings, this, std::placeholders::_1, std::placeholders::_2)
但是,如果我省略std::placeholder::
并直接使用\u 1
,则会导致编译错误:
Error 1 error C2664: 'std::string std::_Pmf_wrap<std::string (__thiscall TestFixture::* )(const std::string &,const std::string &),std::string,TestFixture,const std::string &,const std::string &>::operator ()(_Farg0 &,const std::string &,const std::string &) const' : cannot convert argument 2 from 'boost::arg<1>' to 'const std::string &' C:\APPS\msvs2013\VC\include\functional 1149 1 test
我只是想了解使用std中定义的方法是否与boost库冲突,例如std::bind。提前谢谢
如果我显式地使用std::占位符::+_1,它可以正常工作:
因此,如果您正确地使用它,并且如文档所示,它是有效的
但是如果省略std::placeholders::并直接使用_1,则会导致编译错误:
Error 1 error C2664: 'std::string std::_Pmf_wrap<std::string (__thiscall TestFixture::* )(const std::string &,const std::string &),std::string,TestFixture,const std::string &,const std::string &>::operator ()(_Farg0 &,const std::string &,const std::string &) const' : cannot convert argument 2 from 'boost::arg<1>' to 'const std::string &' C:\APPS\msvs2013\VC\include\functional 1149 1 test
如果你用错了,它就不起作用了
我只是想了解使用std中定义的方法是否与boost库冲突,例如std::bind
是的,有命名冲突。这本身并没有冲突,但不幸的是,Boost Bind将它们的占位符放在了全局名称空间中
完全可以同时使用Boost Bind::_1
、Boost Phoenix、Boost SpiritBoost::Spirit::qi:_1
和std::Bind
,但是,您可能必须限定占位符。或者,使用您自己的别名
另外,在这种情况下,您应该能够使用
std::mem\u fn
。如果你使用lambdas,你最好不要使用[&]
,因为这是一种不安全的习惯。在您的情况下,您只需要捕获[此]
,或者如果您愿意,[=]
您好!谢谢你的回复。使用\u 1
可以很好地与boost::bind配合使用。但是,如果我要显式使用std::bind,我应该使用std::占位符::_1
。感谢您指出lambda中的捕获物,我会养成安全选择捕获物的习惯。:)