Google Mock:模拟重载函数会创建警告 C4373

发布于 2024-10-11 05:56:51 字数 832 浏览 5 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暖心男生 2024-10-18 05:56:51

如果这是新代码,那么应该没问题。 C4373 警告< /a> 表示旧版本的 Visual Studio 违反了标准。从链接的文档:

之前的编译器版本
Visual C++ 2008 将函数绑定到
基类中的方法,然后
发出警告消息。随后的
编译器的版本会忽略
const 或 volatile 限定符,绑定
函数到派生中的方法
类,然后发出警告 C4373。这
后一种行为符合 C++
标准。

仅当您的代码依赖于 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:

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.

素手挽清风 2024-10-18 05:56:51

对我来说(在 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).

尬尬 2024-10-18 05:56:51

建议的替代方法:

#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));
    // ...
};

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));
    // ...
};
终止放荡 2024-10-18 05:56:51

我意识到这是一个老问题,但由于我现在自己偶然发现了它,我想分享我的解决方案(或至少解释):

问题很可能是您的声明有一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文