Would this work properly? (see example)
unique_ptr<A> source()
{
return unique_ptr<A>(new A);
}
void doSomething(A &a)
{
// ...
}
void test()
{
doSomething(*source().get()); // unsafe?
// When does the returned unique_ptr go out of scope?
}
A
unique_ptrreturned from a function does not have scope, because scope only applies to names.In your example, the lifetime of the temporary
unique_ptrends at the semicolon. (So yes, it would work properly.) In general, a temporary object is destroyed when the full-expression that lexically contains the rvalue whose evaluation created that temporary object is completely evaluated.