Search code examples
c++googlemock

How to disambiguate a pointer and a reference version of a function?


We have 2 functions in an interface which looks almost the same in their foot print. The only difference is that one uses a pointer-variable and the other a reference. (We are in the process of deprecating the pointer solution, but since it still is used for now we need to support both in our mocks). Note: these interface functions are called from macros (due to compatibility with C)

A simplified implementation of them is:

#define MY_MACRO(my_var) \
do \
{ \
  INTF::detail::my_func(my_var) \
} \
while(false)

void my_func(my_type* my_var)

The macro and pointer implementation are existing code we now want to add reference support as follows:

void my_func(my_type& my_var)

In our mock for that interface we have implemented handlers for both:

ON_CALL(*this, my_func(testing::MATCHER<my_type*>()).WillByDefault(Invoke(p_fake, static_cast<void (Fake::*)(my_type*)>(&Fake::MY_MACRO_func)));
ON_CALL(*this, my_func(testing::MATCHER<my_type&>()).WillByDefault(Invoke(p_fake, static_cast<void (Fake::*)(my_type&)>(&Fake::MY_MACRO_func)));

This looked OK to us, but apparently is not since when a user does something like:

EXPECT_CALL(m_mockINF, MY_MACRO_func(_)).Times(0);

He/she gets an ambiguity error:

error: call of overloaded 'gmock_MY_MACRO_func(const testing::internal::AnythingMatcher&)' is ambiguous

What are we doing wrong? We looked with multiple people at this and don't see the (probably obvious, since that is usally the case in these kind of problems ;-) ) thing we are doing wrong.


Solution

  • Note problem is well documented:

    So just do this:

    EXPECT_CALL(m_mockINF, my_func(testing::An<my_type&>()).Times(0);
    

    Live demo: https://godbolt.org/z/nEGT5v8a8