Does returning a reference extend its lifetime too?

Viewed 672

AFAIK, in the following code the lifetime of the reference ro1 is extended till the end of the scope (function g()):

class Some {
  // Implementation here
};
Some f() {
  return Some(/* constructor parameters here*/);
}
void g() {
  Some&& ro1 = f();
  // ro1 lives till the end of this function
}

How about returning this reference? Will the object still live in g1(), or will it be destructed upon the exit from h()?

Some&& h() {
  Some&& ro1 = f();
  // Code skipped here
  return std::forward<Some>(ro1);
}

void g1() {
  Some&& ro2 = h();
  // Is ro2 still refering to a valid object?
}
1 Answers
Related