TITLE: return value optimization (Newsgroups: comp.lang.c++.moderated, 7 Feb 97) JARVI: Jaakko Jarvi | > Is there a difference between the following two definitions: | > | > X function() { | > return X(something); | > } | > | > and | > | > X function() { | > X temp(something); | > return temp; | > } ERNST: Jay Ernst | The *first* one can be much more efficient depending on how well your | compiler optimizes the code. According to Scott Meyers (More Effective C++, | Item 20), "The rules for C++ allow compilers to optimize temporary objects | out of existence." This is known as "return value optimization," and it can | eliminate *both* the temporary in the function *and* the temporary copy | returned! MEYERS: smeyers@teleport.com (Scott Meyers) This is true, but in July 1996 the standardization committee clarified that the optimization may also be performed when the function is written the second way. According to the standard, then, both versions of the function are equally optimizable. Whether current compilers treat them the same, I don't know. I suspect some compilers will still optimize the first form more readily than the second.