I have created a Matrix class that does some basic math and such stuff. This class has defined copy and move constructors and operators.What I have noticed is that when I use syntax like (for example):

No (assuming "C" is a function local automatic variable, just like in the OP), the standard demands the attempt to automatically treat the object "C" as if it was an rvalue (and thus the move ctor to be called here -- or, if we enable optimizations, copy elision may occur, which is even better than moving), see again: http://stackoverflow.../9532647/859774

When the criteria for elision of a copy operation are met or would be met save for the fact that the source object is a function parameter, and the object to be copied is designated by an lvalue, overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue. If overload resolution fails, or if the type of the first parameter of the selected constructor is not an rvalue reference to the object’s type (possibly cv-qualified), overload resolution is performed again, considering the object as an lvalue. [ Note: This two-stage overload resolution must be performed regardless of whether copy elision will occur. It determines the constructor to be called if elision is not performed, and the selected constructor must be accessible even if the call is elided.

This means it will first try to move, and then copy. I wonder if copy constructor has side effects, it might bite someone.

In short: sometimes it has to be used (e.g., unique_ptr example), sometimes it can be used to enable move optimizations (e.g., some moving-out and moving-in cases), sometimes it shouldn't be used (e.g., some moving-out cases, just like in your example where it disables copy elision), sometimes it can't be used (e.g., non-moveable objects)

For a longer version, see "Moving from lvalues" (demonstrating where it has to be used for std::unique_ptr), "Moving into members", "Implementation of move":http://stackoverflow...11540204/859774 // linked before

Misery, I just wanted to drop in and admit (and apologize too of course) that I remember answering one of your questions earlier about returning copy-expensive values and while I did introduce move semantics to you, I also said you had to move() the result. I couldn't find much useful information about rvalue references so my self-taught knowledge from experimenting suggested that was the case. Obviously, I screwed up that testing somewhere and got the wrong impression

Matt, your link collection in your first post was quite nice and did clear some small things up a little for me.

Another thing to keep in mind is why you'd want to move it. If your matrix class is flat and contains the data itself, there's no point in moving it.

Moving for optimization's sake only ever makes sense when a class holds pointers to data that it owns on the heap. In such case the move result in only the pointer being moved and thus the heap data stays put and is now owned by the newly located object. In comparison: a proper copy/assignment operation should copy all the data pointed to as well.

But if the class contains all its data directly it will have to move (copy really) each value in the class to the new location anyway. It's the same procedure whether you copy it or not and you wont get any performance gain. Only reason to use move in this case would be to make absolutely sure that the data is unique to one instance, but since it's a matrix class this wouldn't make sense.

@Brother Bob: At the very moment of your answer, then it helped me very very much, because I didn't even know about move constructors and assignment operators. Thanks to you I really pushed forward my lib, because I was able to choose the philosophy for the class/function interfaces. And I am very grateful for your help. And now replacing std::move to " " in a few files is not a big problem

Just a side note about that though. If you're doing this to make use of move semantics and gain performance, you might actually not get much out of it. The indirection caused by the pointer may actually take more time due to cache misses than copying a small matrix, like 4x4, that's already present in the cache. The performance would be obvious in larger matrices of course. I recommend you do some tests, if this is for optimization.