We already know that the & parameter refers to the reference in C/C++ , but as of C++11 we can find the &&
and it refers to the value on the other side of the declaration.
More objectively we can say, for example, that:
Equals:
Of course, values and types are examples.
But in summary, one is the lvalue
and the other is the rvalue
.
In C++03 (and before), temps (called “rvalues”, as they often lie on the right side of an assignment) were designed to never be modifiable - just as in C - and were considered indistinguishable from const types T&
; , however, in some cases the temps may have been modified, a behavior that was even considered a useful loophole.
C++11 adds a new non-const reference type called the rvalue reference, identified by T&&
. Refers to temporaries that can be modified after they are initialized, for the purpose of allowing “semantic move”.
Using
&&
as a parameter, let’s look at the output:
That is, a pointer to an rvalue value, but the pointer to itself.
string
Of course this output will only be the value
I personally don’t see any use for this in the code I create, but this article is for us to know what it means when we see a double ampersand in some code and understand how they are used and what values are returned.
And in summary, as there is in the description of this syntax , C++11 managed to solve the problem of unnecessary copies of C++03 and earlier, but it should be used with care .
That’s all for today, small daily doses that will always keep us in tune with C++!