- Learn C++ - https://www.learncpp.com -

9.17 — References and const

Reference to const value

Just like it’s possible to declare a pointer to a const value, it’s also possible to declare a reference to a const value. This is done by declaring a reference using the const keyword.

A reference to a const value is often called a const reference for short, though this does make for some inconsistent nomenclature with pointers.

Initializing references to const values

Unlike references to non-const values, which can only be initialized with non-const l-values, references to const values can be initialized with non-const l-values, const l-values, and r-values.

Much like a pointer to a const value, a reference to a const value can reference a non-const variable. When accessed through a reference to a const value, the value is considered const even if the original variable is not:

References to r-values extend the lifetime of the referenced value

Normally r-values have expression scope, meaning the values are destroyed at the end of the expression in which they are created.

However, when a reference to a const value is initialized with an r-value, the lifetime of the r-value is extended to match the lifetime of the reference.

Const references as function parameters

References used as function parameters can also be const. This allows us to access the argument without making a copy of it, while guaranteeing that the function will not change the value being referenced.

References to const values are particularly useful as function parameters because of their versatility. A const reference parameter allows you to pass in a non-const l-value argument, a const l-value argument, a literal, or the result of an expression:

The above prints


To avoid making unnecessary, potentially expensive copies, variables that are not pointers or fundamental data types (int, double, etc…) should be generally passed by (const) reference. Fundamental data types should be passed by value, unless the function needs to change them. There are a few exceptions to this rule, namely types that are so small that it’s faster for the CPU to copy them than having to perform an extra indirection for a reference.

A reminder

References act like pointers. The compiler adds the indirection, which we’d do manually on a pointer using an asterisk, for us.

One of those fast types is std::string_view. You’ll learn about more exceptions later. If you’re uncertain if a non-fundamental type is fast to pass by value, pass it by const reference.


Pass non-pointer, non-fundamental data type variables (such as structs) by (const) reference, unless you know that passing it by value is faster.

Quiz time

Question #1

Which of the following types should be passed by value, which by const reference? You can assume that the function which takes these types as parameters doesn’t modify them.

a) char
b) std::string
c) unsigned long
d) bool
e) An enumerator


h) double

For reference, this is how we’d go about using ArrayView:
Show Hint [1]

Show Solution [1]

9.18 -- Member selection with pointers and references [2]
Index [3]
9.16 -- Reference variables [4]