vendredi 22 mai 2015

For what is the move constructor and move assignment constructor in Unique Pointers needed?

I've got a simplified example of a unique_ptr. I wonder for what the move constructor and move assignment operator in the unique pointer is needed? If I understand move constructors (and passing rvalues) right, these two lines of code should result the same.

UniquePointer<T> a(new T);
UniquePointer<T> a(UniquePointer<T>(new T));

And here is the simplified UniquePointer code:

template<typename T> class UniquePointer {
    T* m_ptr;
public:
    UniquePointer(const UniquePointer&) = delete;
    UniquePointer& operator=(const UniquePointer&) = delete;
    UniquePointer(UniquePointer&& rhs);
    UniquePointer& operator=(UniquePointer&& rhs);
    UniquePointer(T* ptr) : m_ptr(ptr) { }
    T* operator->() const { return m_ptr; }
    T& operator*() const { return *m_ptr; }
    T* get() const { return m_ptr; }
    ~UniquePointer() { delete m_ptr; }
};

Aucun commentaire:

Enregistrer un commentaire