3
int main()
{
    int        n = 1;
    int* const p = &n; // ok

    *p = 2; // ok as expected. 
    p  = 0; // error as expected.

    int& const m = n; 
    // error: 'const' qualifier may not be
    // applied to a reference   

    return 0;
}

Why no const reference in C++ just like const pointer?

What's the rationale behind the design?

xmllmx
  • 39,765
  • 26
  • 162
  • 323

3 Answers3

7

References in C++ differ from pointers in several essential ways. One of the difference is:

Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers.

It means Reference are like similar (see the link at the end of this answer) to const pointer (not pointer to a const!) in C++...

int a = 5;
int& m = a; // Behaves similar to int * const m = &a;
// See the link at the bottom for the differences between const pointer and reference.

and hence, you can't change/rebind them to point to some other address. So, you don't need a explicit const qualifier for a reference and that's why it is disallowed by the compiler.

See this link to learn Why are references not reseatable in C++?. I have copied the accepted answer of the above link:

The reason that C++ does not allow you to rebind references is given in Stroustrup's "Design and Evolution of C++" :

It is not possible to change what a reference refers to after initialization. That is, once a C++ reference is initialized it cannot be made to refer to a different object later; it cannot be re-bound. I had in the past been bitten by Algol68 references where r1=r2 can either assign through r1 to the object referred to or assign a new reference value to r1 (re-binding r1) depending on the type of r2. I wanted to avoid such problems in C++.

EDIT:

See this link for Difference between const pointer and reference? (Thanks to @M.M for pointing out the ambiguity in my statement).

abhiarora
  • 9,743
  • 5
  • 32
  • 57
4

Why no const reference in C++ just like const pointer?

References cannot be modified. Adding const qualification to non-modifiable entity would be meaningless and confusing.

Note that it is technically possible to apply const to a reference indirectly through a type alias or template type argument. Example:

T some_t;
using Ref = T&;
Ref const some_ref = some_t; // well-formed

Ref const type "collapses" into T&, and is same as unqualified Ref. I recommend to generally avoid creating type aliases for pointers and references, except for rare cases where they are conventional. Specifically, Container::reference type alias and similar are conventional.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • How about `int n1, n2; int& m = n1; m = n2;`? – xmllmx Jan 08 '20 at 05:19
  • It might be better to say they can't be re-bound to another target (if that was indeed what you meant). References *can* be modified, it's just that modification is done to the target. – paxdiablo Jan 08 '20 at 05:19
  • 2
    @xmllmx That modifies the referenced object. The reference remains unmodified i.e. it still refers to `n1`. Pointer equivalent is `int n1, n2; int* const m = &n1; *m = n2;` Notice how constness of the pointer doesn't prevent the assignment. Also, it has undefined behaviour since `n2` has indeterminate value. – eerorika Jan 08 '20 at 05:20
  • 1
    @xmllmx, that changes the value of `m/n1`, it doesn't rebind the reference. – paxdiablo Jan 08 '20 at 05:20
2
  int& const m = n; 

IMHO because it's inherently constant by compiler nature, just

 int n ; 

n has ininherent constant reference

so as it is parsing codes it just determine to whichever place const qualifier is only allowed being there, by a compiler rule method for parsing, if not allowed then go to error/warning