【问题标题】:std::pair argument in Google Mocked member function fails to compileGoogle Mocked 成员函数中的 std::pair 参数无法编译
【发布时间】:2012-03-31 14:29:19
【问题描述】:

我有一个方法接口和一个模拟该接口的类。该方法采用单个参数。只有当该参数是std::pair<Something, Something> 类型时,它才会编译失败。我正在使用 MSVC 2010,因此问题可能是编译器或 STL 实现特定的,当然,除非问题与湿件相关,这是我的最佳猜测。我一定遗漏了一些明显的东西。就像纳米探针一样。

#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&lt;&gt; 包装在 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.

【问题讨论】:

    标签: c++ visual-c++ compiler-errors googlemock std-pair


    【解决方案1】:

    看起来确实是编译器问题;这可以使用 gcc 4.6 编译。

    更简单的解决方法是通过指向 const 的指针传递 MyBorg

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

    或者如果您乐于使用 Boost,您可以将 std::pair 替换为 boost::tuple

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

    【讨论】:

    • 谢谢!我正在考虑为 Google Mock 提交错误报告,但由于我最近才开始使用它,我认为这更有可能是我的错。
    【解决方案2】:

    如果您想了解该问题的完整说明,请访问 a bug report on this issue with a detailed discussion and investigation。它特定于 VS2010,因为元组构造函数直接采用一对。相关说明是

    试图从一对中构造一个元组>结果 在调用上面提到的第一个构造函数时,它依次尝试 将 T0 分配给对,产生错误。

    解决方案是仅从对构造函数中启用元组 如果作为参数给出的对 中的 T0 和 T1 匹配 T0 和 T1 来自正在构造的元组

    这是VS2010的STL实现中的一个缺陷,已经在VS2012的STL实现中修复了。

    我通过将默认参数添加到函数签名以避免使用单个 pair&lt; T0, T1 &gt; 参数,成功解决了这个问题。这导致有问题的构造函数被避免。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-26
      • 2015-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-21
      相关资源
      最近更新 更多