I know that I can use "Lifetime Extension Reference Binding" to keep a temporary around for the duration of the local scope const auto& temp = "lorem ipsum"s
but I was curious if there was a way to force the duration beyond the local scope without converting it to a permenant object.
More explicitly, I'm wondering if I could do: const auto& temp = foo();
Attempt 1:
const string& foo() {
const auto& temp = "lorem ipsum"s;
return temp;
}
Attempt 2:
struct bar {
const string& temp;
};
const bar foo() {
const auto& temp = "lorem ipsum"s;
const bar result = {temp};
return result;
}
Is there a way that I haven't thought of which will let me get temp
outside the local scope without converting it to a permanent object?