Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!columbia!read.columbia.edu!kearns From: kearns@read.columbia.edu (Steve Kearns) Newsgroups: comp.lang.c++ Subject: Re: References considered crippled Message-ID: <6362@columbia.edu> Date: 14 Jun 89 20:00:11 GMT References: <6361@columbia.edu> <9483@alice.UUCP> Sender: news@columbia.edu Reply-To: kearns@read.UUCP () Distribution: comp.lang.c++ Organization: Columbia University Department of Computer Science Lines: 49 In article <9483@alice.UUCP> ark@alice.UUCP (Andrew Koenig) writes: >In article <6361@columbia.edu>, kearns@read.columbia.edu (Steve Kearns) writes: > >> I think that one should be able to assign to references. >> Here are some arguments for this idea. What were the >> decisions for making them unassignable? > >So far no one has come up with a syntax for doing it and >evidence that it's useful enough to be worth the effort >of implementing. > >For example, the syntax you suggested: > > &a = b; > >can't be used because it already has a meaning: apply the >unary `&' operator to a and assign b to the result. This may >sound nonsensical until you realize that a might be of a class >for which unary operator& is defined and returns an lvalue. >-- > --Andrew Koenig > ark@europa.att.com You have convinced me that "&classref = exp" will not work as a syntax for resassigning to a reference. However, in your rebuttal above "a" should be a REFERENCE to a class for which unary operator& is defined. The basic problem with &classref is that references are dereferenced before anything else is evaluated, so no ordinary operator can act on a reference. This is pointed out pretty clearly on pg. 56 of BS' C++ book. How about using & as a postfix operator? The postfix operator& would have higher precedence than dereferencing a reference: a& = b // I hope that "& =" is not "&=" Of course, this doesn't address your objection that no evidence exists for its utility. All I can offer on that front is the following: (1) Why use a->b instead of (*c).b? The reason is that (*c).b is ugly and hard to type. If we had reassignable references, a ptr that is usually dereferenced, like c, could be declared as a reference cref, so we could write cref.b instead. The few times we don't want to derefence could be handled with the cref& notation. Of course, the most important reason for having references is "var" parameters in functions, and the ability to return an expression which can be an lvalue or a value. Since references in their current form give us this, it is not the end of the world if references are crippled. I look at them as notational conveniences.