我使用boost库测试框架创建了一个单元测试,并在使用该库时遇到了使用std::bind占位符时遇到的问题。
如果我显式地使用std::placeholders:: + _1,它可以很好地工作:
std::bind(&TestFixture::concatStrings, this, std::placeholders::_1, std::placeholders::_2)但是,如果省略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使用lambda是我能想到的最好的解决方案:
[&](const std::string& x, const std::string& y){ return concatStrings(x, y); }我只想了解使用在std中定义的方法是否与boost库冲突,例如std::bind。提前谢谢。
发布于 2017-11-18 09:15:22
如果我显式地使用std::占位符::+ _1,它工作得很好:
因此,如果您正确使用它,并且如文档所示,它可以工作。
但是,如果省略std::占位符::并直接使用_1,则会导致编译错误:
如果你不正确地使用它,它就不起作用。
我只想了解使用在std中定义的方法是否与boost库冲突,例如std::bind。
是的,有个命名冲突。这本身并没有冲突,但不幸的是,Boost Bind将它们的占位符放在全局命名空间中。
完全有可能同时使用Boost Bind ::_1、Boost Boost、Boost perfectly boost::spirit::qi::_1和std::bind,但是是的,您可能必须对占位符进行限定。或者,使用自己的别名。
PS。在这种情况下,您应该能够使用std::mem_fn。如果您使用lambdas,_prefer就不要使用[&],因为这是一个不安全的习惯。在您的情况下,您只需要捕获[this]或如果您愿意,[=]。
https://stackoverflow.com/questions/47361534
复制相似问题