Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!apple!sun-barr!cs.utexas.edu!uunet!odi!benson From: benson@odi.com (Benson Margulies) Newsgroups: comp.lang.c++ Subject: Re: Reference Semantics Message-ID: <323@odi.ODI.COM> Date: 16 May 89 13:04:05 GMT References: <315@odi.ODI.COM> <9310@alice.UUCP> <10251@socslgw.csl.sony.JUNET> Reply-To: benson@odi.com (Benson Margulies) Organization: Object Design Inc., Burlington, MA Lines: 53 Here are some more thoughts on this: 1) I ask myself, why have both pointers and references in the language? There seem to be three differences as described be js: a) references obviate the need for the -> syntax. b) references can "never" by null, obviating error checks. c) references can never change their binding. Of the three, (a) is clearly an advantage. (b) might be an advantage, but I have more to say on the point later. (c), IMHO, is an inconvienience, leading to lots of extra {} blocks. (Now, in Lisp, it is quite typical to have language constructs that require an extra block. But in C++/C, its pretty inconsistent with the rest of the language. (Unless, of course, someone is planning dynamically sized arrays.)) Now for (b). I argue that (b) is false. C++, like C, is supposed to be close to the machine. Therefore, (IMHO) it is inappropriate for implementations to emit code at runtime to ensure that a reference is never assigned to null. Consider this code fragment: foo * foo_pointer; ... arbitrarily complex code .... foo& FOO = *foo_pointer; For compilers to toss an extra dereference into here, let alone a check against the null pointer, would be inappropriate. Therefore, in any case where you are the implementor of a user-callable interface with a reference parameter, you ALREADY have to check for null to make safe code (unless, of course, you prefer "core dumped") as a diagnostic message. Since the existing language does not, in fact, prevent null references, and seems incapable of protecting against them, I (putting on my best, if somewhat dusty from idleness, language lawyer wig) claim that the language ought to explicitly describe their semantics, and that it should demand that in all implementations one must be able to test the address of a reference against 0. As for (c), I still believe that &ref = new_pointer; would be very useful, and more in the C general style. But I have no argument like the above to help me on this one. -- Benson I. Margulies