achen7 / googlemock

Automatically exported from code.google.com/p/googlemock
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

mocking functions with const arguments causes C4373 warning (MSVC 2010) #141

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
IMPORTANT NOTE: PLEASE send issues and requests to
http://groups.google.com/group/googlemock *instead of here*.
This issue tracker is NOT regularly monitored.

If you really need to create a new issue, please provide the information
asked for below.

What steps will reproduce the problem?
1. Make an interface class with method like 
virtual void Func(const int param);
2. Make a mock for it using
MOCK_CONST_METHOD1(Func, void(const int));
3. Compile using MSVC 2010 with Warnings level 3 on.

What is the expected output? What do you see instead?
waring C4373 is arisen

Which version of Google Mock are you using? On what operating system?
1.6.0

Please provide any additional information below.

Original issue reported on code.google.com by yere...@gmail.com on 19 May 2011 at 2:27

GoogleCodeExporter commented 9 years ago
Google Mock code is correct here.

Warning C4373 is a compatibility warnings. Older versions of VC++ did not 
comply with the standard. The new version is compliant, but the behavior of C++ 
programs written originally to the older versions may have changed. This 
warning helps you find out when that happens. But if you don't have that 
warning occurringe elsewhere in your code, simply disable it. Google C4373 for 
more information.

Original comment by vladlosev on 19 May 2011 at 5:13

GoogleCodeExporter commented 9 years ago
According the documentation this warning happens when the signitures differ.
struct Base
{
    virtual void f(int i) {
        printf("base\n");
    }
};
struct Derived : Base
{
    void f(const int i) {  // C4373
        printf("derived\n");
    }
};

The question it then: why is google mock creating methods with a different 
signature (i.e. dropping the const) in this case - given the signature in the 
base class and the value given to the MOCK_MACRO are the same.

C4373 shouldn't happen if signatures are the same - but google mock is causing 
this in the case.
This still looks like a bug in google mock.
MOCK_METHOD1(Func, void(const int));

is generating 
void Func(int), not void Func(const int)

Original comment by malcolm....@str.com.au on 20 May 2011 at 1:45

GoogleCodeExporter commented 9 years ago
The MOCK_METHOD macros operate on function types, and C++ considers types T(U) 
and T(const U) equivalent unless U is a reference type.

Original comment by vladlosev on 20 May 2011 at 2:13

GoogleCodeExporter commented 9 years ago
Is then a flaw with C++, especially C++ type forwarding (or similar)?

Original comment by malcolm....@str.com.au on 20 May 2011 at 2:54

GoogleCodeExporter commented 9 years ago
There is no flaw. The const parameter makes sense only inside the function. It 
doesn't matter for the caller and is thus not part of the function's type.

Original comment by vladlosev on 20 May 2011 at 6:32