STL source code analysis --- _ auto_ptr.h Reading Notes

Source: Internet
Author: User

Auto_ptr is a commonly used smart pointer. Its implementation is very simple and the source code is very short. However, there is a proxy class auto_ptr_ref in the middle, which is very clever and worth learning.

/** Copyright (c) 1997-1999 * Silicon Graphics Computer Systems, Inc. ** copyright (c) 1999 * Boris fow.ev ** this material is provided "as is", with absolutely no warranty expressed * or implied. any use is at your own risk. ** permission to use or copy this software for any purpose is hereby granted * Without tables, provided the above notices are retained on all copies. * permission to modif Y The Code and to distribute modified code is granted, * provided the above notices are retained, and a notice that the code was * modified is already ded with the above copyright notice. **/# ifndef _ stlp_auto_ptr_h # DEFINE _ classes // implementation primitive // auto_ptr base class, declares class _ ptr_base {public: void * _ m_p; void _ set (const volatile void * P) {_ m_p = _ const _ Cast (void *, P);} void _ set (void * P) {_ m_p = P ;}; // use auto_ptr_ref, examples of template <class _ TP> class auto_ptr_ref {public: _ ptr_base & _ M_r; _ TP * const _ m_p; // _ M_r is referenced, _ m_p is a constant pointer. In the constructor initialization list, initialize auto_ptr_ref (_ ptr_base & _ r, _ TP * _ p): _ M_r (_ r ), _ m_p (_ p) {}// release, set the base class _ M_r pointer to null _ TP * release () const {_ M_r. _ set (_ static_cast (void *, 0); Return _ m_p;} PRIVATE: // explicitely defined as private Avoid Warnings: // display definition to avoid warning. // Reload "=" as a private function. You cannot call typedef auto_ptr_ref <_ TP> _ self; _ Self & operator = (_ Self const &);}; template <class _ TP> class auto_ptr: Public _ ptr_base {public: typedef _ TP element_type; typedef auto_ptr <_ TP> _ self; _ TP * release () _ stlp_nothrow {_ TP * _ PX = This-> get (); this-> _ m_p = 0; return _ PX ;} void reset (_ TP * _ PX = 0) _ stlp_nothrow {_ TP * _ Pt = This-> get (); If (_ PX! = _ Pt) delete _ pt; this->__ set (_ px);} _ TP * Get () const _ stlp_nothrow # If! Defined (_ gnuc _) | (_ gnuc _> 2) // like C, use static_cast <> {return _ static_cast (_ TP *, _ m_p) ;}# else // use reinterpret {return _ reinterpret_cast (_ TP *, _ m_p) like C ++;} # endif # If! Defined (_ stlp_no_arrow_operator) // returns the pointer _ TP * operator-> () const _ stlp_nothrow {// asserted that the returned pointer is not a null pointer _ stlp_verbose_assert (get ()! = 0, _ stlmsg_auto_ptr_null) return get () ;}# endif // return reference _ TP & operator * () const _ stlp_nothrow {// asserted that the pointer is not a null pointer, in this way, the _ stlp_verbose_assert (get ()! = 0, _ stlmsg_auto_ptr_null) return * Get ();} // display constructor. Explicit auto_ptr (_ TP * _ PX = 0) cannot be called implicitly) _ stlp_nothrow {This->__ set (_ px);}/* copy constructor. Note that the copy constructor transmits pointer ownership through auto_ptr. Because you want to modify the ownership of the input parameter, the input parameter is not const (different from other replication constructors) auto_ptr <int> P1 (New int (10 )); auto_ptr <int> P2 (P1); after P2 points to new int (10), P1 becomes a null pointer and they cannot share ownership. Therefore, auto_ptr cannot be used as a container element, because the container element must support copying and copying */# If defined (_ stlp_member_templates) # If! Defined (_ stlp_no_template_conversions) template <class _ TP1> auto_ptr (auto_ptr <_ TP1> & _ r) _ stlp_nothrow {_ TP * _ conversioncheck = _ r. release (); this->__ set (_ conversioncheck );} # endif template <class _ TP1> auto_ptr <_ TP> & operator = (auto_ptr <_ TP1> & _ r) _ stlp_nothrow {_ TP * _ conversioncheck = _ r. release (); reset (_ conversioncheck); return * This;} # endif // auto_ptr of the same type as the copy constructor parameter, without the auto_ptr template (_ Self & _ R) _ stlp_nothrow {This->__ set (_ r. release ();} // The Value assignment operator and the type of the copy constructor, passing ownership instead of passing _ Self & operator = (_ Self & _ r) _ stlp_nothrow {reset (_ r. release (); return * This;} // The Destructor is simple. It only calls the Destructor pointing to the object and releases the space pointed to by the pointer. // Delete instead of Delete [], therefore, auto_ptr cannot point to an array ~ Auto_ptr () _ stlp_nothrow {/* Boris: reset (0) might be better */delete this-> get ();} // use auto_ptr_ref, an example of auto_ptr (auto_ptr_ref <_ TP> _ r) _ stlp_nothrow {This->__ set (_ r. release ();} _ Self & operator = (auto_ptr_ref <_ TP> _ r) _ stlp_nothrow {reset (_ r. release (); return * This;} # If defined (_ stlp_member_templates )&&! Defined (_ stlp_no_template_conversions) template <class _ TP1> operator auto_ptr_ref <_ TP1> () _ stlp_nothrow {return auto_ptr_ref <_ TP1> (* This, this-> get ();} template <class _ TP1> operator auto_ptr <_ TP1> () _ stlp_nothrow {return auto_ptr <_ TP1> (release ());} # else operator auto_ptr_ref <_ TP> () _ stlp_nothrow {return auto_ptr_ref <_ TP> (* This, this-> get ();} # endif }; _ stlp_end_namespace # endif/* _ stlp_auto_ptr_h * // local variables: // mode: C ++ // end:


Auto_ptr is a smart pointer. When assigning values (copying constructor or value assignment operator), it is to pass the weights used by pointers, rather than passing values. Therefore, the ownership of the original pointer must be removed from the copy constructor or the copy operator function, so the input parameter cannot be declared as the const type. However, in some cases, it must be declared as the const type, for example

Auto_ptr <int> P (auto_ptr <int> (New int (10 ));

When using a temporary object, you must use the const

Auto_ptr (auto_prt <int> const &), while auto_ptr should modify the ownership of the original pointer and declare it as auto_ptr (auto_prt <int> &). The above Code cannot be compiled.

For example

Auto_ptr <int> P1 (null );

P1 = (auto_ptr <int> (New int (10 ));

As with the above issue, the temporary object is the right value, and the non-const cannot point to the right value.

In this case, auto_ptr_ref is introduced. auto_ptr can be implicitly converted to auto_ptr_ref, so that the above program will not encounter errors.


STL source code analysis --- _ auto_ptr.h Reading Notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.