Topic: temporary objects vs local objects
Author: Jason Merrill <jason@cygnus.com>
Date: 1997/04/11 Raw View
>>>>> Yannis <goulerma@csc.umist.ac.uk> writes:
> X X::f ( const X& v)
> {
> X temp;
> // ............process "temp" using "this" and "v"..........
> return(temp);
> }
> QUESTION:
> Is there any possible way to avoid 2 destructions and 2 constructions.
This is a quality of implementation issue. A compiler is allowed to
recognize that the return value is always a copy of 'temp' and assign them
to the same location. Do any compilers currently perform this
optimization?
Jason
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: jpotter@falcon.lhup.edu (John E. Potter)
Date: 1997/04/11 Raw View
Yannis (goulerma@csc.umist.ac.uk) wrote:
: The method f takes an object X by reference and returns an object X. f
: should not change the "this"
: X X::f ( const X& v)
: {
: X temp;
: // ............process "temp" using "this" and "v"..........
: return(temp);
: }
: QUESTION:
: Is there any possible way to avoid 2 destructions and 2 constructions. ( I
: cannot avoid the declaration of the "temp").
This is the return value optimization. It is now allowed by the CD. It
was incorporated in USL C++ (cfront) a long time ago. In that case, it was
enabled by the "obvious" switch of a user defined copy constructor which
would be your case. I have no other compilers which implement this
optimization but expect that it will become common now that it has been
blessed by the draft. In nice clean code like yours, it is fairly easy.
The return variable is declared first and there is only one return at
the end where it belongs. The compiler removes temp, constructs it in
the space for the return value and performs all operations in that space.
See D&E page 84.
John
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: John Lilley <jlilley@empathy.com>
Date: 1997/04/10 Raw View
Yannis wrote:
> This causes a construction for "temp", a construction for the return
> temporary object, a destruction for "temp", while exiting the function f,
> and later on, when the scope of the expression in the caller vanishes, the
> destruction of the return temporary object.
>
> NOTE: X contains dynamic data new'ed & delete'd by the constructor &
> destructor where applied.
>
> QUESTION:
> Is there any possible way to avoid 2 destructions and 2 constructions.
There are a few approaches. The most general one is to write a
"smart-pointer" for X, so that the actual body of X is not
constructed/destructed when it is passed around by value. If you make
the ctor/dtor of the smart pointer inline, then the hit is minimal. See
Scott Meyer's "More Effective C++" for details.
A less general solution is to implement specific reference-counting
inside your class, like the Standard C++ basic_string/string. This is
better if you want copy-on-write semantics in addition to simple
reference-counting.
john lilley
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Yannis <goulerma@csc.umist.ac.uk>
Date: 1997/04/06 Raw View
I' ve been working on C for quite some time, but I'm new in C++, so the
issue is as follows:
The method f takes an object X by reference and returns an object X. f
should not change the "this"
X X::f ( const X& v)
{
X temp;
// ............process "temp" using "this" and "v"..........
return(temp);
}
This causes a construction for "temp", a construction for the return
temporary object, a destruction for "temp", while exiting the function f,
and later on, when the scope of the expression in the caller vanishes, the
destruction of the return temporary object.
NOTE: X contains dynamic data new'ed & delete'd by the constructor &
destructor where applied.
QUESTION:
Is there any possible way to avoid 2 destructions and 2 constructions. ( I
cannot avoid the declaration of the "temp").
I could declare "temp" as a X*, new'it and not delete'it, but obviously
after the scope of the caller exression vanishes, the dynamic data declared
private in X will be lost in the heap.
THANKS IN ADVANCE.................
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]