Mar*_*ork 182 c++ smart-pointers auto-ptr unique-ptr c++11
随着新标准的出现(以及某些编译器中已有的部件),新类型std::unique_ptr应该是替代品std::auto_ptr.
它们的用法是否完全重叠(因此我可以对我的代码进行全局查找/替换(不是我会这样做,但如果我这样做))或者我应该注意一些在阅读文档时不明显的差异?
此外,如果它是一个直接替代品(为什么给它一个新的名称),而不仅仅是改善std::auto_ptr.
Cub*_*bbi 214
您无法进行全局查找/替换,因为您可以复制auto_ptr(具有已知后果),但unique_ptr只能移动.任何看起来像
std::auto_ptr<int> p(new int);
std::auto_ptr<int> p2 = p;
Run Code Online (Sandbox Code Playgroud)
必须至少成为这样的
std::unique_ptr<int> p(new int);
std::unique_ptr<int> p2 = std::move(p);
Run Code Online (Sandbox Code Playgroud)
至于其他差异,unique_ptr可以正确处理数组(它将调用delete[],而auto_ptr将尝试调用delete.
def*_*ode 90
std::auto_ptr并且std::unique_ptr在某些方面是不相容的,而在其他方面则是替代品.所以,没有找到/替换不够好.然而,在通过编译错误进行查找/替换之后,应该修复除了奇怪的角落情况之外的所有内容.大多数编译错误都需要添加一个std::move.
unique_ptrs必须通过std::move调用传递.这个很简单,因为如果你没有把它弄好,编译器会抱怨.std::auto_ptr复制语义是邪恶的.如果班级不允许复制,那么std::unique_ptr就是替代品.但是,如果您尝试为该类提供合理的复制语义,则需要更改std::auto_ptr处理代码.这很简单,因为如果你没有把它弄好,编译器会抱怨.如果你允许复制一个没有任何特殊代码的std::auto_ptr成员的课程,那么你就会感到羞耻,祝你好运.总之,std::unique_ptr是一个不间断的std::auto_ptr.它在编译时不允许使用a时经常出错的行为std::auto_ptr.因此,如果您使用std::auto_ptr所需的护理,切换到std::unique_ptr应该很简单.如果你依赖于std::auto_ptr奇怪的行为,那么无论如何你都需要重构你的代码.
Unc*_*ens 34
AFAIK,unique_ptr不是直接替代品.它修复的主要缺陷是所有权的隐性转移.
std::auto_ptr<int> a(new int(10)), b;
b = a; //implicitly transfers ownership
std::unique_ptr<int> a(new int(10)), b;
b = std::move(a); //ownership must be transferred explicitly
Run Code Online (Sandbox Code Playgroud)
另一方面,unique_ptr将具有全新的功能:它们可以存储在容器中.
| 归档时间: |
|
| 查看次数: |
57490 次 |
| 最近记录: |