How to use c++11 move semantics to append vector contents to another vector?

Just do: #include <iterator> #include <algorithm> // … void MoveAppend(std::vector<X>& src, std::vector<X>& dst) { if (dst.empty()) { dst = std::move(src); } else { dst.reserve(dst.size() + src.size()); std::move(std::begin(src), std::end(src), std::back_inserter(dst)); src.clear(); } } If dst is empty, a move-assignment from src to dst will do the job – that will be as cheap as it can … Read more

Can I move-assign a std::map’s contents into another std::map?

HINT: Read the update first! The current C++11 standard and the C++14 draft do not provide a member function to enable this feature. As lavr suggested you can still write m.insert(make_move_iterator(begin(temp)), make_move_iterator(end (temp))); which will move the values from the source container into the destination container. However, neither the container nodes nor the keys will … Read more

Does a default virtual destructor prevent compiler-generated move operations?

Yes, declaring any destructor will prevent the implicit-declaration of the move constructor. N3337 [class.copy]/9: If the definition of a class X does not explicitly declare a move constructor, one will be implicitly declared as defaulted if and only if X does not have a user-declared copy constructor, X does not have a user-declared copy assignment … Read more

What is the behaviour of compiler generated move constructor?

does std::is_move_constructible<T>::value == true implies that T has a usable move constructor? Either a move constructor or a copy constructor. Remember that the operation of copy construction satisfies all the requirements that are placed upon the operation move construction, and some more. In Standard terms, a MoveConstructible object is one for which the evaluation of … Read more

Is specializing std::swap deprecated now that we have move semantics? [duplicate]

The specialization of std::swap is now optional, but not deprecated. The rationale is performance. For prototyping code, and perhaps even for much shipping code, std::swap will be plenty fast. However if you’re in a situation where you need to eek out every little bit from your code, writing a custom swap can still be a … Read more

copy vs std::move for ints

In this example, there is no difference. We will end up with 3 ints with value 100. There could definitely be a difference with different types though. For instance, let’s consider something like vector<int>: std::vector<int> a = {1, 2, 3, 4, 5}; // a has size 5 auto a_copy = a; // copy a. now … Read more

Why is passing by value (if a copy is needed) recommended in C++11 if a const reference only costs a single copy as well?

Consider calling the various options with an lvalue and with an rvalue: Dog::Dog(const std::string &name) : _name(name) {} Whether called with an lvalue or rvalue, this requires exactly one copy, to initialize _name from name. Moving is not an option because name is const. Dog::Dog(std::string &&name) : _name(std::move(name)) {} This can only be called with … Read more