0

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).

1
  • The way I understand it moving an argument tears the guts out of the original. If you don't want to use the objects you're comparing again, this is ok. If you do use them again later this would belong in the class of Undefined Behavior. Commented Nov 17, 2018 at 3:03

1 Answer 1

5

The expressions lhs and rhs are lvalue expressions inside bool operator!=(foo&& lhs, foo&& rhs){. This is unrelated to the reference type of variables.

As the message says, a lvalue cannot be bound to a rvalue-reference and therefore the overload bool operator==(foo&& lhs, foo&& rhs){ cannot be considered.

If you want to get a rvalue from a lvalue, so it can bind to the rvalue-reference, you are supposed to use std::move:

bool operator!=(foo&& lhs, foo&& rhs){ return operator==(std::move(lhs), std::move(rhs)); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Also, operator!= needs to return the opposite of what operator== returns: return !operator==(...); If two objects are equal, they can't also be unequal.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.