What is a constant reference? (not a reference to a constant)

135,476

Solution 1

The clearest answer. Does “X& const x” make any sense?

No, it is nonsense

To find out what the above declaration means, read it right-to-left: “x is a const reference to a X”. But that is redundant — references are always const, in the sense that you can never reseat a reference to make it refer to a different object. Never. With or without the const.

In other words, “X& const x” is functionally equivalent to “X& x”. Since you’re gaining nothing by adding the const after the &, you shouldn’t add it: it will confuse people — the const will make some people think that the X is const, as if you had said “const X& x”.

Solution 2

The statement icr=y; does not make the reference refer to y; it assigns the value of y to the variable that icr refers to, i.

References are inherently const, that is you can't change what they refer to. There are 'const references' which are really 'references to const', that is you can't change the value of the object they refer to. They are declared const int& or int const& rather than int& const though.

Solution 3

What is a constant reference (not a reference to a constant)
A Constant Reference is actually a Reference to a Constant.

A constant reference/ Reference to a constant is denoted by:

int const &i = j; //or Alternatively
const int &i = j;
i = 1;            //Compilation Error

It basically means, you cannot modify the value of type object to which the Reference Refers.
For Example:
Trying to modify value(assign 1) of variable j through const reference, i will results in error:

assignment of read-only reference ‘i’


icr=y;          // Can change the object it is pointing to so it's not like a const pointer...
icr=99;

Doesn't change the reference, it assigns the value of the type to which the reference refers. References cannot be made to refer any other variable than the one they are bound to at Initialization.

First statement assigns the value y to i
Second statement assigns the value 99 to i

Solution 4

By "constant reference" I am guessing you really mean "reference to constant data". Pointers on the other hand, can be a constant pointer (the pointer itself is constant, not the data it points to), a pointer to constant data, or both.

Solution 5

This code is ill-formed:

int&const icr=i;

Reference: C++17 [dcl.ref]/1:

Cv-qualified references are ill-formed except when the cv-qualifiers are introduced through the use of a typedef-name or decltype-specifier, in which case the cv-qualifiers are ignored.

This rule has been present in all standardized versions of C++. Because the code is ill-formed:

  • you should not use it, and
  • there is no associated behaviour.

The compiler should reject the program; and if it doesn't, the executable's behaviour is completely undefined.

NB: Not sure how none of the other answers mentioned this yet... nobody's got access to a compiler?

Share:
135,476
Bat0u89
Author by

Bat0u89

Student of University of Pireaus in Computer Science.

Updated on September 14, 2021

Comments

  • Bat0u89
    Bat0u89 over 2 years

    A pretty theoretical question...Why do constant references not behave the same way as constant pointers so that I can actually change the object they are pointing to? They really seem like another plain variable declaration. Why would I ever use them? This is a short example that I run which compiles and runs with no errors:

    int main (){
        int i=0;
        int y=1;    
        int&const icr=i;
        icr=y;          // Can change the object it is pointing to so it's not like a const pointer...
        icr=99;         // Can assign another value but the value is not assigned to y...
        int x=9;
        icr=x;
        cout<<"icr: "<<icr<<", y:"<<y<<endl; 
    }
    
  • Void
    Void over 12 years
    The sample code actually refers to a constant reference (int & const icr = i;) not a reference to a constant.
  • Poodlehat
    Poodlehat over 12 years
    I assumed the poster wasn't clear because of where the const was placed in the code.
  • Dasaru
    Dasaru over 11 years
    So a reference to a const can also be seen as a constant reference to a const? That makes a lot of sense now.
  • Kaiserludi
    Kaiserludi over 9 years
    @Dasaru: Yes, it can, but a reference to non-const can also be seen as a constant reference to non-const, as the reference itself is always const, independently from if it references a const or not.
  • Destructor
    Destructor almost 9 years
    How & why statement like const int& a=3; valid?
  • Tyler
    Tyler over 8 years
    Wait, const Type& is equivalent to Type const&?
  • M.M
    M.M over 5 years
    X& const x is ill-formed, this is a mistake in the FAQ