Search code examples
c++referencelvaluervalue

Returning const lvalue reference to rvalue temporary? Why does this work?


Why does this code work? It prints out 60 every single time. First of all, const thingy& indicates that the function returns a reference to an already existing variable, not a nameless construction. Secondly, shouldn't that temporary die when the function returns, thus creating a null reference? I am using whatever the latest GCC version is on OSX... Can somebody please explain to me why this works?

#include <iostream>

using namespace std;

struct thingy {
    double things;
    double moreThings;
    double evenMoreThings;
};

const thingy& getThingy() {
    return {60, 60, 60};
}

int main() {
    cout << getThingy().evenMoreThings << endl;
}

And if that works, then why doesn't this?

const thingy& getThingy() {
    thingy t{60, 60, 60};
    return t;
}

Solution

  • The compiler is performing the Return Value Optimization here.

    https://en.wikipedia.org/wiki/Return_value_optimization

    The compiler is able to take the value constructed at the return and won't even need to copy it. However, in the example where you construct the struct inside the function, it really is a local variable, and thus falls out of scope at the end of the function, invalidating the reference.