11

I noticed that there is no reference to reference but there is pointer to pointer, and also there is no an array of references but an array of pointers.

Could anybody give me any reason?

Gordon Gustafson
  • 38,624
  • 25
  • 111
  • 155
skydoor
  • 24,272
  • 50
  • 141
  • 199
  • 7
    Because Bjarne Stroustrup says so. – David Pfeffer Feb 23 '10 at 20:52
  • Possible duplicate: http://stackoverflow.com/questions/1164266/why-arrays-of-references-are-illegal – Kirill V. Lyadvinsky Feb 23 '10 at 21:26
  • 1
    Such boring answers. How about answering why the standard disallows it? Is there some fundamental reason why c++ can't support rebinding references? – deft_code Feb 23 '10 at 22:31
  • 2
    @Caspin: When limited in this way a reference can be implemented as an alias in the symbol table (i.e. it is not allocated it's own storage and has no existence once code generation happens), and because c++ inherited pointers from c it has no need to build more complicated behaviors into references. – dmckee --- ex-moderator kitten Mar 03 '10 at 19:44

4 Answers4

16

Pointers are mutable (if non-const), references never. Thus there is no point having a pointer or reference to a reference.

Also, a reference must always refer to something - there is no such thing as a null reference. This is why there can be no arrays of references, as there is no way to default instantiate references inside an array to a meaningful value.

Péter Török
  • 112,083
  • 30
  • 265
  • 327
  • 1
    Now that we have array initialization in C++11, it seems like there could be arrays of references. – Gurgeh Jun 25 '12 at 12:21
  • 1
    there has always been array initialization, and I do not see why int& refs[2] = {x, y}; is immoral. – Slava May 20 '13 at 10:23
11

It is according to C++ Standard 8.3.2/4:

There shall be no references to references, no arrays of references, and no pointers to references.

Kirill V. Lyadvinsky
  • 93,507
  • 24
  • 133
  • 210
  • rvalue references ARE NOT references to references, although their syntax `&&` might suggest so. – fredoverflow Feb 23 '10 at 21:03
  • 8
    -1 Very rare for me to downvote, but I mean really, what is the point of this answer? Some posters here seem to imagine the standard is a kind of holy book. First you need a language that is useful and makes sense, only then is a standard necessary. – Bill Forster Feb 23 '10 at 21:04
  • 4
    @Bill: How does one first have a language then a standard, when languages are defined by standards? The C++ language is *defined* by the standard alone; any question regarding C++ can only be answer with information from the standard. What would you propose as an answer instead? – GManNickG Feb 23 '10 at 21:07
  • 1
    @GMan. You misunderstand how the world works. C++ standardization occurred after C++ was created and found useful. Do you really think it is plausible to spend a vast amount of effort creating an enormously detailed standard (like the C++ standard) as the first step in launching the language ? No, these things work organically, someone has an idea, makes a toy compiler, shows it to other people, it builds momentum (or not), eventually you may get something like the C++ standard. I would propose as an answer an explanation of why this feature of C++ makes sense. Others have provided that. – Bill Forster Feb 23 '10 at 21:16
  • 1
    FredOverflow, seems to be you are right. There are still no pointers to rvalue references. – Kirill V. Lyadvinsky Feb 23 '10 at 21:16
  • I thought the question asked to provide some reasoning behind this. – Slava May 20 '13 at 10:26
10

A reference is an abstraction at the language level. It's an opaque way of aliasing a variable to another. While under the hood, the compiler is likely to work out references using pointers, they are very different things at a higher level. On the other hand, pointers are explicitly used by a programmer to achieve indirection. A pointer variable is a distinct variable from what it's pointed to. A reference should be thought as if it's simply an alias to the original variable, not as if it's yet another variable holding an address. Consequently, an alias for an alias of a variable would simply be an alias to the variable itself. Considering the binding a reference to a variable is a compile-time thing may help understanding the rationale behind this behavior.

With this reasoning, you can argue that since arrays are structures that store values, not variables, it doesn't make sense for them to be able to store aliases of variables. Basically, a reference variable (by which I mean the pointer, if exists, that may be used by the compiler to wire up the reference) is invisible to the programmer at the C++ level. If it was possible to declare arrays of references, the compiler probably needed to require constant indexes passed to the arrays to be able to resolve the binding at compile time.

mmx
  • 402,675
  • 87
  • 836
  • 780
  • This is the only answer that actually explains why the standard doesn't want any implementation to allow arrays of references. – Chani Dec 15 '15 at 06:34
4

C++ Standard 8.3.2/4:

There shall be no references to references, no arrays of references, and no pointers to references.

The reasoning behind this is that a reference doesn't exist in and of itself at run-time. A reference merely is another name for a location. They are immutable.

David Pfeffer
  • 37,721
  • 29
  • 123
  • 200