1

I'm using Google Mock 1.6.0. When using the MOCK_METHODN macros, it seems to think I'm passing 3 arguments for methods that return a std::pair:

#include <gmock/gmock.h>
#include <utility>
class IFoo {
public:
    virtual std::pair<int, int> bar() = 0;
};
class MockFoo {
public:
    MOCK_METHOD0(bar, std::pair<int, int>());
};
int main() {
    MockFoo f;
    return 0;
}

Compiler output (GCC 4.6.3):

main.cpp:9:44: error: macro "MOCK_METHOD0" passed 3 arguments, but takes just 2
main.cpp:9:5: error: ‘MOCK_METHOD0’ does not name a type

This error doesn't appear if I:

  • Replace std::pair<int, int> with a simple type like int or void
  • Use on a method that has a std::pair argument, instead of returning it
congusbongus
  • 13,359
  • 7
  • 71
  • 99

2 Answers2

2

In FAQ GoogleMock:

You still need a typedef if the return type contains an unprotected comma, but that's much rarer.

Use typedef

Ily
  • 21
  • 4
1

This appears to be a bug with version 1.6.0. The same code works with version 1.7.0, so the best solution would be to upgrade to that.

Alternately, compiling with Clang also runs into the same error, but provides a strong clue where the error is:

main.cpp:9:38: error: too many arguments provided to function-like macro invocation
    MOCK_METHOD0(bar, std::pair<int, int>());
                                     ^
main.cpp:9:5: error: C++ requires a type specifier for all declarations
    MOCK_METHOD0(bar, std::pair<int, int>());
    ^~~~~~~~~~~~
2 errors generated.

Looks like a buggy macro expansion is interpreting the , inside std::pair<int, int> as another argument. Therefore, you can also work around the issue by avoiding the ,, for instance using a typedef:

typedef std::pair<int, int> MyType;
class MockFoo {
public:
    MOCK_METHOD0(bar, MyType());
};
congusbongus
  • 13,359
  • 7
  • 71
  • 99
  • 1
    If upgrading to 1.7.0 isn't an option, here's a question with a number of workarounds: http://stackoverflow.com/questions/13842468/comma-in-c-c-macro. – Ian Feb 16 '15 at 21:56