p
будет уничтожен при выходе из функции g
, как и любая другая переменная с локальной областью видимости. Только в это время он больше не хранит данные;чтобы понять, вам на самом деле не нужна отдельная функция, просто подумайте:
int* n = new int(42);
{
std::unique_ptr<int> p(n); // p how owns the value pointed to by n
// for brevity, I'll say p owns n from now on,
// although technically not correct...
{ // opening another scope! (corresponds to function call)
std::unique_ptr<int> q; // another variable, nothing else is a
// function parameter either...
q = std::move(p); // this happens, too, when calling a function
// at this point, the ownership is transferred to q
// q now owns n, p is left with just nothing
// (i. e. now holds a null-pointer)
} // at this point, q is going out of scope; as it is the current
// owner of n, it will delete it
// p still is in scope, but it has transferred ownership, remember?
} // at this point, p is destroyed; as it doesn't own anything at all any more
// it dies without doing anything either...