should std::auto_ptr<>::operator = reset / deallocate its existing pointee ?

Posted by afriza on Stack Overflow See other posts from Stack Overflow or by afriza
Published on 2010-04-05T03:19:35Z Indexed on 2010/04/05 3:23 UTC
Read the original article Hit count: 219

Filed under:
|
|
|

I read here about std::auto_ptr<>::operator=

Notice however that the left-hand side object is not automatically deallocated when it already points to some object. You can explicitly do this by calling member function reset before assigning it a new value.

However, when I read the source code for header file 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);
    }

What is the correct/standard behavior? How do other STL implementations behave?

© Stack Overflow or respective owner

Related posts about c++

Related posts about stl