Should the Copy-and-Swap Idiom become the Copy-and-Move Idiom in C++11?

First of all, it is generally unnecessary to write a swap function in C++11 as long as your class is movable. The default swap will resort to moves: void swap(T& left, T& right) { T tmp(std::move(left)); left = std::move(right); right = std::move(tmp); } And that’s it, the elements are swapped. Second, based on this, the … Read more

C++11 virtual destructors and auto generation of move special functions

No, a defaulted destructor is still considered user defined, so it will prevent the generation of move operations. Also declare the move operations default-ed to make the compiler generate them. You need to only declare the move operations as default-ed in the base class. In the derived class, the destructor won’t be user defined anymore … Read more

Is std::array movable?

std::array is movable only if its contained objects are movable. std::array is quite different from the other containers because the container object contains the storage, not just pointers into the heap. Moving a std::vector only copies some pointers, and the contained objects are none the wiser. Yes, std::array uses the default move constructor and assignment … Read more

problems with Move constructor and Move overloaded assignment operator?

Your class A has several issues: Your assignment operator don’t handle self assignment and leak: A& A::operator=(const A& a) { std::cout<<“overload operator=\n”; if (this != &a) { p = a.p; delete[] s; s = new char[strlen(a.s) + 1]; strcpy(s, a.s); } return *this; } Your move doesn’t move but copy: A::A(A&& a) : p(a.p), s(a.s) … Read more