Answer the question
In order to leave comments, you need to log in
What is the lifetime of the variable?
std::forward_list <T> copy_list(const std::forward_list<T>& in)
{
auto out = in;
out.reverse();
return out;
}
Answer the question
In order to leave comments, you need to log in
auto out = in;
The type of the `out` variable will be `std::forward_list<T>`. [Explanation 1] , [Explanation 2]
So `out ` will be a local variable and will have a local lifetime.
The only exception is temporary lifetime extension . Then the lifetime of the variable will be extended.
The lifetime of a temporary object may be extended by binding to a const lvalue reference or to an rvalue reference (since C++11), see reference initialization for details.
On the stack, a place is separately reserved for the value returned by the function, and it is there, and not at the address of the out variable, that the result is written through return. And, accordingly, further work occurs precisely at the address with the returned value.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question