I'm implementing comparison operator overloads for a particular class foo:
class foo{ public: foo() {}; }; bool operator==(foo&& lhs, foo&& rhs){ // ... return true; } bool operator!=(foo&& lhs, foo&& rhs){ return operator==(lhs,rhs); } However, when calling the != operator, I get the following compilation error:
tester.cpp: In function ‘bool operator!=(foo&&, foo&&)’: tester.cpp:37:27: error: no matching function for call to ‘operator==(foo&, foo&)’ return operator==(lhs,rhs); ^ tester.cpp:33:6: note: candidate: ‘bool operator==(foo&&, foo&&)’ <near match> bool operator==(foo&& lhs, foo&& rhs){ ^~~~~~~~ tester.cpp:33:6: note: conversion of argument 2 would be ill-formed: tester.cpp:37:24: error: cannot bind rvalue reference of type ‘foo&&’ to lvalue of type ‘foo’ return operator==(lhs,rhs); ^~~ Which seems strange to me because the == operator overload takes rvalue references as arguments, so why exactly is the compiler trying to dereference them?
PS: I understand I could solve this by just passing the objects as const &, but for design purposes rvalue references would make more sense (foo is a nested helper class no supposed to be instantiated outside base class definition).