在了解auto_ptr、unique_ptr和shared_ptr时,我发现auto_ptr析构函数使用delete而不是delete[],然而unique_ptr能正确处理它。
auto_ptr<char> aptr(new char[100]);
unique_ptr<char []> uptr(new char[100]);
无论如何,auto_ptr在c++11中已经被弃用。我知道unique_ptr比auto_ptr功能更强大。
我有两个与此行为相关的问题:
a) 为什么在设计auto_ptr的行为时,c++标准库团队没有考虑到它在数组上的缺点。
b) 即使在c++11中引入了shared_ptr,为什么它的实现不支持删除数组?
std::array
或std::vector
的智能指针,或者使用你选择的容器。 - stefan