Why can not auto_ptr as elements of the vector

  Yesterday to see effectve c ++, we found the auto_ptr this thing. Since I worked at the company are used in the old version of c ++, code smart pointers or something completely original pointer is directly operated appeared. Although I rarely go wrong, but always still not safe. Closer to home, said back auto_ptr, this thing first I have seen, but it was not how the project got the idea in a hurry, this time just to see, to learn more about the case.

  In effiectev c ++, I learned that this is where the old version of the c ++ smart pointers (before I only know c ++ 11 in the share_ptr and unique_ptr), the same object from beginning to end will be only one auto_ptr pointing to it, that is, He said that when auto_ptr pointing to an object is copied (copy constructor or assignment), the original auto_ptr will be set to NULL. When the auto_ptr pointing object goes out of scope, the destructor is automatically invoked to achieve the purpose of preventing leakage of resources.

  Naturally, I want my recent project by the new operating smart pointer instead of bare hands, because the STL standard containers used in the project, so I plan to use auto_ptr vector under test. Test code is as follows:

#include<iostream>
#include<vector>
#include<memory>class Test{
public:
    Test() {std::cout << "init" <<std::endl;
    }~Test() {
        std::cout << "del" <<std::endl;
    }
};int main()
{
    std::auto_ptr<Test> ap(new Test());
    std::vector<std::auto_ptr<Test> >
    Veckpus_bak (up);


         
    
     


     vec;
    
    std::cout << ap.get() << std::endl;
    return 0;
}

  Compile time, suggesting auto_ptr outdated warnings not to mention, being given as follows:

/usr/include/c++/7/ext/new_allocator.h:136:4: error: no matching function for call to ‘std::auto_ptr<Test>::auto_ptr(const std::auto_ptr<Test>&)’ 
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }

  Was probably a warning plus an error message is too long, I do not see this const. . . Then like a long time to figure out why not, I was understanding that the vector auto_ptr will be copied into the original auto_ptr becomes zero. Then he would suspect the vector push_back operation wrong, stl open source analytic, find the function when the vector construct added elements:

template <class T1, class T2>
inline void construct(T1* p, const T2& value) 
{
    new (p) T1(value);       
}

  Ok? I do not remember bad, why can not it? Then I went to Internet search, to know the truth of my tears fell down. It is simple:

Will need to be modified because the original auto_ptr auto_ptr copying, the original auto_ptr is set to NULL, it is not supported in a manner const copy constructor!

  In other words, auto_ptr can not be used as an element of the STL standard containers.

  For the emergence of this question, I think one is that you are not careful, did not see the meaning of being given careful; on the other hand is still a little short of their own thinking, knowledge obviously knew, but did not think of.

 

Guess you like

Origin www.cnblogs.com/RookieSuperman/p/11257446.html