应该std :: auto_ptr <> :: operator = reset/deallocate其现有的指针?

Afr*_*ief 3 c++ stl smart-pointers auto-ptr visual-c++

在这里读到std :: auto_ptr <> :: operator =

但请注意,当左侧对象已指向某个对象时,它不会自动释放.您可以通过在为其分配新值之前调用成员函数reset来明确执行此操作.

但是,当我读取头文件的源代码时 C:\Program Files\Microsoft Visual Studio 8\VC\ce\include\memory

template<class _Other>
    auto_ptr<_Ty>& operator=(auto_ptr<_Other>& _Right) _THROW0()
    { // assign compatible _Right (assume pointer)
    reset(_Right.release());
    return (*this);
    }

auto_ptr<_Ty>& operator=(auto_ptr<_Ty>& _Right) _THROW0()
    { // assign compatible _Right (assume pointer)
    reset(_Right.release());
    return (*this);
    }

auto_ptr<_Ty>& operator=(auto_ptr_ref<_Ty> _Right) _THROW0()
    { // assign compatible _Right._Ref (assume pointer)
    _Ty **_Pptr = (_Ty **)_Right._Ref;
    _Ty *_Ptr = *_Pptr;
    *_Pptr = 0; // release old
    reset(_Ptr); // set new
    return (*this);
    }
Run Code Online (Sandbox Code Playgroud)

什么是正确/标准行为?其他STL实现如何表现?如果上面引用的网站有错误/过时的信息,您建议哪个网站作为参考?

Jam*_*lis 6

如果auto_ptr被指定为已经拥有指针,则必须首先删除该指针.

从2003年标准(§20.4.5.1):

auto_ptr& operator=(auto_ptr& a) throw();
Run Code Online (Sandbox Code Playgroud)

7要求:表达delete get()形式良好.

8效果:reset(a.release()).

9返回:*this.

因此,分配给a 与使用从右侧释放的指针auto_ptr调用reset它具有相同的效果auto_ptr.

你引用的网站是错误的.