Before C ++ 11, it was always the case that the copy assignment operator should always follow a const reference, for example:
template <typename T> ArrayStack<T>& operator= (const ArrayStack& other);
However, with the introduction of assignment operators and move constructors, it seems that some people advocate using pass by value instead of assigning a copy. You must also add a move destination operator:
template <typename T> ArrayStack<T>& operator= (ArrayStack other); ArrayStack<T>& operator= (ArrayStack&& other);
The above implementation of the statement is as follows:
template <typename T> ArrayStack<T>& ArrayStack<T>::operator =(ArrayStack other) { ArrayStack tmp(other); swap(*this, tmp); return *this; } template <typename T> ArrayStack<T>& ArrayStack<T>::operator =(ArrayStack&& other) { swap(*this, other); return *this; }
Is it good to use pass by value when creating a copy assignment operator for C ++ 11? Under what circumstances should I do this?
c ++ assignment-operator c ++ 11 move-semantics
Mantracker
source share