Why does reallocating a vector copy instead of moving the elements? [duplicate]

The short answer is that I think @BenVoigt is basically correct. In the description of reserve (§23.3.6.3/2), it says: If an exception is thrown other than by the move constructor of a non-CopyInsertable type, there are no effects. [And the description of resize in §23.3.6.3/12 requires the same.] This means that if T is CopyInsertable, … Read more

What is move_iterator for

If I understand it correct, a=std::move(b) binds reference a to the address of b. And after this operation the content that b points to is not guaranteed. Ah, no: a is not necessarily a reference. The above use of std::move also grants the compiler permission to call decltype(a)::operator=(decltype(b)&&) if it exists: such assignment operators are … Read more

Proper way (move semantics) to return a std::vector from function calling in C++11

If you’re using a C++0x-compatible compiler and standard library, you get better performance from the first example without doing anything. The return value of get_vector(_n, _m) is a temporary, and the move constructor for std::vector (a constructor taking an rvalue reference) will automatically be called with no further work on your part. In general, non-library … Read more

Why does C++ move semantics leave the source constructed?

In the “universe of move operations” there are four possibilities: target source is is left ———————————————————- constructed <– constructed // C++11 — move construction constructed <– destructed assigned <– constructed // C++11 — move assignment assigned <– destructed Each of these operations is useful! std::vector<T>::insert alone could make use of the first three. Though note: … Read more

Move the string out of a std::ostringstream

This is now possible with C++20, with syntax like: const std::string s = std::move(ss).str(); This is possible because the std::ostringstream class now has a str() overload that is rvalue-ref qualified: basic_string<charT, traits, Allocator> str() &&; // since C++20 This was added in P0408, revision 7, which was adopted into C++20. This is the exact approach … Read more

Default move constructor/assignment and deleted copy constructor/assignment

user-declared means either user-provided (defined by the user), explicitly defaulted (= default) or explicitly deleted (= delete) in contrast with implicitly defaulted / deleted (such as your move constructor). So in your case, yes the move constructor is implicitly deleted because the copy-constructor is explicitly deleted (and thus user-declared). However, in that particular case, how … Read more