Google Mock:模拟重载函数会创建警告 C4373
我正在使用 Google Mock 和 VS2010 模拟一个具有 2 个重载函数的 C++ 类:
#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));
// ...
};
每次我编译时收到以下警告两次:
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'
知道为什么吗?
这是正确的行为吗?
我怎样才能避免这种情况?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果这是新代码,那么应该没问题。 C4373 警告< /a> 表示旧版本的 Visual Studio 违反了标准。从链接的文档:
仅当您的代码依赖于 Visual Studio 的不正确行为时,这才会成为问题。
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:
This would only be a problem if you had broken code that relied on Visual Studio's incorrect behavior.
对我来说(在 VS 2010 中),在原始类型参数上指定 const(我看到你也有)导致了这种行为。每当我想要重写的基类函数中存在这样的情况时,我都无法以某种方式指定模拟,这样就不会出现此警告;当只有类类型 const 值/const 引用参数时,警告从未发生。
所以对我来说,这种情况下的警告实际上是编译器中的错误(因为签名完全相同)。
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).
建议的替代方法:
Suggested alternative approach:
我意识到这是一个老问题,但由于我现在自己偶然发现了它,我想分享我的解决方案(或至少解释):
问题很可能是您的声明有一个 const 参数,这将被编译器忽略。这是可以有效地使用 const 作为参数的定义。
现在 google mock faq 现在,为了消除警告,请删除
const
来自函数声明中的参数。就我而言,我发现它仍然很困难,因为函数实现是针对标头内的模板化类,其中声明和定义都是一起完成的。解决方案可能是在包含模拟类的标头时禁用警告。
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.