views:

140

answers:

5
+3  A: 

The compiler is permitted to elide the call to a copy constructor when an object is returned from a function.

That is, it is not required to actually call the copy constructor: it can just construct the object to be returned in whatever place the object needs to be to be returned from the function.

James McNellis
+9  A: 

I believe you've encountered return value optimization (RVO) http://en.wikipedia.org/wiki/Return_value_optimization

fingerprint211b
A: 

It's legal to ellide copies, even when the copy constructor has side effects. It's called RVO (there's also one for named (comment: thanks) values, NRVO) and is explicitly allowed by the Standard.

DeadMG
The N in NRVO stands for "named," not "new."
James McNellis
Close enough :P
DeadMG
+2  A: 

It looks like RVO (Return Value Optimization). Your compiler sees that you are not doing anything with the 'b' instance nor with its returned copy so it removes it (object copy operation) from the compiled output.

Amardeep
b is not removed. There's just no reason to copy it. Copy elision works by merging source and target object into a single object. When NRVO is applied, b refers to the same object the function returns.
sellibitze
@sellibitze: Sorry for the imprecise use of pronouns. The 'it' that gets removed is the call to the copy constructor. The compiler won't optimize out 'b' object altogether because of possible constructor side effects. Those side effects are not given a slide as they are with RVO. Thanks for the comment.
Amardeep
+6  A: 

Hi,

The web-wide famous C++ FAQ Lite (which you can find here for instance) is a must-read for every C++ programmer.

Your question probably corresponds to that one :
[10.9] Does return-by-value mean extra copies and extra overhead?

LeGEC