Google中的std :: pair参数Mocked成员函数无法编译
我有一个方法接口和一个模拟该接口的类。该方法只有一个参数。只有当参数类型为std::pair<Something, Something>
时,才会编译失败。我正在使用MSVC 2010,所以可能的问题是编译器或STL实现的具体情况,除非这个问题与湿件相关,这是我最好的猜测。我必须失去一些明显的东西。像纳米探针一样。Google中的std :: pair参数Mocked成员函数无法编译
#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.
看起来像一个编译器的问题确实;这使用gcc 4.6编译OK。
一个更简单的解决办法是通过指针传递MyBorg
为const:
virtual void Assimilate(const MyBorg *borg_in_training) = 0;
,或者如果你乐于使用升压,你可以用boost::tuple
typedef boost::tuple<int, long> MyBorg;
取代std::pair
如果你想有一个充分说明问题,有a bug report on this issue with a detailed discussion and investigation。它特定于VS2010,因为它直接使用一对元组构造函数。相关说明是
试图构造元组<对< T0,T1>>从一对< T0,T1>在所提到的第一个构造上述这又试图 分配T0呼叫导致 配对< T0,T1>,产生错误。
该解决方案将是使从一对的构造元组只有 如果T0和T1从一对< T0,T1>从所述元组< T0给出作为参数匹配T0和T1 ,T1>被构造。
这是VS2010的STL实现中的缺陷,并已在VS2012的STL实现中修复。
我成功地解决这个问题曾通过添加默认参数的函数签名,以避免单一pair< T0, T1 >
参数。这导致避免了有问题的构造函数。
谢谢!我正在考虑提交错误报告谷歌模拟,但自从我开始使用它只是在最近,我认为这更可能是我的错。 – irobot 2012-03-31 20:43:11