lundi 2 octobre 2017

swap function when swapped object don't implement move semantics

I'm reading about move semantics in c++ and encountered the following example as motivation:

template<class T> 
void swap(T& a, T& b) 
{ 
  T tmp(std::move(a));
  a = std::move(b); 
  b = std::move(tmp);
} 

It said here that if T doesn't implement move semantics (in copy constructor and assignment operator), then swap will behave as "normal" swap:

template<class T>
void swap(T& a, T& b) 
{ 
  T tmp(a);
  a = b; 
  b = tmp; 
}

Why is that true? It's said here (last paragraph) that because std::move(b) is rvalue, we can't call the assignment operator with signature Type& operator=(Type&) when doing a = std::move(b);.

Aucun commentaire:

Enregistrer un commentaire