C++11 rvalues and move semantics confusion (return statement)

First example std::vector<int> return_vector(void) { std::vector<int> tmp {1,2,3,4,5}; return tmp; } std::vector<int> &&rval_ref = return_vector(); The first example returns a temporary which is caught by rval_ref. That temporary will have its life extended beyond the rval_ref definition and you can use it as if you had caught it by value. This is very similar to … Read more

push_back vs emplace_back

In addition to what visitor said : The function void emplace_back(Type&& _Val) provided by MSCV10 is non conforming and redundant, because as you noted it is strictly equivalent to push_back(Type&& _Val). But the real C++0x form of emplace_back is really useful: void emplace_back(Args&&…); Instead of taking a value_type it takes a variadic list of arguments, … Read more

What is move semantics?

I find it easiest to understand move semantics with example code. Let’s start with a very simple string class which only holds a pointer to a heap-allocated block of memory: #include <cstring> #include <algorithm> class string { char* data; public: string(const char* p) { size_t size = std::strlen(p) + 1; data = new char[size]; std::memcpy(data, … Read more