【发布时间】: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