Article View: net.lang.c++
Article #31Re: further C++ fix opportunities
From: John Rose
Date: Thu, 27 Feb 1986 16:48
Date: Thu, 27 Feb 1986 16:48
47 lines
2513 bytes
2513 bytes
In article <34200001@orstcs.UUCP> nathan@orstcs.UUCP (nathan) writes:
> In returning a structure, one assigns to
>fields of a local variable, then returns it.
>To avoid [exta copying], some sort of "self" structure is needed.
>I propose three alternatives:
>1. .a = 1;
As you say, what do you call the whole structure?
Also, it uses an intriguing general syntax for
a very small purpose.
>2. struct ab func() {
> func.a = 1;
> return func; // type-checking resolves return value
> }
Type checking does not resolve anything if there is a conversion
operator from (struct ab (*)()) to (struct ab)--not inconceivable.
>3. struct ab func() {
> this.a = 1; this.b = 2;
Confusing to have "this" be pointer or struct depending on usage?
>Clearly there are other alternatives.
How about something ugly, unmistakable, and specific:
4. return.a = 1; return.b = 2; // and, subr(&return)
By the way, if the copying bothers you, you can use reference types
in new code. E.g.: struct ab& func() {....}
There's actually a deeper problem: Values are returned not by
assignment but by initialization. That's a big difference in C++.
So "subr(&return)" would pass a pointer to an uninitialized object
to subr(), and so on. This is probably an unacceptible breach of
C++ data integrity rules. So C++ syntax for getting at the
yet-to-be-returned structure is a bad idea. But all those copying
steps mentioned are not a problem in C++. This C code:
temp.a = 1; temp.b = 2; return temp;
could be replaced by this C++ code:
return ab(1, 2);
(assuming definition of ab::ab(,)) and the compiler is responsible
for making it efficient.
--
----------------------------------------------------------
John R. Rose, Thinking Machines Corporation, Cambridge, MA
245 First St., Cambridge, MA 02142 (617) 876-1111 X270
rose@think.arpa ihnp4!think!rose
Technical Details
Message-ID:
<4439@think.ARPA>Path:
arc1-ca.pugleaf.net!utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!roseReferences:
<34200001@orstcs.UUCP>Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/17/84; site think.ARPA Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!rose From: rose@think.ARPA (John Rose) Newsgroups: net.lang.c++ Subject: Re: further C++ fix opportunities Message-ID: <4439@think.ARPA> Date: Thu, 27-Feb-86 11:48:38 EST Article-I.D.: think.4439 Posted: Thu Feb 27 11:48:38 1986 Date-Received: Sat, 1-Mar-86 04:19:32 EST References: <34200001@orstcs.UUCP> Reply-To: rose@think.UUCP (John Rose) Distribution: net Organization: Thinking Machines, Cambridge, MA Lines: 47