Am I right, that this (taken from this GotW):
string f() { return "abc"; }
void g() {
const string& s = f();
cout << s << endl; // can we still use the "temporary" object?
}
is completely fine, while this is not ok at all:
const string& foo() { string x{"abc"}; return x; }
void bar() {
const string& y = foo();
}
?
Why is it different? Can one say that "the lifetime of a temporay is not extended across a function call (even if bound to a const ref)" ? Or what is an explanation, why the first is ok, but not the second?