Forwarding References
Learn the concept of forwarding references in C++.
We'll cover the following
Kinds of references in C++11
One of the most important features that were added to the language in C++11 is move semantics, which helps improve performance by avoiding making unnecessary copies. Move semantics are supported by another C++11 feature called rvalue references. Before discussing these, it’s worth mentioning that in C++, we have two kinds of values:
lvalues are values that refer to a memory location; therefore, we can take their address with the
&
operator. lvalues can appear both on the left and right sides of an assignment expression.rvalues are values that are not lvalues. They are defined by exclusion. rvalues do not refer to a memory location, and we can’t take their address with the
&
operator. rvalues are literals and temporary objects and can only appear on the right side of an assignment expression.
Note: In C++11, there are a few other value categories, glvalue, prvalue, and xvalue. Discussing them here would not benefit the current topic. However, we can read more about them on the cppreference website.
References are aliases to already existing objects or functions. Just as we have two kinds of values, in C++11 we have two kinds of references:
lvalue references, denoted with a
&
, such as in&x
, are references to lvalues.rvalue references, denoted with
&&
, such as in&&x
, are references to rvalues.
Let’s look at some examples to understand these concepts better:
Get hands-on with 1400+ tech skills courses.