I have a simple class which contains an std::vector, and I would like to benefit from move semantics (not RVO) when returning the class by value.
I implemented the move constructor, copy constructor and copy assignment operators in the following way:
class A { public: // MOVE-constructor. A(A&& other) : data(std::move(other.data)) { } // COPY-constructor. A(const A& other) : data(other.data) { } // COPY-ASSIGNMENT operator. A& operator= (const A& other); { if(this != &other) { data = other.data; } return *this; } private: std::vector<int> data; }; Are the above implementations correct?
And an other question: do I even have to implement any of these members, or are they auto-generated by the compiler? I know that the copy-constructor and the copy-assignment operator are generated by default, but can the compiler auto-generate the move constructor as well? (I compile this code both with MSVC and GCC.)
Thanks in advance for any suggestions. (I know that there already are some similar questions, but not for this exact scenario.)