Search code examples
c++operator-overloadingrvalue-referencervalue

C++ - What does it mean when you put an ampersand (&) in front of an rvalue reference?


So I have this code:

foo<T>& foo<T>::operator=(foo&& M) 
{
  if (this != &M)
  {
    // Do something
  }
}

and I am quite confused as to what this != &M means here. What happens when you try to get the memory address of a rvalue reference? Is that what this code is trying to do?

  1. What does this != &F do exactly? What does it check?
  2. What is the purpose of using an rvalue reference as parameter for an assignment operator?

Sorry if this is dumb. I am quite new to C++ and I am trying to learns how rvalue references work and why they are used in cases like this.

Thanks!


Solution

  • It's simply checking for self-assignment. If the address of the object being passed in is the same as this then do nothing.