Path: utzoo!utgpu!watmath!att!tut.cis.ohio-state.edu!cs.utexas.edu!usc!orion.cf.uci.edu!uci-ics!ucla-cs!rjc
From: rjc@maui.cs.ucla.edu (Robert Collins)
Newsgroups: comp.lang.c++
Subject: Re: named return values
Message-ID: <26314@shemp.CS.UCLA.EDU>
Date: 9 Aug 89 03:41:37 GMT
References: <1826@cmx.npac.syr.edu> <26302@shemp.CS.UCLA.EDU> <6444@columbia.edu>
Sender: news@CS.UCLA.EDU
Reply-To: rjc@cs.ucla.edu (Robert Collins)
Organization: UCLA Computer Science Department
Lines: 45

In article <6444@columbia.edu> kearns@cs.columbia.edu writes:
>I am in favor of named return values; however....
> [ ... ]
>matrix m1, m2, m3;
>...
>m1 = m1 + 2*m2 + m3;
> [ rewrite as ]
>m1.plus(matrix(m2).times(2)).plus(m3)

That is REALLY ugly.  I am using C++ so I don't have to write
code like that!

>(assume that matrix(m2) is a constructor returning a matrix equal to
>m2.)  Admittedly, the syntax is much less readable.  On the other
>hand, it gives the programmer the chance to optimize his expressions
>and significantly reduce the number of large temporaries.  It is also
>more "honest":  matrices are NOT good candidates for having value semantics
>because their copying time is large.  

Part of this cost is unnecessary copies that named return values can
help allieviate.

>By writing in this style when appropriate, most uses of named return values
>go away.

Yes, if you write code like that, you don't need named return values.  I
don't know anyone who wants to write code like that!  I think the fact
that people are tempted to write in this sort of style is a very strong
argument for named return values!  Anyone who is considering writing
code in C++ and sees examples in this `efficient' style is going to
run away screaming into the night.  Just say no.

>-steve
>(kearns@cs.columbia.edu)







rob collins
-------------------------------------------------------------------------------
rjc@cs.ucla.edu	                    C++/Paris on the CM2:  The only way to fly.
-------------------------------------------------------------------------------