🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2197

Re: Why not adopt gcc's "return" optimization?

#2197
From: kanze
Date: Wed, 11 Oct 1995 17:24
83 lines
3292 bytes
In article <45ejq1$ojo@engnews2.Eng.Sun.COM> clamage@Eng.Sun.COM
(Steve Clamage) writes:

|> In article I0J@netcom.com, arnstein@netcom.com (David Arnstein) writes:
|> >Gcc's "return" extension to c++ looks like this:

|> >   Integer  operator+  (const Integer& x, const Integer& y) return r
|> >   {
|> >     add(x, y, r);
|> >   }

|> >The "return r" clause in the first line prevents a copy of an Integer
|> >object on function return.

|> >Has the committee considered this extension?  It seems so sensible
|> >that I wonder why it was never adopted.

|> It seems to me that this extension does not involve any semantic change,
|> but provides only an explicit hint for an optimization. I don't see that
|> this hint tells the compiler any more than it could figure out for itself
|> in the absence of the extension. In many cases compilers can and do
|> optimize away extra copies of a returned value.

I suspect that it would depend on the semantics actually given the
return variable.  At present, the compiler cannot optimize out a named
variable; the variable named by return being a special case, the
committee could authorize eliding it.

Take for example the classic (and obvious) implementation of
operator+:

	MyClass
	operator+( MyClass const& op1 , MyClass const& op2 )
	{
	    MyClass         result( op1 ) ;
	    op1 += op2 ;
	    return op1 ;
	}

When used as an initializer, for example, the compiler must (under the
present rules) call two constructors: one for result, and then a copy
constructor for the named variable being initialized, since both of
the variables are named.

Traditionally, programmers are thus encouraged to avoid the named
variable, writing something like the following:

	MyClass
	operator+( MyClass const& op1 , MyClass const& op2 )
	{
	    return MyClass( op1 ) += op2 ;
	}

Personally, I find this very ugly, and would much prefer something
along the lines of:

	MyClass
	operator+( MyClass const& op1 , MyClass const& op2 )
	    return result( op1 )
	{
	    result += op2 ;
	}

This could generate the same code if the standard gave the compiler
explicit autorization to elide the variable named in the return
clause.

This said, I think it is a minor point, and it is much to late to be
considering such proposals now.
--

James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils en informatique industrielle --
                              -- Beratung in industrieller Datenverarbeitung



---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <9510110950.AA01204@slsvgqt.lts.sel.alcatel.de>
Path: arc1-ca.pugleaf.net!128.32.155.1!matt
References: <arnsteinDG8zHt.I0J@netcom.com> <45ejq1$ojo@engnews2.Eng.Sun.COM>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!128.32.155.1!matt
From: kanze <kanze@lts.sel.alcatel.de>
Newsgroups: comp.std.c++
Subject: Re: Why not adopt gcc's "return" optimization?
Date: 11 Oct 1995 17:24:15 GMT
Organization: <none>
Approved: matt@physics2.Berkeley.EDU (Matt Austern)
Message-ID: <9510110950.AA01204@slsvgqt.lts.sel.alcatel.de>
References: <arnsteinDG8zHt.I0J@netcom.com> <45ejq1$ojo@engnews2.Eng.Sun.COM>
NNTP-Posting-Host: physics2.berkeley.edu