C ++ standard STL-sgi stl source code learning notes (01) auto_ptr

Source: Internet
Author: User

Preface:

Learning C ++ is now the ninth day, so I just have a simple understanding of C ++ theoretically. I have no practical experience in practical application. so next we will analyze the sgi stl source code.Article, More

This is just a direct analysis of the source code, and there is no experience on the actual application scenario. So we only talk about the source code, and nothing else.

For Hou Jie's article <STL source code analysis>, I downloaded a PDF file and just browsed the directory roughly. the problem I want to explain is that I have no reference to books prepared for analysis by others, and may be in the process of analysis.

I have referenced some good blog explanations about some detailed issues in the source code reading process. if the analysis is incorrect, it is also the fault of my own analysis. It is definitely not a reference to the analysis data wrong by xx people, and it has nothing to do with others.

 

The implementation of auto_ptr in the memory file contains only 134 lines of comments.Code, So there are very few.

1. _ stl_begin_namespace macro:

You can see the _ stl_begin_namespace macro at the beginning of the code, and it is easy to find the macro definition:

Macro is defined in the stl_config.h file.

 
# DEFINE _ stl_begin_namespace namespace STD {# DEFINE _ stl_end_namespace}

 

Ignore other macros used when defining and changing macros. The macro definition is shown above. it defines the namespace STD, which is very simple. but there is another problem here. Go out to auto_ptr and read other source code.

When the _ stl_inin_namespace macro is used, some source files do not include "stl_config.h". (Note: not <stl_config> ).

This is a very strange problem. I tried to find it in many source files, but none of them were found. There is no explanation ~

2. _ stl_nothrow macro:

Like the macro above, __stl_nothrow macro is defined in the stl_config.h file:

 
# DEFINE _ stl_nothrow throw ()

 

This is an exception description. The exception description is after the member functions. These member functions do not throw an exception.

3. auto_ptr member Variable _ m_ptr:

Auto_ptr has only one private member Variable _ m_ptr:

 
Template <class _ TP> class auto_ptr {PRIVATE: _ TP * _ m_ptr;

 

4. constructor:

 
Explicit auto_ptr (_ TP * _ p = 0) _ stl_nothrow: _ m_ptr (_ p ){}

 

A. The default constructor parameter is null. You can skip this parameter when using the constructor.

B. Use explicit to disable automatic type conversion of parameters.

5. copy constructor:

 
Auto_ptr (auto_ptr & _ A) _ stl_nothrow: _ m_ptr (_ A. Release ()){}

 

The const restriction parameter is not used here. This auto_ptr function is related. When using the copy constructor, not only copying an object that is the same as the original object, but also canceling its control.

Realcompute functions:

 
_ TP * release () _ stl_nothrow {_ TP * _ TMP = _ m_ptr; _ m_ptr = 0; return _ TMP ;}

 

The function implemented by reallocate is to remove the control of the original smart pointer.

6. Get and reset functions:

A. Get function:

_ TP * Get () const _ stl_nothrow {return _ m_ptr ;}

 

Returns the pointer from the smart pointer to the object.

B. reset function:

 
Void reset (_ TP * _ p = 0) _ stl_nothrow {If (_ p! = _ M_ptr) {Delete _ m_ptr; _ m_ptr = _ p ;}}

 

The reset function provides default parameters to cancel the control of auto_ptr on the original object and selectively provide a new control of the object.

7. auto_ptr_ref:

The implementation of auto_ptr_ref is very simple. It is related to several member functions in auto_ptr to complete auxiliary functions. Why do we need to provide an auxiliary structure such as auto_ptr_ref?

As mentioned above, the auto_ptr function mainly controls the security of object control. Its copy constructor does not impose const restrictions on parameters because it needs to be modified. this also involves the concept of the Standard C ++ left value and right value.

 

Well, we will only discuss the Standard C ++, and I will explain why this implementation method is for the sake of Standard C ++.

 

8. lvalue & rvalue

There are many blogs and posts on the Internet about the specific analysis and differentiation of the left and right values. Let's take a look at the more authoritative explanation:

The names rvalue and lvalue come originally from the assignment expression expr1 = expr2, in which the left operand expr1 must be a (modifiable) lvalue ("Left value ").
However, an lvalue is perhaps better considered as representing an object locator value. Thus, it is an expression that designates an object by name or address
(Pointer or reference). lvalues need not be modifiable. For example, the name of a constant object is a nonmodifiable lvalue. All expressions that are not lvalues
Are rvalues. In particle, temporary objects created explicitly (T () or as the result of a function call are rvalues.

The C ++ standard libraryBy niclai M. josutis

 

 
An ordinary copy constructor can copy an rvalue, but to do so it must declare its parameter as a reference to a const object.

 

That is to say, when passing a parameter to a replication constructor, if it is the right value, the const reference must be the const reference parameter.

 

The previous copy constructor of auto_ptr is not like this. therefore, auto_ptr_ref is added for implementation. the implementation mechanism is to use auto_ptr_ref to implement a conversion process from right to center left.

Therefore, auto_ptr needs to provide class type conversion, and then it also needs to provide a duplicate constructor. The following are the two functions:

 
Auto_ptr (auto_ptr_ref <_ TP> _ ref) _ stl_nothrow: _ m_ptr (_ ref. _ m_ptr ){}

 

 

 
Template <class _ TP1> operator auto_ptr_ref <_ TP1> () _ stl_nothrow {return auto_ptr_ref <_ TP1> (this-> release ());}

 

 

This is why when we see the source code, we find that auto_ptr is implemented through two parts. The curve implementation method is indeed very delicate ~

 

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.