Limitations of autoptr

Source: Internet
Author: User

Because STD: auto_ptr is the transfer semantics, and the element of STL container must be the value semantics, that is, the copy semantics.

For example, STL containers store elements in the form of copies.
STD: vector <int> V;
Int A = 1;
V. push_back ();
V [0] is also an int with a value of 1, but it is not a .. It is just a copy. The value of A has not been changed.

STD: auto_ptr <int> P1 (New INT );
STD: auto_ptr <int> P2 = p1;
P2 structure modifies the value of P1, so that P1 gives reference to the dynamically allocated Int. At this time, P1 no longer references the dynamic Int. This is the transfer semantics.

In addition, STD: auto_ptr only provides such a copy constructor to meet the transfer semantic requirement.
Auto_ptr (auto_ptr &); instead of the normal T (const T &); this is a manifestation of non-value semantics.

STD: vector requires the copy constructor T (const T &) for the element type because it requires the value semantics.
It cannot be used for dynamically created arrays.
It may be because it is appropriate to use STD: vector most of the time, so there is no auto_array corresponding to auto_ptr. However, if you have special requirements, you can easily write an auto_array like STD: auto_ptr.

To use auto_ptr, you need to know:
1. Smart pointers cannot share ownership of Objects
2. Smart pointers cannot point to arrays. Because the implementation calls Delete instead of Delete []
3. The smart pointer cannot be an element of the container class.

 

Because STD: auto_ptr is the transfer semantics, and the element of STL container must be the value semantics, that is, the copy semantics.

For example, STL containers store elements in the form of copies.
STD: vector <int> V;
Int A = 1;
V. push_back ();
V [0] is also an int with a value of 1, but it is not a .. It is just a copy. The value of A has not been changed.

Let's take a look at STD: auto_ptr.
STD: auto_ptr <int> P1 (New INT );
STD: auto_ptr <int> P2 = p1;
P2 structure modifies the value of P1, so that P1 gives reference to the dynamically allocated Int. At this time, P1 no longer references the dynamic Int. This is the transfer semantics.

In terms of semantics, these two are not compatible.
In addition, STD: auto_ptr only provides such a copy constructor to meet the transfer semantic requirement.
Auto_ptr (auto_ptr &); instead of the normal T (const T &); this is a manifestation of non-value semantics.

STD: vector requires the copy constructor T (const T &) for the element type because it requires the value semantics.

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.