mardi 4 août 2020

Rvalue references, copy and move

Consider following section of code for swap :

//for some type T
void swap(T& a, T& b) {
    T temp = a;
    a = b;
    b = temp;
}

and this one below :

//for some type T
void swap(T& a, T& b) {
    T temp = static_cast<T&&>(a);
    a = static_cast<T&&>(b);
    b = static_cast<T&&>(temp);
}

I have following doubts : What is the difference between them? I mean why the first swap is more expensive than the second one ?

Reference : The c++ programming language 7.7.2

Aucun commentaire:

Enregistrer un commentaire