To make it clearer what exactly && does, && 's compare three “classical” ways of passing an argument (this is precisely the use of “ampersands” that appear in the question):
- Transfer on the left-side link (
Type& ). This is the simplest case; only the pointer to the object is copied (and the link is at the lowest level). - Pass by value . In this case, the compiler creates a full copy of the object by allocating memory on the stack and calling the copy constructor .
And finally, the transfer on the right-hand link ( Type&& ). Here, the compiler also allocates space on the stack, however, it already causes the displacement constructor , whose task is to “move” all data from the original object to a new one, turning the first one into a “dummy” (safe from the point of view of lack of communication with the new object data).
Why was “move” written in quotes? Yes, because in fact a simple copying is performed with the original zoning.
The following question may arise: if it all comes down to simple copying with zeroing, why can't we do without the good old pass by value ? After all, we can assume that simply discarding the data of the original when it is destroyed is quite enough. The fact is that this is not always the case. A class can contain pointers (like, for example, std::vector ) or external resource descriptors (like std::fstream ). If you just copy the instances of these classes, we get:
- or “deep” copying of the entire buffer for
std::vector , - or two references to the same file descriptor for
std::fstream ; the destruction of one instance of a class will lead to the appearance of an incorrect descriptor in another.
*****) pointers. The concept of "link" came later :) - PinkTux