If you return a value from a method and you assing it to the a const reference, that value will be kept alive as long as the const reference is alive:
string get() { return "string"s; }
int main()
{
const string& ref_to_temp = get();
cout << ref_to_temp << endl;
}
Better explaination: https://herbsutter.com/2008/01/01/gotw-88-a-candidate-for-the-most-important-const/ or stackoverflow: https://stackoverflow.com/questions/2784262/does-a-const-reference-class-member-prolong-the-life-of-a-temporary
Or standard explaination: 8.5.3/5
, here