Maybe.
In C++11, values are returned by moving, rather than copying, if they have a move constructor. This can be much more efficient than copying.
In some circumstances - such as when returning a local variable or a temporary (as you do here) - the move or copy can be elided. The value is created directly into the caller's stack frame, so that it doesn't need to be moved or copied on return. People who like acronyms sometimes call this (N)RVO - (Named) Return Value Optimisation.
Likewise, the copy or move from the temporary return value to x
can also be elided.
Any decent compiler will implement this optimisation, so your code should only create a single Foo
. You can verify this by making the destructor print a message, and observing that it only does this once: http://ideone.com/xydJqY. If you disable the optimisation, there may be up to three objects.