why is the destructor call after the std::move necessary?

Moving from an object just means that the moved-from object might donate its guts to live on in another live object shortly before it is [probably] going to die. Note, however, that just because an object donated its guts that the object isn’t dead! In fact, it may be revived by another donating object and … Read more

Why does Visual Studio not perform return value optimization (RVO) in this case

If the code looks like it should be optimized, but is not getting optimized I would submit bug here http://connect.microsoft.com/VisualStudio or raise a support case with Microsoft. This article, although it is for VC++2005 (I couldn’t find a current version of document) does explain some scenarios where it won’t work. http://msdn.microsoft.com/en-us/library/ms364057(v=vs.80).aspx#nrvo_cpp05_topic3 If we want to … Read more

Move or Named Return Value Optimization (NRVO)?

The compiler may NRVO into a temp space, or move construct into a temp space. From there it will move assign x. Update: Any time you’re tempted to optimize with rvalue references, and you’re not positive of the results, create yourself an example class that keeps track of its state: constructed default constructed moved from … Read more

Is the pass-by-value-and-then-move construct a bad idiom?

Expensive-to-move types are rare in modern C++ usage. If you are concerned about the cost of the move, write both overloads: void set_a(const A& a) { _a = a; } void set_a(A&& a) { _a = std::move(a); } or a perfect-forwarding setter: template <typename T> void set_a(T&& a) { _a = std::forward<T>(a); } that will … Read more

Reusing a moved container?

From section 17.3.26 of the spec “valid but unspecified state”: an object state that is not specified except that the object’s invariants are met and operations on the object behave as specified for its type [ Example: If an object x of type std::vector<int> is in a valid but unspecified state, x.empty() can be called … Read more