C++gmock在命名空间中找不到方法参数的运算符==



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

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

#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名称空间,则编译成功。如果我为Param2添加模板包装器,该包装器在其运算符==中调用模板运算符==,则编译成功。

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

解决方案是添加:

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

testing::internal它是gmock中的一个命名空间,其中调用了运算符==。上面的代码指示此命名空间在全局命名空间中查找运算符==。

相关内容

  • 没有找到相关文章

最新更新