简体   繁体   English

如何让自定义运营商==使用Google Test?

[英]How to get a custom operator== to work with Google Test?

I'm having trouble using a custom overloaded '==' operator with PCL and Google Test (GTest) 我在使用PCL和Google Test(GTest)的自定义重载'=='运算符时遇到问题

#include <pcl/point_types.h>
namespace pcl { struct PointXYZ; }

bool operator==(pcl::PointXYZ p1, pcl::PointXYZ p2) {return p1.x-p2.x<.1;}
#include <gtest/gtest.h>

TEST(Foo, bar) {
  pcl::PointXYZ a{2,3,4};
  pcl::PointXYZP b{2,3,4};
  EXPECT_EQ(a,b); // Compile error no match for operator==
}

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

The error I get is: 我得到的错误是:

|| /usr/include/gtest/gtest.h: In instantiation of 'testing::AssertionResult testing::internal::CmpHelperEQ(const char*, const char*, const T1&, const T2&) [with T1 = pcl::PointXYZ; T2 = pcl::PointXYZ]':
/usr/include/gtest/gtest.h|1361 col 23| required from 'static testing::AssertionResult testing::internal::EqHelper<lhs_is_null_literal>::Compare(const char*, const char*, const T1&, const T2&) [with T1 = pcl::PointXYZ; T2 = pcl::PointXYZ; bool lhs_is_null_literal = false]'
src/foo/src/tests.cpp|20 col 3| required from here
/usr/include/gtest/gtest.h|1325 col 16| error: no match for 'operator==' (operand types are 'const pcl::PointXYZ' and 'const pcl::PointXYZ')
||    if (expected == actual) {
||                 ^
/usr/include/gtest/internal/gtest-linked_ptr.h|213 col 6| note: candidate: template<class T> bool testing::internal::operator==(T*, const testing::internal::linked_ptr<T>&)
||  bool operator==(T* ptr, const linked_ptr<T>& x) {
||       ^
/usr/include/gtest/internal/gtest-linked_ptr.h|213 col 6| note:   template argument deduction/substitution failed:
/usr/include/gtest/gtest.h|1325 col 16| note:   mismatched types 'T*' and 'pcl::PointXYZ'

I tried to adhere to the primer: https://github.com/google/googletest/blob/master/googletest/docs/primer.md#binary-comparison 我试着坚持使用这个底漆: https//github.com/google/googletest/blob/master/googletest/docs/primer.md#binary-comparison

In particular, my operator is defined before including gtest, and I'm sure the types match up. 特别是,我的运算符在包含gtest之前定义,我确定类型匹配。 I also tried writing the overload to take const references, but that just compared the addresses instead of the values. 我也尝试编写重载来获取const引用,但这只是比较了地址而不是值。

Operators on custom types are found through argument dependent lookup. 通过参数依赖查找找到自定义类型的运算符。

Argument dependent lookup (in a nutshell) means that functions may be considered if they are defined in the same namespace as one or more of their arguments. 依赖于参数的查找(简而言之)意味着如果函数在与其一个或多个参数相同的命名空间中定义,则可以考虑这些函数。

This code: 这段代码:

namespace pcl { struct PointXYZ; }

bool operator==(pcl::PointXYZ p1, pcl::PointXYZ p2) {return p1.x-p2.x<.1;}

defines PointXYZ in the pcl:: namespace, but defines the operator== function in the global namespace. pcl:: namespace中定义PointXYZ ,但在全局命名空间中定义operator== function。 Hence, not a candidate for ADL. 因此, 不是 ADL的候选人。

doing this: 这样做:

namespace pcl { 
  struct PointXYZ; 
  bool operator==(pcl::PointXYZ p1, pcl::PointXYZ p2) {return p1.x-p2.x<.1;}
}

Fixes that because now the operator== has the name pcl::operator== which is in the same namespace as one of its arguments (actually in this case both of them, but you get the idea). 修复了因为现在运算符==的名称为pcl::operator== ,它与其参数之一位于同一名称空间中(实际上在这种情况下都是两个,但你明白了)。 This makes it a candidate during ADL and thus it will be selected when gtest invokes the equality test. 这使它成为ADL期间的候选者,因此当gtest调用相等性测试时将选择它。

Include the operator== definition within the namespace pcl : namespace pcl包含operator==定义:

#include <pcl/point_types.h>
namespace pcl
{
    bool operator==(pcl::PointXYZ p1, pcl::PointXYZ p2) {return p1.x-p2.x<.1;}
}

Besides, you can remove the forward declaration for pcl::PointXYZ since it must be complete in header pcl/point_types.h . 此外,您可以删除pcl::PointXYZ的前向声明,因为它必须在头文件pcl/point_types.h Otherwise, compiler would complain when defining variables within TEST . 否则,编译器会在TEST定义变量时抱怨。

If not defined, you will also have to define operator<<(std::ostream&, const pcl::PointXYZ&) so that Google Test can print out your values when the equality assertion fails. 如果未定义,则还必须定义operator<<(std::ostream&, const pcl::PointXYZ&)以便Google Test可以在相等断言失败时打印出您的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM