Код: Выделить всё
// same for move, can handle both with `(cls other)`
cls& operator=(const cls& other) {
cls temp{ other };
swap(*this, other);
return *this;
}
< /code>
std::swapКод: Выделить всё
template
void swap(T& lhs, T& rhs) {
T tmp{ std::move(lhs); } // move ctor
lhs = std::move(rhs); // move assign
rhs = std::move(tmp); // move assign
} // temp dtor
< /code>
While clearly not helping in assignment which constructs a new temporary, sticking an if (&lhs == &rhs) { return; }разрешает ли стандарт это, или он говорит, что (2+1) движения всегда выполняются? Есть ли причины, почему это не сработает?
Подробнее здесь: https://stackoverflow.com/questions/797 ... self-check
Мобильная версия