views:

153

answers:

1

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? If the website cited above has wrong/outdated information, which website do you recommend as a reference?

+7  A: 

If the auto_ptr being assigned to already holds a pointer, that pointer must be deleted first.

From the 2003 standard (§20.4.5.1):

auto_ptr& operator=(auto_ptr& a) throw();

7 Requires: The expression delete get() is well formed.

8 Effects: reset(a.release()).

9 Returns: *this.

So, assigning to an auto_ptr has the same effect as calling reset on it with the pointer released from the right hand side auto_ptr.

The website you cite is wrong.

James McNellis
Do you have any link(s) for me to click? (link to the standards / other references I can use for daily use)
afriza
You have to purchase the 2003 specification if you want it. You can get the Final Committee Draft (FCD) of the C++0x standard here: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3092.pdf (note that it's not final, official, or normative).
James McNellis
@afriza: Secret hint: Google for "14882 pdf"
Potatoswatter