Does introducing a new variable defeat return value optimisation?

Viewed 151

We all know that

Foo returnAFoo()
{
    return Foo();
}

will be compiled with return value optimisation so a value copy will not be taken even if the copy constructor of Foo has side effects. But will

Foo returnAFoo()
{
    Foo f = Foo();
    return f;
}

too? The second construct can be helpful when debugging. But am I throwing away an important optimisation in doing so? Perhaps I need to write an explicit move constructor?

1 Answers
Related