views:

595

answers:

5

Does anyone have an idea why the ampersand was chosen as the way to denote references in C++?

AFAIK (though I don't have the book near me), Stroustroup didn't explain that choice, which I find a little odd because the same symbol was already used for address-of in C.

+6  A: 

Stroustrup was always very reluctant to introduce a new reserved symbol or name, so he probably used it to avoid making the feature look weird to users of C.

Daniel Earwicker
That makes sense. Though IMHO, the whole concept of references was bizarre enough for long-time C users, that having to suddenly have the address-of operator have a different semantics may not have been natural...
Uri
Hmm... maybe you shouldn't look at this: http://www.open-std.org/JTC1/sc22/wg21/docs/papers/2004/n1690.html
Daniel Earwicker
I just remembered something from D but the reaction to new reserved words was violently negative (though - ironically - surely it wouldn't have needed to been a reserved word?)
Daniel Earwicker
+9  A: 

In addition to Earwicker's response which I generally agree with. I would also speculate that since & is the "address-of" operator, it is somewhat fitting. Since a reference in many ways is like passing by address instead of by value.

In addition to that, taking the address of a variable is often referred to as "referencing"

(Yes I know that references don't have to be implemented using pointers under the hood, I am referring to the way they conceptually work).

This is just speculation though.

Evan Teran
+1  A: 

Who knows why Stroustrup does anything, but my guess is that because the implementation of reference parameters involves passing the address of an lvalue, Stroustrup chose the C address-of operator because it would give C programmers the right idea about the cost model.

Norman Ramsey
+2  A: 

Here is my theory on that. I think it has much to do with what operators are valid (syntactically) for symbols. Consider

int a[1]; // a[1] is valid (syntactically)
int *a; // *a is valid
int a(char, bool); // a(<a char>, <a bool>) is valid (function call)
int C::*a; // <a C>.*a is valid

Conceptually, in those declarations what is named with a type (C, char, bool) is substituted with an expression of that type later on. Of course the intention is to reuse as much of the existing language as possible. So i think he used &:

int &a; // &a is valid

The important one is that & is only valid on the kind of expression a reference denotes: For lvalues. References are lvalues (named variables are too) and only for them & can be applied:

int &g(); // &g() is valid (taking the address of the referred to integer)
int g(); // &g() is *not* valid (can't apply to temporary int)
Johannes Schaub - litb
I vote up, but notice that "int
Klaim
yes i'm aware of that. I intentionally left it uninitialized because i thought it would not make it clearer to insert an initialization = some_int; likewise when i would have defined an empty class C for the member pointer case. i wanted to highlight their use in expressions. thanks for your +1 :)
Johannes Schaub - litb
sadly, all this breaks with rvalue references in C++1x where it looks like int :)
Johannes Schaub - litb
+2  A: 

My thought was that there are 2 symbols used in pointers: * and &. since int* means a pointer to an int, probably Stroustrup didn't want to introduce a whole new symbol. Since references are sort of like pointers, he stuck with &. Plus, the only previously valid use of & was to take the address of something, so it was OK to use in declarations.

rlbond