6.11a — 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.

References to const values are often called “const references” for short.

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-value, 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:

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

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.

Rule: Pass non-pointer, non-fundamental data type variables (such as structs) by (const) reference.

6.12 -- Member selection with pointers and references
6.11 -- Reference variables

13 comments to 6.11a — References and const

  • Yan

    "Rule: Pass non-pointer, non-fundamental data type variables by (const) reference." - u mean non-fundamental data type variables as what variables? Cuz if int, float, char, bool don't match this category, so what is left? Or u mean non-fundamental data type variables as literals?

  • Orfeas

    Hello Alex,
    Thanks for the wonderful tutorials! I came across these two short programs yesterday but I'm not entirely sure how they work.

    This prints:

    2 5 2
    5 5 5

    Why does the value of variable i change when I point pointer i to variable j's memory adress?

    The next one is rather similar:

    This also prints:

    2 5 2
    5 5 5

    I assume &i is a reference, so does that mean that it doesn't need to be initialized when part of a struct? and then I can't even begin to explain what happens on starting from a.i = j.

    Could you help me understand how they work?

    Thanks in advance!

    • Alex

      On Visual Studio, I don't get 555 for the second line, I get 255. Sounds like maybe a compiler bug?

      • Orfeas

        I read further into the tutorials so I understand pointers and references better, so I understand what happens in program 2, but check this out (I'm also on Visual Studio). Turns out in the first program I actually get an error:

        1>c:usersorfeadocumentsvisual studio 2017projectseurydiceeurydiceeurydice.cpp(18): error C2100: illegal indirection (HERE, LINE 10: A a({ i });)
        1>Done building project "Eurydice.vcxproj" -- FAILED.

        So it was printing the second program instead (as I tried that one first), which still gives me 5 5 5 on the second line.

        I tried to think the flow of the second program through:

        The program starts executing at main: i = 2, j = 5. i is then sent to a (an A struct) so "a.i" _literally is_ i, since &i is a reference variable. Then i (still 2), j (still 5) and a.i (refers to i, so 2) are printed.

        Then I make a.i (literally i) have j's value, so i must have j's value, so cout prints 5 5 5.

        So I then tried figuring out what caused the "Illegal indirection" error in the first program.

        The program starts executing at main: i = 2 and j = 5, just as before. a (an A struct) contains a pointer, to which &i (i's memory adress) is sent.., meaning a.i points to i.
        i (still 2), j (still 5), and *a.i (a dereferenced pointer, pointing to i, so 2).

        Then I make a.i point to &j (right?) so i SHOULD stay the same, j as well, but if I were to derefence *a.i, I'll get 5. So I assume this went fine on your machine and the computer correctly printed 2 5 2, 2 5 5. But mine for some reason found an error and ended up reprinting 2 5 2, 5 5 5 from the previous (here the second) program. What should I do about this?

        • Alex

          Well, your first program has a syntax error, in that you're trying to initialize variable a in a way that the compiler doesn't understand. Try this instead:

          Then your program should compile and run correctly.

      • Kumar Santhanam

        Hi Alex, output is 555 should be second line. because  A a({ i }); it means internally i value alias to &a.i, correct?,  so second time assigning value j value to it will update in i also. so output is 555 for second line.

  • Khang

    Hi, Alex.
    At "References to r-values extent the life time of the referenced value"
    What is the difference between
    [const int &ref=2+3;]
    [const int value=2+3;].
    It seems that they're all extend the life time of "2+3", so is it too obvious
    to say the statement above?

  • FinalDevil

    Hi Alex, at the command

    , should it be

    ? It is reference to rvalue, so we use double ampersand?

    • Alex

      There's no need to double-ampersand here. A double-ampersanded reference variable is still treated as an l-value (after all, it has an address). So essentially, in such a case, the second ampersand is ignored.

Leave a Comment

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