tiemann@LURCH.STANFORD.EDU (Michael Tiemann) (11/22/89)
Why not allow a third form, where the caller would pass as a third argument
a reference to the place where the result should be put?
void operator+(matrix& x, matrix& y, matrix& result) { ... }
The previous example would translate into:
matrix U;
operator+(A,B,U);
operator+(U,C,X);
and no copying would be necessary. Am I missing something obvious?
Daniel Ranson
X400: ranson@lannion.cnet.fr
uucp: ranson@cnetlu.fr, or ...!mcvax!inria!cnetlu!ranson
Why not allow a fourth form, using Named Return Values (see
July/August C++ Report for details of this GNU C++ language
extension):
matrix operator+(matrix& x, matrix& y) return z { ... }
This permits the compiler to give the effiency of return-by-reference
with the semantics of return-by-value.
Michaelrae%alias@csri.utoronto.ca (Reid Ellis) (11/28/89)
tiemann@lurch.stanford.edu writes: |Why not allow a fourth form, using Named Return Values (see |July/August C++ Report for details of this GNU C++ language |extension): | |matrix operator+(matrix& x, matrix& y) return z { ... } And look! It's extensible! Later we can add multiple return values! matrix operator+(matrix& x, matrix& y) return x,y,z { ... } Ya, dat's it.. :) But seriously, what if operator+ is used inside an expression in this new case? Then the compiler generates a temp on its own, I guess.. e.g.: A=(b+c)*2; Reid --- Reid Ellis, 264 Broadway Avenue, Toronto ON, M4P 1V9, Canada rae%alias@csri.utoronto.ca +1 416 487 1383
tiemann@LURCH.STANFORD.EDU (Michael Tiemann) (11/28/89)
But seriously, what if operator+ is used inside an expression in this new
case? Then the compiler generates a temp on its own, I guess..
e.g.: A=(b+c)*2;
In fact, it does generate a temporary. But you missed the point. The
point was to avoid needless copying objects, not to avoid the creation
of temporaries. It is often the case that copy elimination avoids
temporary creation, but not always.
Michael