In C++, a reference is an alias for an existing object. Once a reference has been defined, any operation on the reference is applied to the object being referenced. This means we can use a reference to read or modify the object being referenced.
Although references might seem silly, useless, or redundant at first, references are used everywhere in C++ (we’ll see examples of this in a few lessons).
Key insight
A reference is essentially identical to the object being referenced.
You can also create references to functions, though this is done less often.
Modern C++ contains two types of references: lvalue references, and rvalue references. In this chapter, we’ll discuss lvalue references.
Related content
Because we’ll be talking about lvalues and rvalues in this lesson, please review 12.2 -- Value categories (lvalues and rvalues) if you need a refresher on these terms before proceeding. Rvalue references are covered in the chapter on move semantics (chapter 22).
Lvalue references types
An lvalue reference (commonly just called a “reference” since prior to C++11 there was only one type of reference) acts as an alias for an existing lvalue (such as a variable).
Just like the type of an object determines what kind of value it can hold, the type of a reference determines what type of object it can reference. Lvalue reference types can be identified by use of a single ampersand (&) in the type specifier:
// regular types
int // a normal int type (not an reference)
int& // an lvalue reference to an int object
double& // an lvalue reference to a double object
const int& // an lvalue reference to a const int object
For example, int&
is the type of an lvalue reference to an int
object, and const int&
is the type of an lvalue reference to a const int
object.
A type that specifies a reference (e.g. int&
) is called a reference type. The type that can be referenced (e.g. int
) is called the referenced type.
Nomenclature
There are two kinds of lvalue references:
- An lvalue reference to a non-const is commonly just called an “lvalue reference”, but may also be referred to as an lvalue reference to non-const or a non-const lvalue reference (since it isn’t defined using the
const
keyword). - An lvalue reference to a const is commonly called either an lvalue reference to const or a const lvalue reference.
We’ll focus on non-const lvalue references in this lesson, and on const lvalue references in the next lesson (12.4 -- Lvalue references to const).
Lvalue reference variables
One of the things we can do with an lvalue reference type is create an lvalue reference variable. An lvalue reference variable is a variable that acts as a reference to an lvalue (usually another variable).
To create an lvalue reference variable, we simply define a variable with an lvalue reference type:
#include <iostream>
int main()
{
int x { 5 }; // x is a normal integer variable
int& ref { x }; // ref is an lvalue reference variable that can now be used as an alias for variable x
std::cout << x << '\n'; // print the value of x (5)
std::cout << ref << '\n'; // print the value of x via ref (5)
return 0;
}
In the above example, the type int&
defines ref
as an lvalue reference to an int, which we then initialize with lvalue expression x
. Thereafter, ref
and x
can be used synonymously. This program thus prints:
5 5
From the compiler’s perspective, it doesn’t matter whether the ampersand is “attached” to the type name (int& ref
) or the variable’s name (int &ref
), and which you choose is a matter of style. Modern C++ programmers tend to prefer attaching the ampersand to the type, as it makes clearer that the reference is part of the type information, not the identifier.
Best practice
When defining a reference, place the ampersand next to the type (not the reference variable’s name).
For advanced readers
For those of you already familiar with pointers, the ampersand in this context does not mean “address of”, it means “lvalue reference to”.
Modifying values through a non-const lvalue reference
In the above example, we showed that we can use a reference to read the value of the object being referenced. We can also use a non-const reference to modify the value of the object being referenced:
#include <iostream>
int main()
{
int x { 5 }; // normal integer variable
int& ref { x }; // ref is now an alias for variable x
std::cout << x << ref << '\n'; // print 55
x = 6; // x now has value 6
std::cout << x << ref << '\n'; // prints 66
ref = 7; // the object being referenced (x) now has value 7
std::cout << x << ref << '\n'; // prints 77
return 0;
}
This code prints:
55 66 77
In the above example, ref
is an alias for x
, so we are able to change the value of x
through either x
or ref
.
Reference initialization
Much like constants, all references must be initialized. References are initialized using a form of initialization called reference initialization.
int main()
{
int& invalidRef; // error: references must be initialized
int x { 5 };
int& ref { x }; // okay: reference to int is bound to int variable
return 0;
}
When a reference is initialized with an object (or function), we say it is bound to that object (or function). The process by which such a reference is bound is called reference binding. The object (or function) being referenced is sometimes called the referent.
Non-const lvalue references can only be bound to a modifiable lvalue.
int main()
{
int x { 5 };
int& ref { x }; // okay: non-const lvalue reference bound to a modifiable lvalue
const int y { 5 };
int& invalidRef { y }; // invalid: non-const lvalue reference can't bind to a non-modifiable lvalue
int& invalidRef2 { 0 }; // invalid: non-const lvalue reference can't bind to an rvalue
return 0;
}
Key insight
If non-const lvalue references could be bound to non-modifiable (const) lvalues or rvalues, then you would be able to change those values through the reference, which would be a violation of their const-ness.
Lvalue references to void
are disallowed (what would be the point?).
Even though the type of the reference (e.g. int&
) doesn’t exactly match the type of the object being bound (e.g. int
), no conversion is applied here (not even a trivial conversion) -- the difference in types is handled as part of the reference initialization process.
A reference will (usually) only bind to an object matching its referenced type
In most cases, a reference will only bind to an object whose type matches the referenced type, (there are some exceptions to this rule that we’ll discuss when we get into inheritance).
If you try to bind a reference to an object that does not match its referenced type, the compiler will try to implicitly convert the object to the referenced type and then bind the reference to that.
Key insight
Since the result of a conversion is an rvalue, and a non-const lvalue reference can’t bind to an rvalue, trying to bind a non-const lvalue reference to an object that does not match its referenced type will result in a compilation error.
int main()
{
int x { 5 };
int& ref { x }; // okay: referenced type (int) matches type of initializer
double d { 6.0 };
int& invalidRef { d }; // invalid: conversion of double to int is narrowing conversion, disallowed by list initialization
double& invalidRef2 { x }; // invalid: non-const lvalue reference can't bind to rvalue (result of converting x to double)
return 0;
}
References can’t be reseated (changed to refer to another object)
Once initialized, a reference in C++ cannot be reseated, meaning it cannot be changed to reference another object.
New C++ programmers often try to reseat a reference by using assignment to provide the reference with another variable to reference. This will compile and run -- but not function as expected. Consider the following program:
#include <iostream>
int main()
{
int x { 5 };
int y { 6 };
int& ref { x }; // ref is now an alias for x
ref = y; // assigns 6 (the value of y) to x (the object being referenced by ref)
// The above line does NOT change ref into a reference to variable y!
std::cout << x << '\n'; // user is expecting this to print 5
return 0;
}
Perhaps surprisingly, this prints:
6
When a reference is evaluated in an expression, it resolves to the object it’s referencing. So ref = y
doesn’t change ref
to now reference y
. Rather, because ref
is an alias for x
, the expression evaluates as if it was written x = y
-- and since y
evaluates to value 6
, x
is assigned the value 6
.
Reference scope and duration
Reference variables follow the same scoping and duration rules that normal variables do:
#include <iostream>
int main()
{
int x { 5 }; // normal integer
int& ref { x }; // reference to variable value
return 0;
} // x and ref die here
References and referents have independent lifetimes
With one exception (that we’ll cover next lesson), the lifetime of a reference and the lifetime of its referent are independent. In other words, both of the following are true:
- A reference can be destroyed before the object it is referencing.
- The object being referenced can be destroyed before the reference.
When a reference is destroyed before the referent, the referent is not impacted. The following program demonstrates this:
#include <iostream>
int main()
{
int x { 5 };
{
int& ref { x }; // ref is a reference to x
std::cout << ref << '\n'; // prints value of ref (5)
} // ref is destroyed here -- x is unaware of this
std::cout << x << '\n'; // prints value of x (5)
return 0;
} // x destroyed here
The above prints:
5 5
When ref
dies, variable x
carries on as normal, blissfully unaware that a reference to it has been destroyed.
Dangling references
When an object being referenced is destroyed before a reference to it, the reference is left referencing an object that no longer exists. Such a reference is called a dangling reference. Accessing a dangling reference leads to undefined behavior.
Dangling references are fairly easy to avoid, but we’ll show a case where this can happen in practice in lesson 12.12 -- Return by reference and return by address.
References aren’t objects
Perhaps surprisingly, references are not objects in C++. A reference is not required to exist or occupy storage. If possible, the compiler will optimize references away by replacing all occurrences of a reference with the referent. However, this isn’t always possible, and in such cases, references may require storage.
This also means that the term “reference variable” is a bit of a misnomer, as variables are objects with a name, and references aren’t objects.
Because references aren’t objects, they can’t be used anywhere an object is required (e.g. you can’t have a reference to a reference, since an lvalue reference must reference an identifiable object). In cases where you need a reference that is an object or a reference that can be reseated, std::reference_wrapper
(which we cover in lesson 23.3 -- Aggregation) provides a solution.
As an aside…
Consider the following variables:
int var{};
int& ref1{ var }; // an lvalue reference bound to var
int& ref2{ ref1 }; // an lvalue reference bound to var
Because ref2
(a reference) is initialized with ref1
(a reference), you might be tempted to conclude that ref2
is a reference to a reference. It is not. Because ref1
is a reference to var
, when used in an expression (such as an initializer), ref1
evaluates to var
. So ref2
is just a normal lvalue reference (as indicated by its type int&
), bound to var
.
A reference to a reference (to an int
) would have syntax int&&
-- but since C++ doesn’t support references to references, this syntax was repurposed in C++11 to indicate an rvalue reference (which we cover in lesson 22.2 -- R-value references).
Author’s note
If references seem a bit useless at this point, don’t worry. References are used a lot, and we’ll cover one of the primary reasons why shortly, in lessons 12.5 -- Pass by lvalue reference and 12.6 -- Pass by const lvalue reference.
Quiz time
Question #1
Determine what values the following program prints by yourself (do not compile the program).
#include <iostream>
int main()
{
int x{ 1 };
int& ref{ x };
std::cout << x << ref << '\n';
int y{ 2 };
ref = y;
y = 3;
std::cout << x << ref << '\n';
x = 4;
std::cout << x << ref << '\n';
return 0;
}