std::move()
in C++
is written to avoid copy
.
For example, the following two swap
functions behave differently:
template <class T>
swap1(T& a, T& b) {
Ttmp(a); // have two copies of a.
a = b; // become two copies of b (one of the copies of + a disappears)
b = tmp; // 2 copies of tmp (one of the copies of +b disappears)
}
If you use //move, you can swap without copying
template <class T>
swap2(T& a, T& b) {
T tmp(std::move(a));
a = std::move(b);
b = std::move(tmp);
}
© 2024 OneMinuteCode. All rights reserved.