jeudi 28 avril 2016

move semantics inside assignment operator - side effects, destruction

Forcing Move Semantics

So in a sense, we have drifted into the netherworld of non-deterministic destruction here: a variable has been assigned to, but the object formerly held by that variable is still out there somewhere. That's fine as long as the destruction of that object does not have any side effects that are visible to the outside world. But sometimes destructors do have such side effects. An example would be the release of a lock inside a destructor. Therefore, any part of an object's destruction that has side effects should be performed explicitly in the rvalue reference overload of the copy assignment operator:

X& X::operator=(X&& rhs)
{

  // Perform a cleanup that takes care of at least those parts of the
  // destructor that have side effects. Be sure to leave the object
  // in a destructible and assignable state.

  // Move semantics: exchange content between this and rhs

  return *this;
}

I get that l-value's original object normally is destructed - eventually - in a NON-assignment situation.

But, in an assignment operator, why would you want the same behavior?

Aucun commentaire:

Enregistrer un commentaire