2012-03-31 6 views
3

하나의 메소드 인터페이스와 해당 인터페이스를 조롱하는 클래스가 있습니다. 이 메소드는 단일 인수를 취합니다. 인수가 std::pair<Something, Something> 인 경우에만 컴파일이 실패합니다. 저는 MSVC 2010을 사용하고 있습니다. 문제는 컴파일러 나 STL 구현에 따라 달라질 수 있습니다. 물론 문제가 wetware와 관련이없는 경우가 가장 좋습니다. 나는 명백한 것을 놓치고 있어야합니다. 나노 탐침처럼.Google Mocked 멤버 함수의 std :: pair 인수가 컴파일되지 않습니다.

#include <gmock/gmock.h> 

class BorgInterface 
{ 
public: 
    typedef std::pair<int, long> MyBorg; // <--- MyBorg is problematic! 
    //typedef long MyBorg; // ..but this MyBorg complies 
    virtual void Assimilate(MyBorg borg_in_training) = 0; 
}; 

class MockBorg 
    : public BorgInterface 
{ 
public: 
    MOCK_METHOD1(Assimilate, void(BorgInterface::MyBorg borg_in_training)); 
}; 

/*TEST(MyBorgTestCase, BorgInterfaceTest) 
{ 
    using ::testing::_; 

    MockBorg funny_borg; 
    EXPECT_CALL(funny_borg, Assimilate(_)); 
    // ...etc. (irrelevant) 
}*/ 

오류가 발생하면 실제 테스트 케이스에서 주석을 제거하지 않아도됩니다.

지금은 std::pair<>struct으로 묶어서이 문제를 해결했지만이 방법은 차선책입니다.

오류 메시지의 길이는 다소 불행한 일이지만, 도움이 될 수

1>Build started 3/31/2012 4:02:43 PM. 
1>ClCompile: 
1> test_pair_parameter_mock.cpp 
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\tuple(127): 
    error C2664: 'std::pair<_Ty1,_Ty2>::pair(const std::pair<_Ty1,_Ty2> &)' 
     : cannot convert parameter 1 from 'int' to 'const std::pair<_Ty1,_Ty2> &' 
1>   with 
1>   [ 
1>    _Ty1=int, 
1>    _Ty2=long 
1>   ] 
1>   Reason: cannot convert from 'int' to 'const std::pair<_Ty1,_Ty2>' 
1>   with 
1>   [ 
1>    _Ty1=int, 
1>    _Ty2=long 
1>   ] 
1>   No constructor could take the source type, 
      or constructor overload resolution was ambiguous 
1>   c:\...\microsoft visual studio 10.0\vc\include\tuple(404) 
       : see reference to function template instantiation 
       'std::tr1::_Cons_node<_Car,_Cdr>::_Cons_node< 
        _Ty1&,_Ty2&,std::tr1::_Nil&,std::tr1::_Nil&, 
        std::tr1::_Nil&, 
        ............... 
        std::tr1::_Nil&, 
        std::tr1::_Nil&>(_Farg0,...,_Farg9)' being compiled 
1>   with 
1>   [ 
1>    _Car=BorgInterface::MyBorg, 
1>    _Cdr=std::tr1::_Tuple_type< 
        std::tr1::_Nil, 
        .............. 
        std::tr1::_Nil, 
        std::tr1::_Nil>::_Type, 
1>    _Ty1=int, 
1>    _Ty2=long, 
1>    _Farg0=int &, 
1>    _Farg1=long &, 
1>    _Farg2=std::tr1::_Nil &, 
1>    ....................... 
1>    _Farg9=std::tr1::_Nil & 
1>   ] 
1>   d:\...\gmock\include\gmock\gmock-generated-function-mockers.h(97) : 
       see reference to function template instantiation 
       'std::tr1::tuple<_Arg0>::tuple<int,long>(
        std::pair<_Ty1,_Ty2> &)' being compiled 
1>   with 
1>   [ 
1>    _Arg0=BorgInterface::MyBorg, 
1>    _Ty1=int, 
1>    _Ty2=long 
1>   ] 
1>   d:\...\gmock\include\gmock\gmock-generated-function-mockers.h(92) : 
       while compiling class template member function 
       'void testing::internal::FunctionMocker<Function>::Invoke(A1)' 
1>   with 
1>   [ 
1>    Function=void (BorgInterface::MyBorg), 
1>    A1=BorgInterface::MyBorg 
1>   ] 
1>   d:\..\myapp\src\tests\unit_tests\test_pair_parameter_mock.cpp(17) : 
       see reference to class template instantiation 
       'testing::internal::FunctionMocker<Function>' being compiled 
1>   with 
1>   [ 
1>    Function=void (BorgInterface::MyBorg) 
1>   ] 
1> 
1>Build FAILED. 

답변

1

실제로 컴파일러 문제 같은데; 이것은 gcc 4.6을 사용하여 OK를 컴파일합니다. 당신이 부스트를 사용하는 것이 행복하면 당신이 원하는 경우 또는, 당신은 boost::tuple

typedef boost::tuple<int, long> MyBorg; 
+0

감사합니다. Google Mock에 대한 버그 신고서를 제출할 것을 고려하고 있었지만 최근에야 사용하기 시작한 이래로 내 잘못이라고 생각했습니다. – irobot

1

std::pair을 대체 할 수

virtual void Assimilate(const MyBorg *borg_in_training) = 0; 

을 :

더 간단한 해결 방법은 const를 포인터로 MyBorg을 통과하는 것 문제에 대한 전체 설명은 a bug report on this issue with a detailed discussion and investigation입니다. 쌍을 직접 가져 오는 튜플 생성자 때문에 VS2010에만 해당됩니다. 관련 참고

는 튜플 < 쌍 < T0를 구성하기 위해 노력하고, T1> 한 쌍의 < T0에서 T1은> 다시 T0을 할당 을 시도하는 위에서 언급 한 최초의 생성자를 호출에 결과> < T0, T1>과 쌍을 이루어 오류를 산출합니다.

용액을 튜플 < T0에서 인수 일치 T0와 T1 로 주어진 쌍 < T0, T1> 만 T0와 T1 경우 쌍 생성자로부터 튜플을 사용하는 것, T1은>로 구성된다.

VS2010의 STL 구현의 결함으로 VS2012의 STL 구현에서 수정되었습니다.

pair< T0, T1 > 매개 변수가 하나만있는 것을 피하기 위해 함수 서명에 기본 매개 변수를 추가하여 문제를 해결했습니다. 결과적으로 문제가되는 생성자를 피할 수 있습니다.

관련 문제