TITLE: what exactly are references (Newsgroups: comp.lang.c++.moderated, 8 Oct 96) PAYNE: Tom Payne >[...] > * Most people interpret the standard as saying that sizeof(T&) is > sizeof(T). (I disagree with that interpretation both on the > basis of the actual words and from the stand point of common > sense.) CLAMAGE: clamage@taumet.Eng.Sun.COM (Steve Clamage) I'll quote from the current draft standard, section 5.3.3 "Sizeof": "When applied to a reference, the result is the size of the referenced type." You can find exactly the same sentence in the ARM, p 56. I don't see how you could possibly interepret that in any way other than by saying that sizeof(T&) is the same as sizeof(T). PAYNE: >: References are synonyms, not objects; ... > >Your claim that "references are synonyms, not objects" puts you in >good company, that of nearly the entire C++ standards committee. >Still, I find nothing in the draft standard to support it. Yes, the >standard explicitly says that references don't have to be allocated >memory, but, by the as-if rule, neither do pointers or other objects, >especially in the contexts where references are allowed. CLAMAGE: The description of references (8.3.2) in the current draft (September 1996) allows an informal interpretation as aliases or synomyms. The language definition doesn't describe them as such. References are described as having certain properties. References can sometimes be optimized away, as can some temporary or const objects. You can always force a const object to occupy memory by taking its address. That does not apply to a reference. I do not believe there is any reliable way to detect via C++ source code whether a reference has been assigned any space in memory. (You can add a reference member to a class and see if its size changes, but any conclusion you draw from that will involve assumptions about class layout which are not guaranteed. Besides, a class data member is at best a subobject, and is not always guaranteed to be a distinct object in its own right.) You can argue from an implementation viewpoint that under certain circumstances a reference can feasibly be implemented only as a pointer in memory which is dereferenced. That is not the same as saying that a reference is an object by the C++ language definition. Similarly, on the machine I'm using now, ints and longs have exactly the same implementation. That does not mean they are the same type; they are different types. The language definition allows them to be implemented differently, and indeed on some machines they are implemented differently. Implementation details and language definition are different categories of things. In C++, objects have certain properties. Reference do not have all the properties of objects, and so are not objects. What are they? They are references, a term which is defined extensionally.