On Oct 16, 8:44 am, int...@gmail.com wrote:
> On Oct 9, 7:56 pm, "terminator(jam)" <farid.mehr...@gmail.com> wrote:
>
>
>
>
>
> > 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
>
> It won't. It certainly won't do that with the example given, since
> there's no clear association in it between the copy constructor and
> the assignment operator (considering that the latter isn't even
> defined). Assuming there was such an association, though, I'd rather
> expect to see:
>
> mem pig default
> mem pig copy // first assignment
> mem pig copy // second assignment
> mem pig finish
>
As I wrote before its just an illustration and I did not mean any
assignment,replace it with any function( eg. modify(result)

.
> Which is correct and fine - if you have explicitly written the
> assignment operator twice, then surely you want the associated side-
> effects? And if you do not, then perhaps you shouldn't have written it
> that way.
>
> > 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 precisely what NRVO does.
>
I was certain about the NRVO by the time I started the post.However
NRVO is a compiler option(ugly!!!) not a demand .With NRVO we have two
distinct phases optimize simultaniously : prior to return inside the
callee and after that inside the caller:
A foo(){
A ret;//this will be return under any conditions.
//....
return ret;//pre return :no copy/move
};
class A a=foo();//after return : copy/move to a
> I have a feeling that you're confusing assignment with initialization
> in this case. Assignment operator always applies to an already-
> initialized object - the compiler cannot elide the initialization
> except for the trivial cases (e.g. POD types), which is why operator=
> still results in unnecessary copying even when the compiler implements
> NRVO. It's not a problem with NRVO though - it's because assignment is
> used where just initialization would suffice. Direct initialization is
> still more efficient then assignment-following-initialization, even
> with the move semantics of C++0x.
Everybody makes mistakes while learning , but confusing assignment
with copy certainly has never been the mistake I can make.
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 ]