Topic: References to references


Author: nobody@nowhere.com ("Anonymous")
Date: Mon, 24 Feb 2003 19:56:06 +0000 (UTC)
Raw View
Why doesn't C++ support references to references?

I think it would make sense. For example,

int i;
int j;
int& r1 = a;
int& r2 = b;

int&& rr = r1; //rr refers to r1
&rr = &r2; //rr refers to r2
rr = 3; //Set value of r2 (b) to 3
rr = a; //r2 refers to a
(int&)rr = 3; //a = 3;

Written in terms of pointers, we would have

int i;
int j;
int* r1 = &a;
int* r2 = &b;

int** rr = &r1; //rr refers to r1
rr = &r2; //rr refers to r2
**rr = 3; //Set value of r2 (b) to 3
*rr = &a; //r2 refers to a
**rr = 3; //a = 3;


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: ron@sensor.com ("Ron Natalie")
Date: Mon, 24 Feb 2003 21:20:18 +0000 (UTC)
Raw View
""Anonymous"" <nobody@nowhere.com> wrote in message news:kf%5a.10385$ep5.2516@nwrddc02.gnilink.net...
> Why doesn't C++ support references to references?

No.

> I think it would make sense. For example,
>
> int i;
> int j;
> int& r1 = a;
> int& r2 = b;
>
> int&& rr = r1; //rr refers to r1
> &rr = &r2; //rr refers to r2

Use a pointer.   One of the tenets of references is that they can not be reseated once
they are initialized.



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Richard Smith" <richard@ex-parrot.com>
Date: Tue, 25 Feb 2003 16:23:42 CST
Raw View
""Anonymous"" <nobody@nowhere.com> wrote in message
news:kf%5a.10385$ep5.2516@nwrddc02.gnilink.net...
> Why doesn't C++ support references to references?

There is a defect report (106) that is related to this.  See the following
links for more details:

  http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_defects.html#106
  http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/2000/n1245.ps

But the suggestion in these is that a reference to a reference is just a
reference.  I.e.

  typedef int& int_ref;
  int_ref& i;  // Exactly the same as int& i

So semantically

  int& & i;

would be equivalent to

  int& i;

However, I don't think there is any intention to allow a declaration with a
repeated &.  The proposed resolution to the DR does not remove the sentence
"There shall be no references to references..." from 8.3.2/4, and
Stroustup's paper is fairly explicit about not allowing it.

On an entirely unrelated note, the similar looking syntax

  int&& i;

(which is parsed to use the && token once rather than & token twice) might
get hijacked for use with rvalue references.

  http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/2002/n1377.htm


> I think it would make sense. For example,
>
> int i;
> int j;
> int& r1 = a;
> int& r2 = b;

I assume you mean i and j not a and b.
>
> int&& rr = r1; //rr refers to r1

"rr refers to r1", which refers to a. Thus rr refers to a.  So this line is
exactly equivalent to

  int& rr = i;  // or whatever a was supposed to be

Incidentally, to get the ampersands to parse correctly you'd need a space
between them, much as with

  vector<pair<int,int>> v;  // Error:  >> parsed as a single token

> &rr = &r2; //rr refers to r2

Since when have you been allowed to rebind a reference of any type?  This is
no different from

  int i, j;
  int& r = i;
  &r = &j;   // Illegal.

--
Richard Smith


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]