The first feature presented in the question is
const std::string &f() { const std::string &s = "hello"; static const std::string &ss = s; return ss; }
gives Undefined Behavior if a return link is used. The specified object ceases to exist when the first function call is returned. And subsequent ss calls are a dangling link.
life extension standard paragraph context
C ++ 11 §12.2 / 4" There are two contexts in which temporary objects are destroyed at a different point than the end of the full expression
Ie, all this concerns temporal times that would otherwise be destroyed at the end of the full expression they created.
One of two contexts: with four exceptions noted
C + 11 §12.2 / 5" & hellip; when the link is bound to [such] temporary
In the above code, the temporary std::string created by the full expression "hello" is bound to the s link, and the lifetime is extended to the region s , which is the body of the function.
Subsequent declaration and initialization of the static link ss does not include the full expression that creates the temporary. Its s initialization expression is not temporary: it is a reference to a local one. Therefore, it is out of context covered by the paragraph of the extension of life.
But how do we know what this means? Well, making sure that the dynamic dynamics refer to what was originally temporary is not computable for the general case, and the C ++ language standard does not include such distant concepts. So simple, really.
IMHO a more interesting case, approx. formal rules
#include <string> #include <iostream> using namespace std; template< class Type > auto temp_ref( Type&& o ) -> T& { return o; } auto main() -> int { auto const& s = temp_ref( string( "uh" ) + " oh!" ); cout << s << endl; }
I argue that there is no extension of lifespan here, and that the output statement using the s reference uses a sagging link with the result of UB.
And I think that this conclusion, unlike the choice of OP, for example, cannot be affirmed solely on the basis of the standard formulation, because (it seems to me) the standard formulation is a little inferior. That he cannot make an exception for reference types. But maybe I'm wrong, and if I find out that I will update this answer to reflect this new understanding.