如果函数Foo()将std::unique_ptr的所有权转移到函数Bar()并假设Bar()抛出异常,则std::unique_ptr中包含的对象将被销毁。
在这种情况下,如何处理Foo()可能希望保留所有权的情况。
class A
{
public:
std::unique_ptr<SomeData> getSomeData(...);
};
class B
{
public:
pushToQ(std::unique_ptr<SomeData>);
doSomething()
...
popFromQ();
...
};现在,如果B::pushToQ()抛出QueueFullException,我将释放由getSomeData()生成的数据,而这可能是我不想要的。
发布于 2014-11-13 17:20:51
你要么转移所有权,要么不转让。如果您想要传输,那么您不应该关心Bar是否可以抛出并杀死您的对象:
// i do not care what bar does with ptr, i am done with it
bar(std::move(ptr));如果您可能希望保留所有权,那么转移所有者是错误的解决方案。或者您希望通过引用传递您的unique_ptr,或者只需要取出原始指针,或者甚至只使用shared_ptr。这取决于你的用例。但没有半途而废的所有权转移。
这里有一些例子。谁是首选完全取决于你:
bar(std::unique_ptr<Data>& ptr) {
// not transferring, caller still owns the data
something_that_might_throw();
something_that_might_not();
// ok got here?
std::unique_ptr<Data> myPtr = std::move(ptr);
// MINE NOW!!!
}以上是“以异常安全的方式将unique_ptr移动到容器中的唯一例外安全解决方案,并有力地保证系统不会损坏”(来自MGetz)
或者:
bar(Data* ptr) {
// it really doesn't matter, caller always owns the data
// just don't go doing something like this
std::unique_ptr<Data> awful_idea(ptr);
// now two separate people both think they own the data
// and will both try to delete it. That'll work the first time...
}或者是一个更好的版本,除非你真的很努力,否则你不能搞砸它。
bar(Data& data) {
// well clearly the caller owns it
}或者:
bar(std::shared_ptr<Data> ptr) {
// we both share the data
// it won't get deleted until both bar() and caller delete it
}发布于 2014-11-13 17:18:27
那就别转移所有权。
相反,将一个SomeData&传递给Bar()。
发布于 2014-11-13 17:23:15
您可以使用std::shared_ptr而不是std::unique_ptr,如果pushToQ()成功,则释放实例,否则保留副本。
https://stackoverflow.com/questions/26914534
复制相似问题