rvalue reference

An rvalue reference is a reference that will bind only to a temporary object. The primary reason for adding rvalue references is move semantics. Unlike traditional copying, moving means that a target object pilfers the resources of the source object, leaving the source in an "empty" state. In certain cases where making a copy of an object is both expensive and unnecessary, a move operation can be used instead. The C++11 Standard Library uses move semantics extensively. Many algorithms and containers are now move-optimized.
Prior to C++11, if you had a temporary object, you could use a "regular" or "lvalue reference" to bind it, but only if it was const:
```
#include <iostream>
#include <string>
using namespace std;
 
string getName() {
    return "abc";
} 
 
int main()
{
    const string& name1 = getName(); // ok
    string& name2 = getName(); // Not ok
}
```
The intuition here is that you cannot use a "mutable" reference because, if you did, you'd be able to modify some object that is about to disappear, and that would be dangerous. Notice, by the way, that holding on to a const reference to a temporary object ensures that the temporary object isn't immediately destructed. This is a nice guarantee of C++, but it is still a temporary object, so you don't want to modify it.
In C++11, however, there's a new kind of reference, an "rvalue reference", that will let you bind a mutable reference to an rvalue, but not an lvalue. In other words, rvalue references are perfect for detecting if a value is temporary object or not. Rvalue references use the && syntax instead of just &, and can be const and non-const, just like lvalue references, although you'll rarely see a const rvalue reference (as we'll see, mutable references are kind of the point):
```
const string&& name = getName(); // ok
string&& name = getName(); // also ok - praise be!
```
R-values references cannot be initialized with l-values.
Rvalue reference | modifiable l-values | non-modifiable l-values | R-values
--- | ---
can be initialized with | No | No | Yes
can modify | No | No | Yes

Rvalue reference to const| modifiable l-values | non-modifiable l-values | R-values
--- | ---
can be initialized with | No | No | Yes
can modify | No | No | No

R-value references have two properties that are useful. First, r-value references extend the lifespan of the object they are initialized with to the lifespan of the r-value reference (l-value references to const objects can do this too). Second, non-const r-value references allow you to modify the r-value!
R-value references are more often used as function parameters. This is most useful for function overloads when you want to have different behavior for l-value and r-value arguments.
You should almost never return an r-value reference, for the same reason you should almost never return an l-value reference. In most cases, you’ll end up returning a hanging reference when the referenced object goes out of scope at the end of the function.