2017-11-18 9 views
2

부스트 라이브러리 테스트 프레임 워크를 사용하여 단위 테스트를 만들고이 라이브러리와 함께 std :: bind 자리 표시자를 사용하는 데 문제가 발생했습니다.boost 라이브러리와 함께 std :: bind 자리 표시 자 사용 문제

std::bind(&TestFixture::concatStrings, this, std::placeholders::_1, std::placeholders::_2) 

을하지만이 std::placeholders:: 직접이 _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 

람다를 사용하여 , 내가 생각할 수있는 최상의 솔루션입니다 :

[&](const std::string& x, const std::string& y){ return concatStrings(x, y); } 

std :: bind와 같이 boost 라이브러리와 std 충돌로 정의 된 메소드를 사용하는 경우를 이해하고 싶습니다. 미리 감사드립니다.

답변

2

If I explicitly use std::placeholders:: + _1, it works fine:

올바르게 사용하면 문서화 된대로 작동합니다.

But if I omit the std::placeholders:: and directly use _1, it results to a compilation error:

잘못 사용하면 작동하지 않습니다.

I just want to understand if using methods defined in std conflicts with boost library, such as the std::bind.

네, 충돌이 있습니다. 그 자체로 갈등은 없지만, 불행하게도 Boost Bind는 역사적으로 자리 표시자를 전역 네임 스페이스에 넣습니다.

부스트 바인드 ::_1, 부스트 피닉스, 부스트 스피릿 boost::spirit::qi::_1std::bind을 함께 사용할 수는 있지만 네, 자리 표시자를 정규화해야 할 수도 있습니다. 또는 별칭을 사용하십시오.


추신. 이 경우에는 std::mem_fn을 사용할 수 있어야합니다. lambdas _prefer를 사용하지 않는 경우 [&]을 사용하는 것은 안전하지 않은 습관입니다. 귀하의 경우에는 [this] 또는 원하는 경우 [=] 만 캡처하면됩니다.

+0

안녕하세요! 답장을 보내 주셔서 감사합니다. '_1'을 사용하면 boost :: bind에서 잘 작동합니다. 비록 std :: bind를 명시 적으로 사용하려고한다면'std :: placeholders :: _ 1'을 사용해야합니다. 람다에서 캡처를 지적 해 주셔서 감사합니다, 나는 안전하게 캡처를 선택하는 습관을 만들 것입니다. :) – cawols