【问题标题】:C++ gmock cannot find operator== for method argument in namespaceC ++ gmock在命名空间中找不到方法参数的运算符==
【发布时间】:2020-10-20 16:47:59
【问题描述】:

我无法使用 gcc 6.4 编译以下代码。 gmock 版本是 1.8.1。

也许这里的 operator== 看起来很奇怪,但由于它的大小,我无法附上完整的代码。

#include <iostream>
#include <gmock/gmock.h>

template<class T>
constexpr bool operator==(T&&, T&&) noexcept
{
    return true;
}

namespace NNNN
{

struct Param2
{
    int i;
};

} // namespace NN

using ::testing::_;
using ::testing::Return;
using ::testing::AtMost;


struct Param
{
    int i;
};

struct A
{
    MOCK_METHOD1(f, int(const Param&));
};

struct B
{
    MOCK_METHOD1(f, int(const NNNN::Param2&));
};


TEST(Test, test)
{
    A a;
    EXPECT_CALL(a, f(Param{ 1 }));

    B b;
    std::cout << (NNNN::Param2{ 1 } == NNNN::Param2{ 2 }) << std::endl;
    EXPECT_CALL(b, f(NNNN::Param2{ 1 }));
}

int main(int argc, char** argv)
{
    ::testing::InitGoogleMock(&argc, argv);
    return RUN_ALL_TESTS();
}

我得到的错误:

In file included from /usr/local/lib/googletest/1.8.1/lib/pkgconfig/../../include/gmock/gmock-spec-builders.h:71:0,
                 from /usr/local/lib/googletest/1.8.1/lib/pkgconfig/../../include/gmock/gmock-generated-function-mockers.h:44,
                 from /usr/local/lib/googletest/1.8.1/lib/pkgconfig/../../include/gmock/gmock.h:62,
                 from /home/jzldfb/projects/uc_workspace/ultracruise/rte/utils/configuration/daemon/sub_module/test/TestApi2.cpp:7:
/usr/local/lib/googletest/1.8.1/lib/pkgconfig/../../include/gmock/gmock-matchers.h: In instantiation of 'bool testing::internal::AnyEq::operator()(const A&, const B&) const [with A = NNNN::Param2; B = NNNN::Param2]':
/usr/local/lib/googletest/1.8.1/lib/pkgconfig/../../include/gmock/gmock-matchers.h:1083:18:   required from 'bool testing::internal::ComparisonBase<D, Rhs, Op>::Impl<Lhs>::MatchAndExplain(Lhs, testing::MatchResultListener*) const [with Lhs = const NNNN::Param2&; D = testing::internal::EqMatcher<NNNN::Param2>; Rhs = NNNN::Param2; Op = testing::internal::AnyEq]'
/home/jzldfb/projects/uc_workspace/ultracruise/rte/utils/configuration/daemon/sub_module/test/TestApi2.cpp:60:1:   required from here
/usr/local/lib/googletest/1.8.1/lib/pkgconfig/../../include/gmock/gmock-matchers.h:238:60: error: no match for 'operator==' (operand types are 'const NNNN::Param2' and 'const NNNN::Param2')
   bool operator()(const A& a, const B& b) const { return a == b; }

如果我删除 NNNN 命名空间,则编译成功。 如果我在其 operator== 中为调用模板 operator== 的 Param2 添加模板包装器,则编译成功。

【问题讨论】:

标签: c++ namespaces operator-keyword googlemock


【解决方案1】:

感谢您将我发送到 ADL 页面。

解决方法是添加:

namespace testing {
namespace internal {
    using ::operator==;
} }

testing::internal 它是 gmock 中调用 operator== 的命名空间。 上面的代码指示此命名空间在全局命名空间中查找 operator==。

【讨论】:

  • 在声明它的代码之外使用internal 或detail 命名空间对我来说总是很可疑。我宁愿在NNNN 命名空间中声明operator==(const Param2&amp;, const Param2&amp;),这样ADL 才能正常工作。此外,将operator== 与为其定义的类位于同一命名空间中也是一种很好的编码习惯。如果您的生产代码中不需要operator==,那么我会声明MATCHER_P 仅用于测试目的。
  • 当然是有问题的。问题是我的operator== 是模板并处理具有特定SFINAE 属性的所有类,因此无法在具体命名空间中定义它。真正的实现与我提供的示例不同。在这种情况下,这是 gmock 中的一个错误。例如 gtest 包含using ::operator&lt;&lt;;
猜你喜欢
  • 2018-10-19
  • 2019-07-16
  • 2010-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多