In C++ when you have the following:
std::string get_string();
std::string const& v = get_string();
The lifetime of the temporary returned from get_string() is extended for the same lifetime of the reference v;
If I have the following:
std::string const& get_string(std::string const& p) {
return p;
}
std::string const& v =
get_string(std::string{"Hello"});
Is the lifetime of the temporary extended? or is this a dangling reference;
My understanding is that the temporary is bound to the lifetime of p and that only exists for the duration of the function and that secondary references to the temporary dont extend the lifetime.
What is the expected result?