local lvalue references-to-const and rvalue references can extend the lifetime of temporaries:
const std::string& a = std::string("hello");
std::string&& b = std::string("world");
Does that also work when the initializer is not a simple expression, but uses the conditional operator?
std::string&& c = condition ? std::string("hello") : std::string("world");
What if one of the results is a temporary object, but the other one isn’t?
std::string d = "hello";
const std::string& e = condition ? d : std::string("world");
Does C++ mandate the lifetime of the temporary be extended when the condition is false?
The question came up while answering this question about non-copyable objects.
Both of those are fine.
§5.16 says (extraordinarily abridged):
Nope.
Nope.
Nope. (In the first, both are prvalues and in the second one is a glvalue and one is a prvalue.)
Okay, so both of these result in prvalues. So the binding is fine, but what’s the binding to?
Okay, so both are now rvalues if they weren’t already.
Okay, so it’s either
std::string(first_operand)orstd::string(second_operand).Regardless, the result of the conditional expression is a new prvalue temporary, and it’s that value that’s extended by binding to your references.