On 2007-12-27 23:06, Tristan Wibberley wrote:
On Thu, 2007-12-27 at 16:23 +0100, Tadeusz B. Kopec wrote:
Output is,
I am in constructor
I am in constructor
I am in copy constructor
My expected output is,
I am in constructor
I am in constructor
The standard says that compiler is allowed to elide copy in return value
but doesn't require it. Whether a specific compiler does this
optimisation or not is implementation defined so off topic here.
I understood that the copy constructor must not be elided if it would
result in a different program. IE, any statistics gathering, reference
counting or custom allocators being used prevents elision (is that a
word?) except where the compiler can (and will) prove that the elided
version would give the same result in all cases.
Actually no, the copy-constructor may always be elided, even if that
changes the effects of the program. That is why it is possible to
demonstrate the effects using print-outs in the copy constructor.
Because of this a copy-constructor should be designed to only copy an
object and nothing more, and you should never design your programs to
rely on the copy-constructor running.
--
Erik Wikström
.