As far as I understand one of the purposes of adding move semantics is to optimize code by calling special constructor for copying "temporary" objects. For example, in this answer we see that it can be used to optimize such string a = x + y
stuff. Because x+y is an rvalue expression, instead of deep copying we can copy only the pointer to the string and the size of the string. But as we know, modern compilers support return value optimization, so without using move semantics our code will not call the copy constructor at all.
To prove it I write this code:
#include <iostream>
struct stuff
{
int x;
stuff(int x_):x(x_){}
stuff(const stuff & g):x(g.x)
{
std::cout<<"copy"<<std::endl;
}
};
stuff operator+(const stuff& lhs,const stuff& rhs)
{
stuff g(lhs.x+rhs.x);
return g;
}
int main()
{
stuff a(5),b(7);
stuff c = a+b;
}
And after executing it in VC++2010 and g++ in optimize mode I'm getting empty output.
What kind of optimization is it, if without it my code still works faster? Could you explain what I'm understanding wrong?
After some digging I find this excellent example of optimization with rvalue references inStroustrup's FAQ .
Yes, swap function:
This will generate optimized code for any kind of types (assuming, that it have move constructor).
Edit: Also RVO can't optimize something like this(at least on my compiler):
This function always calls copy constructor (checked with VC++). And if our class can be moved faster, than with move constructor we will have optimization.
Move semantics should not be thought as an optimization device, even if they can be used as such.
If you are going to want copies of objects (either function parameters or return values), then RVO and copy elision will do the job when they can. Move semantics can help, but are more powerful than that.
Move semantics are handy when you want to do something different whether the passed object is a temporary (it then binds to a rvalue reference) or a "standard" object with a name (a so called const lvalue). If you want for instance to steal the resources of a temporary object, then you want move semantics (example: you can steal the contents a
std::unique_ptr
points to).Move semantics allow you to return non copyable objects from functions, which is not possible with the current standard. Also, non copyable objects can be put inside other objects, and those objects will automatically be movable if the contained objects are.
Non copyable objects are great, since they don't force you to implement an error-prone copy constructor. A lot of the time, copy semantics do not really make sense, but move semantics do (think about it).
This also enables you to use movable
std::vector<T>
classes even ifT
is non copyable. Thestd::unique_ptr
class template is also a great tool when dealing with non copyable objects (eg. polymorphic objects).Imagine your stuff was a class with heap allocated memory like a string, and that it had the notion of capacity. Give it a operator+= that will grow the capacity geometrically. In C++03 this might look like:
Also imagine you want to add more than just two stuff's at a time:
For me this prints out:
I count 3 allocations and 2 deallocations to compute:
Now add move semantics:
...
Running again I get:
I'm now down to 2 allocations and 1 deallocations. That translates to faster code.
Your posted example only takes const lvalue references and so explicitly cannot have move semantics applied to it, as there is not a single rvalue reference in there. How can move semantics make your code faster when you implemented a type without rvalue references?
In addition, your code is already covered by RVO and NRVO. Move semantics apply to far, far more situations than those two do.
This line calls the first constructor.
Plus operator is called using explicit common lvalue references.
Inside operator overload method, you have no copy constructor called. Again, the first constructor is called only.
assigment is made with RVO, so no copy is need. NO copy from returned object to 'c' is need.
Due no
std::cout
reference, compiler take care about yourc
value is never used. Then, whole program is stripped out, resulting in a empty program.Another good example I can think of. Imagine that you're implementing a matrix library and write an algorithm which takes two matrices and outputs another one:
Note that you can't pass U and V by const reference, because the algorithm tweaks them. You could theoretically pass them by reference, but this would look gross and leave
U
andV
in some intermediate state (since you callTransform(U)
), which may not make any sense to the caller, or just not make any mathematical sense at all, since it's just one of the internal algorithm transformations. The code looks much cleaner if you just pass them by value and use move semantics if you are not going to useU
andV
after calling this function: