例えば、
#include <gmock/gmock.h> using ::testing::Eq; using ::testing::Return; using ::testing::Matcher; class X { public: bool operator==(const X& other) const { return false; } }; class Y { public: bool operator==(const Y& other) const { return false; } }; class Foo { public: virtual ~Foo() { } virtual void function(X x) { } virtual void function(Y x) { } }; class Mock : public Foo { public: MOCK_METHOD1(function, void(X)); MOCK_METHOD1(function, void(Y)); }; TEST(MockTest, Call_Foo_X) { Mock m; X arg; EXPECT_CALL(m, function(Eq(arg))).WillOnce(Return()); }これをコンパイルすると
$ g++ -I/opt/vendor/include -L/opt/vendor/lib -lgtest -lgmock -c ambiguous.cc ambiguous.cc:11: error: ISO C++ forbids declaration of ‘Interface’ with no type ambiguous.cc:23: error: multiple types in one declaration ambiguous.cc: In member function ‘virtual void MockTest_Call_Foo_X_Test::TestBody()’: ambiguous.cc:28: error: call of overloaded ‘gmock_function(testing::internal::EqMatcherambiguousですよ。ambiguous!)’ is ambiguous ambiguous.cc:19: note: candidates are: testing::internal::MockSpec & Mock::gmock_function(const testing::Matcher &) ambiguous.cc:20: note: testing::internal::MockSpec & Mock::gmock_function(const testing::Matcher &)
で、どうすっかって。
EXPECT_CALL(m, function(Matcher明示的にMatcherで型を示してあげると。(Eq(arg)))).WillOnce(Return());