What happens to auto_ptr as a vector element

Source: Internet
Author: User

Because of the device limitations, I am now testing under Windows, and all of the following code has been run in vs2013

Here is Example 1:

#include <stdio.h> #include <iostream> #include <memory> #include <vector>using namespace std; Class D{public:d (): D (1) {}~d () {printf ("D destruction\n");  } int D;}; void Autoptrinvec () {vector<auto_ptr<d> > auto_vec;for (int i = 0; i < i++) Auto_vec.push_back (auto_ptr& Lt;d> (new D)); for (int i = 0; i <; i++) printf ("Auto_vec[%d]->d:%d\n", I, auto_vec[i]->d);} int _tmain (int argc, _tchar* argv[]) {Autoptrinvec (); GetChar (); return 0;}

The result of the operation is:

auto_vec[0]->d:1auto_vec[1]->d:1auto_vec[2]->d:1auto_vec[3]->d:1auto_vec[4]->d:1auto_vec[5]- >D:1AUTO_VEC[6]->D:1AUTO_VEC[7]->D:1AUTO_VEC[8]->D:1AUTO_VEC[9]->D:1D DestructionD DestructionD Destructiond destructiond destructiond Destructiond destructiond Destructiond destructiond Destruction

Results analysis: From the above, auto_ptr as a vector element in the above example does not appear error, and does not like the online part of the article said that the compilation will be wrong (compilation error I think may be the result of different compiler).


So, does the above example indicate that auto_ptr can be an element of a vector? The answer is no, see the following example:

#include <stdio.h> #include <iostream> #include <memory> #include <vector>using namespace std;  Class D{public:d (): D (1) {}~d () {printf ("D destruction\n"); }int D;}; void Funauto (auto_ptr<d> D) {printf ("do nothing\n");} void Autoptrinvec () {vector<auto_ptr<d> > auto_vec;for (int i = 0; i < 3; i++) Auto_vec.push_back (AUTO_PTR&L T;d> (new D)); for (int i = 0; i < 3; i++) printf ("Auto_vec[%d].get ():%p\n", I, Auto_vec[i].get ()); for (int i = 0; I & Lt 3; i++) Funauto (Auto_vec[i]), for (int i = 0; i < 3; i++) printf ("Auto_vec[%d].get ():%p\n", I, Auto_vec[i].get ());p rintf ("\ n "); Auto_vec.clear (); for (int i = 0; i < 3; i++) Auto_vec.push_back (auto_ptr<d> (new D)), for (int i = 0; i < 3; i++) printf ("Auto_vec[%d].get ():%p\n", I, Auto_vec[i].get ()); for (int i = 0; i < 3; i++) auto_ptr<d> temp = Auto_v ec[i];for (int i = 0; i < 3; i++) printf ("Auto_vec[%d].get ():%p\n", I, Auto_vec[i].get ());} int _tmain (int argc, _tchar* argv[]) {AutoptrinVec (); GetChar (); return 0;} 

The results of the operation are as follows:

Auto_vec[0].get (): 0041c930auto_vec[1].get (): 0041c9b0auto_vec[2].get (): 0041c970do nothingd Destructiondo NothingD Destructiondo nothingd destructionauto_vec[0].get (): 00000000auto_vec[1].get (): 00000000auto_vec[2].get (): 00000000auto_vec[0].get (): 0041c930auto_vec[1].get (): 0041c970auto_vec[2].get (): 0041c9b0d DestructionD Destructiond destructionauto_vec[0].get (): 00000000auto_vec[1].get (): 00000000auto_vec[2].get (): 00000000

Based on the result analysis: The auto_ptr element in the vector is set to NULL for the value of the member variable _myptr in the original auto_ptr in the process of passing the value (calling the copy constructor of the Auto_ptr Class) and assigning (calling the assignment operator of the Auto_ptr class). In this case the original auto_ptr is invalid (about how the whole process of failure can go directly to the Auto_ptr source code, here is not analyzed), can no longer be accessed, the vector of auto_ptr elements in these two processes will be invalidated.

the C + + standard says: "STL elements must have copy construction and assignable ...", it is clear from the above process that auto_ptr as a vector element obviously can not achieve this condition, so in any case it is best not to put Auto_ PTR, as an element of a vector, may silently fail in the process of invoking a function such as swap, sort, and so on.

What happens to auto_ptr as a vector element

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.