c++ - How to get a custom operator== to work with Google Test? -
i'm having trouble using custom overloaded '==' operator 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 operator== } int main(int argc, char **argv){ testing::initgoogletest(&argc, argv); return run_all_tests(); }
the error 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 '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 here /usr/include/gtest/gtest.h|1325 col 16| error: no match 'operator==' (operand types 'const pcl::pointxyz' , '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*' , 'pcl::pointxyz'
i tried adhere primer: https://code.google.com/p/googletest/wiki/primer#binary_comparison
in particular, operator defined before including gtest, , i'm sure types match up. tried writing overload take const references, compared addresses instead of values.
operators on custom types found through argument dependent lookup.
argument dependent lookup (in nutshell) means functions may considered if defined in same namespace 1 or more of arguments.
this code:
namespace pcl { struct pointxyz; } bool operator==(pcl::pointxyz p1, pcl::pointxyz p2) {return p1.x-p2.x<.1;}
defines pointxyz
in pcl::
namespace, defines operator==
function in global namespace. hence, not candidate adl.
doing this:
namespace pcl { struct pointxyz; bool operator==(pcl::pointxyz p1, pcl::pointxyz p2) {return p1.x-p2.x<.1;} }
fixes because operator== has name pcl::operator==
in same namespace 1 of arguments (actually in case both of them, idea). makes candidate during adl , selected when gtest invokes equality test.
Comments
Post a Comment