From 6611506117642396267
X-Google-Thread: f78e5,bd52daa8ac2e51f1,start
X-Google-Thread: 109fba,bd52daa8ac2e51f1,start
X-Google-Attributes: gidf78e5,gid109fba,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
Path: g2news2.google.com!news3.google.com!out04a.usenetserver.com!news.usenetserver.com!in04.usenetserver.com!news.usenetserver.com!nx01.iad01.newshosting.com!newshosting.com!news.alt.net!comp-std-cpp-robomod!not-for-mail
From: "terminator(jam)" <farid.mehrabi@gmail.com>
Newsgroups: comp.std.c++,comp.lang.c++
Subject: pre return optimization
Date: Tue,  9 Oct 2007 09:56:13 CST
Organization: http://groups.google.com
Lines: 105
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
Message-ID: <1191919832.635392.256930@d55g2000hsg.googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
X-Trace: posting.google.com 1191919832 24688 127.0.0.1 (9 Oct 2007 08:50:32 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Tue, 9 Oct 2007 08:50:32 +0000 (UTC)
Return-Path: <devnull@stump.algebra.com>
X-Authentication-Warning: mulga.csse.unimelb.edu.au: fjh set sender to devnull@stump.algebra.com using -f
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-Original-To: std-c++@mailman.ucar.edu
Delivered-To: std-c++@mailman.ucar.edu
X-SMTP-Auth: no
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Media Center PC 3.0; .NET CLR 1.0.3705; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2),gzip(gfe),gzip(gfe)
X-HTTP-Via: 1.1 krj-cache-1.morva.net:3128 (squid/2.6.STABLE13)
Complaints-To: groups-abuse@google.com
Injection-Info: d55g2000hsg.googlegroups.com; posting-host=84.47.210.130;
   posting-account=ps2QrAMAAAA6_jCuRt2JEIpn5Otqf_w0
X-Virus-Scanned: amavisd-new at ucar.edu
X-Virus-Scanned: amavisd-new at csse.unimelb.edu.au
X-Virus-Scanned: amavisd-new at csse.unimelb.edu.au
Xref: g2news2.google.com comp.std.c++:781 comp.lang.c++:21819

consider:

struct memory_pig{//a really large type:

	memory_pig(){
		std::cout<<"mem pig default\n";
		//etc...
	};

	memory_pig(memory_pig const&){
		std::cout<<"mem pig copy\n";
		//etc...
	};

	~memory_pig(){
		std::cout<<"mem pig finish\n";
		//etc...
	};

	//etc...

};///struct memory_pig

memory_pig foo(){
	memory_pig result;
	result=something;
	//etc...
        result=something_else;
	return result;
};

any time 'foo' is called the output will contain the following
sequence:


mem pig default
mem pig copy
mem pig finish

the last line of output may repeat based on how the result is
stored(rvo) or not.
So,two objects of a large type will be constructed and at least one is
destructed on every call to 'foo' in PASCAL you can write:

function foo:memory_pig
begin
	foo:=something;
	{etc...}
	foo:=somthing_else;
end

that is you can refrence the returned object inside the function and
decrease the overhead for copying large objects.C++ lacks such syntax
and IMHO we should be able to mark the result object as referencing
the actual return so that there is no need for the extra copy
construction;this is espesifically beneficall when dealing with
operator definitions .We can declare the return itself as an object.I
suggest the following syntax:

class ret_type funxn (paramlist){
	ret_type return /*optional:*/(initiallizer params);

        //etc...

        if(false)return;//return like void functions

        //return something;//error:named return accepts no param.

        return=something;//ok;
        return=something_else;
        return.member_funxn();
	another_funcxn(return);

};

Provided that 'return' is declared as an object { whenever you use the
'return' keyword inside pharantesis or accompanied via an operator ?
the return object is referenced :otherwise a parameterless 'return'
returns to the caller.
The return object is an lvalue inside the function and can be aliased
for readability:

memory_pig foo(){
	memory_pig return , & result=return;
	result=something;
	//etc...
        result=something_else;
};

and whenever 'foo' is called the output looks like this:

mem pig default

the unnessesary copy/move has vanished and the destruction of
temporary depends on what you do with it.

regards,
FM.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]



