xmllmx
xmllmx

Reputation: 42369

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

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?

Upvotes: 3

Views: 1367

Answers (3)

abhiarora
abhiarora

Reputation: 10440

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

Upvotes: 9

eerorika
eerorika

Reputation: 238401

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.

Upvotes: 5

user12450543
user12450543

Reputation:

  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

Upvotes: 2

Related Questions