tags:

views:

280

answers:

4

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?

+10  A: 

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
FredOverflow
-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
@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?
GMan
What? No. You cannot reinitialize an rvalue reference either...
FredOverflow
@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
FredOverflow, seems to be you are right. There are still no pointers to rvalue references.
Kirill V. Lyadvinsky
+12  A: 

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
+8  A: 

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.

Mehrdad Afshari
+4  A: 

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
References are occupy memory. Check what is the size of `struct X { int long }` for instance.
Kirill V. Lyadvinsky
David, Constructor omitted for simplicity.
Kirill V. Lyadvinsky
References exists plenty at runtime. Check your call stack in binary. Lookit them addresses point at your objects! :)
Marcus Lindblom