這能正常工作嗎? (請參閱示例)從函數返回的unique_ptr的作用域是什麼?
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?
}
+1表示精確和不透明。 – briantyler 2011-05-23 13:28:03
所以函數doSomething在unique_ptr被銷燬之前執行? – tache 2011-05-23 13:34:42
@tache:是的。對於任何返回值都是如此。如果它立即超出範圍,從函數返回*任何內容都將毫無用處。 :) – jalf 2011-05-23 13:41:44