-1

I know I'm not allowed to write long& const x. But why not? Theoretically I could have a constant reference to x. I just wouldn't be able to set the reference to another address.

Why is it forbiden in C++?

Edit: I know there is something I don't understand, but I can assign a reference to another variable. This piece of code works:

#include <iostream>

int main()
{
 int a = 1, b = 2;
 int & x = a;
 x = b;
 return 0;
}

But this is a contradiction to what you are saing.

user2738748
  • 1,106
  • 2
  • 19
  • 36
  • 7
    Are you missing the fact that references are always constant? – E_net4 Oct 19 '15 at 11:02
  • 2
    a const reference is declared like this : `const long& x` – Manos Nikolaidis Oct 19 '15 at 11:02
  • *"but I can assign a reference to another variable"* - no you can't; what your code actually does is move the value of `b` into `a`, not rebase the reference. Remember that - as a reference - `x` is a form of alias for `a`, so whatever you do to it transparently affects `a` - including the assignment. `x` can ***not*** be changed to address a different object. – Tony Delroy Oct 19 '15 at 11:10
  • After `int& x = a;`, "x" and "a" are two different names for the same object, and `x = b;` is exactly the same as `a = b;`. – molbdnilo Oct 19 '15 at 11:12
  • You may also wish to see this question. http://stackoverflow.com/q/26818908/1233251 – E_net4 Oct 19 '15 at 11:14
  • 2
    @Manos: No, that is a reference-to-`const`. See why accurate terminology matters? – Lightness Races in Orbit Oct 19 '15 at 11:15
  • @LightnessRacesinOrbit the question mentions "const reference". This term appears in the ISO C++11 standard referring to expressions like `const int& cri = i;` e.g. paragraph 14.1 clause 6. I couldn't find the term "reference to const" in the standard. But given the similar syntax and behaviour to "pointer to const" it might actually be more appropriate. – Manos Nikolaidis Oct 19 '15 at 11:59
  • @ManosNikolaidis: "reference to const" appears several times, but you are correct that it says "const reference" more often. This is a well-known defect, probably arising out of laziness :( – Lightness Races in Orbit Oct 19 '15 at 12:04

2 Answers2

6

References are already always constant.

You can't reassign them to reference another variable after initialization, and you must always initialize them to reference a variable.

In your example, the code isn't doing what you think it is:

x = b; doesn't reassign x to refer to b, it assigns the value of b to the variable that x is referencing, i.e. a.

A reference is just an alias, another name, for a variable. Meaning that you could substitute each instance of x with a, and the behavior would be the same.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
3

I just wouldn't be able to set the reference to another address.

You already can't.

References are immutable. Adding const would not make them "more" immutable.

There is simply no reason to add this, and every reason to avoid the confusion it may bring.

In particular, it would result in people thinking that references are objects, which they're not. References are not pointers under a different guise. They are references; alias names for another object. And names cannot be const.

I can assign a reference to another variable.

No, you can't.

this is a contradiction to what you are sa[y]ing.

No, it's you misunderstanding your own code. With x = b, you are setting a to 2.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055