cpp/language/reference

Declares a named variable as a reference, that is, an alias to an already-existing object or function.

Syntax
A reference variable declaration is any simple declaration whose has the form

@1@ Lvalue reference declarator: the declaration declares  as an lvalue reference to the type determined by. @2@ Rvalue reference declarator: the declaration declares  as an rvalue reference to the type determined by.

A reference is required to be initialized to refer to a valid object or function: see.

There are no references to and no references to references.

Reference types cannot be at the top level; there is no syntax for that in declaration, and if a qualification is added to a typedef-name or, it is ignored.

References are not objects; they do not necessarily occupy storage, although the compiler may allocate storage if it is necessary to implement the desired semantics (e.g. a non-static data member of reference type usually increases the size of the class by the amount necessary to store a memory address).

Because references are not objects, there are no arrays of references, no pointers to references, and no references to references:

{{rrev|since=c++11|

Reference collapsing
It is permitted to form references to references through type manipulations in templates or typedefs, in which case the reference collapsing rules apply: rvalue reference to rvalue reference collapses to rvalue reference, all other combinations form lvalue reference:

(This, along with special rules for when  is used in a function template, forms the rules that make std possible.) }}

Lvalue references
Lvalue references can be used to alias an existing object (optionally with different cv-qualification):

They can also be used to implement pass-by-reference semantics in function calls:

When a function's return type is lvalue reference, the function call expression becomes an expression:

{{rrev|since=c++11|

Rvalue references
Rvalue references can be used to of temporary objects (note, lvalue references to const can extend the lifetimes of temporary objects too, but they are not modifiable through them):

More importantly, when a function has both rvalue reference and lvalue reference, the rvalue reference overload binds to rvalues (including both prvalues and xvalues), while the lvalue reference overload binds to lvalues:

This allows s, operators, and other move-aware functions (e.g. std) to be automatically selected when suitable.

Because rvalue references can bind to xvalues, they can refer to non-temporary objects:

This makes it possible to move out of an object in scope that is no longer needed:

Forwarding references
Forwarding references are a special kind of references that preserve the value category of a function argument, making it possible to forward it by means of std. Forwarding references are either: @1@ function parameter of a function template declared as rvalue reference to cv-unqualified of that same function template:

@2@ except when deduced from a brace-enclosed initializer list:

See also and std. }}

Dangling references
Although references, once initialized, always refer to valid objects or functions, it is possible to create a program where the of the referred-to object ends, but the reference remains accessible (dangling). Accessing such a reference is undefined behavior. A common example is a function returning a reference to an automatic variable:

Note that rvalue references and lvalue references to const extend the lifetimes of temporary objects (see for rules and exceptions).

If the referred-to object was destroyed (e.g. by explicit destructor call), but the storage was not deallocated, a reference to the out-of-lifetime object may be used in limited ways, and may become valid if the object is recreated in the same storage (see for details).