Search

10.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

1234

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.

Best practice

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 the function that takes these types as parameters doesn’t modify them.

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

g)

h) double
i)

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

Show Solution


10.18 -- Member selection with pointers and references
Index
10.16 -- Reference variables

86 comments to 10.17 — References and const

  • Aasif Ali

    can anyone suggest to me a site (or something else) where I can practice the lessons I am learning through learncpp. Sometimes I feel like the quiz is not enough I need to practice a lot before attempting the quiz questions ( most of the time I am not able to solve the difficult questions in quiz ). Alex or nascardriver can you guys suggest something.

  • Waldo Lemmer

    Question #1:
    > You can assume that the function which takes these types as parameters doesn’t modify them.

    "which" should be "that" (https://www.grammarly.com/blog/which-vs-that/)

    Question #1i), line 23:

    >

    You haven't yet explained what {} is (I don't see it in the operator precedence and associativity table).

    • nascardriver

      I have updated the sentence that misused "which". The sentence, which misused "which", is not correct :)

      The {} is list-initialization. The syntax with an omitted type name is covered in lesson 9.4 - Structs. It is the same as

  • s1ngular1tea

    const int value{ 5 };
    const int &ref{ value }; // ref is a reference to const value

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

    Ummm... Dude, you're making this impossible to read. Here, you name a variable with the identifier "value"... Which leads to the entire lesson being moot from then on out... :\

Leave a Comment

Put all code inside code tags: [code]your code here[/code]