daniel@cis.ucsc.edu (Daniel Edelson) (08/24/90)
Is there a concensus on what a const reference means? A volatile ref. makes as much sense as a volatile pointer, but a const reference seems redundant. Is their either agreement or active debate on whether this should be allowed or if it has non-null semantic content? Daniel Edelson | ``C++ is not too complex, daniel@cis.ucsc.edu, or | job security is important.'' ...!sun!practic!peren!daniel | Daniel Edelson | ``C++ is not too complex, daniel@cis.ucsc.edu, or | job security is important.'' ..!sun!practic!peren!daniel |
rfg@NCD.COM (Ron Guilmette) (08/25/90)
In article <6285@darkstar.ucsc.edu> daniel@cis.ucsc.edu (Daniel Edelson) writes: >Is there a concensus on what a const reference means? I assume you mean a reference which is itself const. If so, the answer is yes. The following two are the same: extern int & const xx; extern int & yy; There's no semantic difference. >A volatile ref. makes as much sense as a volatile pointer, Not exactly true. The volatility of a pointer object becomes significant whenever that pointer object itself is written. This may occur often during the lifetime of the pointer object. A reference object is however only written once (i.e. during its initialization). Thus, volatile references, while having some semantic differences from non-volatile references, are not very useful. >but a const reference seems redundant. Quite. >Is their either >agreement or active debate on whether this should be allowed >or if it has non-null semantic content? I haven't seen any debate so I assume that indicates agreement that const refs should be allowed. Perhaps a warning like: CAUTION: silly programming ahead should be suggested by the final standard. The semantic content does seem to be zero. -- // Ron Guilmette - C++ Entomologist // Internet: rfg@ncd.com uucp: ...uunet!lupine!rfg // Motto: If it sticks, force it. If it breaks, it needed replacing anyway.
ark@alice.UUCP (Andrew Koenig) (08/25/90)
In article <6285@darkstar.ucsc.edu>, daniel@cis.ucsc.edu (Daniel Edelson) writes: > Is there a concensus on what a const reference means? If you truly mean a const reference, such as int x; int &const y = x; then I've never seen such a thing. I suppose it would have to mean a reference that you're not allowed to change, even though you can change the thing to which it refers. But that's true of all references, so `const' here doesn't add anything. If, on the other hand, you mean a reference to const: const int& z = x; then that does carry a useful semantic meaning. I've said here that z is an alias to x, but I promise that I will not use z to change x. The most common use of such things is as formal paramters to functions that do not wish to incur the overhead of copying their argument. For example: complex operator+(const complex& x, const complex& y) { return complex(x.re() + y.re(), x.im() + y.im() ); } This function uses references to constants as a way to avoid copying the arguments, which would presumably be slower. There is one other semantic detail that is important: a reference to constant may be bound to any expression, while an ordinary reference may be bound only to an lvalue: int& x = 3; // illegal const int& x = 3; // OK You may be surprised to see that the first example is illegal. Perhaps a better word is `deprecated:' the manual says it's illegal but many compilers still allow it. The point is to prohibit stuff like this: cin >> (x + y); // ??? -- --Andrew Koenig ark@europa.att.com