In this blog post, you will learn the rvalue reference and its uses in C++ programming with the help of example code.
Before starting the article, I believe that you are already familiar with lvalue, rvalue, and references in C++ because these are the primary prerequisites to understanding this topic.
If you are not aware don’t worry before explaining the rvalue reference I will give a small introduction that helps you understand.
Because rvalue reference is one of the types of references introduced in C++11.
Note: A reference can be thought of as a name of an object.
After C++11, C++ supports two types of reference lvalue reference and rvalue reference. The lvalue reference is declared using the &
and the rvalue reference is declared using the &&
. Lvalue references and rvalue references are distinct types.
Syntax of Rvalue reference:
type-specifier-seq && attribute-specifier-seq
opt
D
Note: Lvalue references and rvalue references are syntactically and semantically similar, but they follow slightly different rules
Example:
int data = 100; // Declaring lvalue reference int& lRef = data; // Declaring rvalue reference int&& rRef = 120;