Mike said:
It is wrong. A reference is *not* a pointer. Period.
I didn't say it was. Note the qualifiers "not entirely wrong" and
"view".
Saying it is *any* kind of pointer is incorrect.
I didn't say it was. But try this: "A reference has identical syntax
and semantics to a pointer that is implicitly dereferenced, in all
operations except initialization." I believe this description is
flawless.
There are others who occasionally explain references as being related
to pointers in some way:
"The obvious implementation of a reference is as a (constant) pointer
that is dereferenced each time it is used. It doesn't do much harm
thinking about references that way, as long as one remembers that a
reference isn't an object that can be manipulated the way a pointer
is:" [TC++PL, page 98]
Of course, Stroustrup's analogy is just illustration. I am claiming
mine is perfect. So it is more of an issue if mine has exceptions.
Still, I asked for exceptions to be pointed out, and if there are any,
they can be listed as special cases. If there are too many special
cases, the whole of it can be used as an example of how thinking of
references as "like pointers" leads to pitfalls. But as of yet, the
only exception I know is initialization. Initialization includes
passing references as arguments.
[...]
This is not how the Standard describes references,
Of course not, because it's utterly wrong.
How would the C++ language be different, if references were defined the
way I said, instead of how it is done in the standard? You tried to
point out a way below, but the as-if rule makes it irrelevant.
A reference is not a pointer. It's not an object, it has no
address. The same is not true of a pointer.
No it's not. A pointer is an object, and occupies storage.
A reference does not.
Doesn't count, since it is purely lingustic. In the standard
explanation it is a primary that the reference occupies no storage. In
my analogy, it is a secondary consequence; the storage can never be
accessed. By the as-if rule, this is equivalent to it having no
storage.
I am looking for a way in which my above explanation of references
would result in wrong code, or code with a different meaning, than the
standard-ise explanation of references. C++ is not a standards
document; it is the language defined by that document. There are often
multiple ways of defining the same construct. The same is true of
languages.
Also, this is not the only to think about references. The standard's
way of doing it is fine too. And I sometimes think of references as
follows,
- objects have value syntax and value semantics
- pointers have reference syntax and reference semantics
- (pointers are objects, and they also have value operations)
- references have value syntax but reference semantics
I think this view is most helpful when deciding whether to pass an
argument by pointer with its address, or by reference.