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
What is the difference between them? I mean why the first swap is more expensive than the second one ?
The second one enable move for types that support it (ie having move assignment operator). Those types include standard vector, string, etc.
For basic types (int, bool, etc.) that does not support move semantic, there is no difference, it's a normal copy.