Google Mock: Mocked overloaded functions create warning C4373
Asked Answered
M

4

9

I'm mocking a C++ class which has 2 overloaded functions using Google Mock and VS2010:

#include "stdafx.h"
#include "gmock/gmock.h"

#include "A.h"

class MockA : public A
{
public:
    // ...
    MOCK_METHOD3(myFunc, void(const int id, const int errorCode, const CString errorMsg));
    MOCK_METHOD1(myFunc, void(const CString errorMsg));
    // ...
};

Each time I compile I get the following warning twice:

1>c:\dev\my_project\tests\mocka.h(83): warning C4373: 'MockA::myFunc': virtual function overrides 'A::myFunc', previous versions of the compiler did not override when parameters only differed by const/volatile qualifiers
1>          c:\dev\my_project\my_project\include\a.h(107) : see declaration of 'A::myFunc'

Any idea why?
Is this correct behavior?
How can I avoid this?

Musteline answered 9/1, 2011 at 11:33 Comment(3)
Make sure you're using the right variant -- MOCK_CONST_METHOD should be used when A's method you're overriding is const.Lysimachus
@Billy ONeal - The method itself is not const, only its parameters are. Should I still use MOCK_CONST_METHOD ?Musteline
No; in that case you should do what you are doing :)Lysimachus
S
9

If this is new code, you should be fine. The C4373 warning is saying that old versions of Visual Studio violated the standard. From the linked documentation:

Versions of the compiler prior to Visual C++ 2008 bind the function to the method in the base class, then issue a warning message. Subsequent versions of the compiler ignore the const or volatile qualifier, bind the function to the method in the derived class, then issue warning C4373. This latter behavior complies with the C++ standard.

This would only be a problem if you had broken code that relied on Visual Studio's incorrect behavior.

Siracusa answered 9/1, 2011 at 11:40 Comment(4)
I sure hope you're right. Do you have a reference to back this up? Somewhere people discussing this is indeed past false behavior? Also - how then can I mute this specific warning?Musteline
@Jon, it's explained in the link. Note the last part, "This latter behavior complies with the C++ standard."Siracusa
thanks. I used the following to suppress the warning: #2542484Musteline
I don't understand, all const are in right places. And anyway the warning has been fired, and why only for some subset of methods?Reheat
A
8

For me (in VS 2010), specifying the const on primitive type parameters (which I see you also have) caused this behavior. Whenever such existed in the base class function I wanted to override, I couldn't specify the mock in a way so that this warning did not occur; when only having class type const value / const reference parameters, the warning never occured.

So to me it seems like the warning in that case is actually a mistake in the compiler (as the signatures are exactly the same).

Assured answered 30/7, 2013 at 14:25 Comment(1)
I had the error exactly in this case (const qualifier for a primitive). VS 2015 and gmock.1.10.0. Removing the const solved the problem.Gadolinite
R
1

Suggested alternative approach:

#include "stdafx.h"
#include "gmock/gmock.h"

#include "A.h"

class MockA : public A
{
public:
    // ...

    void myFunc(const int id, const int errorCode, const CString errorMsg) {
      mocked_myFunc3(id, errorCode, errorMsg);
    }

    void myFunc(const CString errorMsg) {
      mocked_myFunc1(errorMsg);
    }

    MOCK_METHOD3(mocked_myFunc_3, void(const int id, const int errorCode, const CString errorMsg));
    MOCK_METHOD1(mocked_myFunc_1, void(const CString errorMsg));
    // ...
};
Retha answered 20/6, 2014 at 11:18 Comment(0)
T
1

I realise this is an old question, but since I stumbled upon it myself now, I'd like to share my solution (or at least explanation):

The problem is likely that your declaration has a const parameter, which will be ignored by the compiler. It is the definition that may effectively use const for the parameter.

It's also mentioned now in the google mock faq now that, in order to get rid of the warning, remove const from the parameter in the function declaration.

In my case I found it still hard since the function implementation was for a templated class inside a header where declaration and definition happen are both done together. Solution to that is probably to disable the warning when including the mocked class' header.

Treasonous answered 19/7, 2018 at 7:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.