What is rvalue reference in C++11
In this article we will discuss what is rvalue reference and how it is different from lvalue reference.
lvalue references
Prior to C++11 we only had references i.e. A Reference variable is an alias that always points to a an existing variable i.e.
1 |
int x = 7; |
1 |
int & refX = x; // refX is a reference |
With C++11 this reference has become lvalue reference and it can refer to lvalues only i.e.
1 |
int & lvalueRef = x; // lvalueRef is a lvalue reference |
Here, lvalueRef is a lvalue reference pointing to a lvalue x. A lvalue reference can not point to a rvalue i.e.
1 |
int & lvalueRef2 = (x+1); // COMPILE Error - lvalue Reference Can't point to rvalue |
Here lvalueRef2 is a lvalue reference, so it cannot point to a rvalue.
rvalue Reference
rvalue references are introduced in C++11 and rvalue references can do what lvalue references fails to do i.e. a rvalue reference can refer to rvalues.
Declaring rvalue reference
To declare a rvalue reference, we need to specify two & operator i.e. &&.
1 |
int && rvalueRef = (x+1); // rvalueRef is rvalue reference |
Here, rvalueRef is rvalue reference which is pointing to a rvalue i.e. (x+1).
Let’s see an another example,
1 2 3 4 |
int getData() { return 9; } |
getData() is a rvalue. So, only rvalue reference can refer to it. If lvalue reference will try to refer to it then will result in compile error i.e.
1 |
int & lvalueRef3 = getData(); // Compile error - lvalue Reference Can't point to rvalue |
Although const lvalue reference can refer to temporary object returned by getData() but as its a const reference, so we can not modify this temporary.
1 |
const int & lvalueRef3 = getData(); // OK but its const |
But a rvalue reference can refer to rvalue without const i.e.
1 |
int && rvalueRef2 = getData(); |
Now, as we understand the rvalue references, the next question that comes in mind is, What was the need of rvalue references in C++11 ?
Well, we will discuss it in next articles.
Follow @thisPtr
Very Simple and precise…