Path: utzoo!utgpu!watserv1!watmath!att!dptg!ulysses!andante!alice!ark From: ark@alice.UUCP (Andrew Koenig) Newsgroups: comp.std.c++ Subject: Re: Unnecessary copying of returned object Message-ID: <11267@alice.UUCP> Date: 1 Sep 90 13:21:56 GMT References: <1990Aug31.234937.29938@athena.mit.edu> <1990Aug31.235606.166@athena.mit.edu> Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 27 In article <1990Aug31.235606.166@athena.mit.edu>, ahodgson@hstbme.mit.edu (Antony Hodgson) writes: > Vector Vector::operator + ( Vector & A ) > { > Vector temp; > // for all valid i, temp[i] = (*this)[i] + A[i] > return temp; // copy of temp made here > } // temp deallocated here > Could an intelligent compiler bypass this copying without the help of > any new syntax? Yes. Moreover, I hear that at least one commercially available C++ implementation already does this. One fairly easy to express such an optimization would be to say that `if every return statement in a function returns the same local variable, and that local variable has the same type as the function's return value, then the compiler should alias the local variable to the return value.' The language definition permits this optimization. -- --Andrew Koenig ark@europa.att.com