Can someone explain why I get a compilation error here - error C2558: class 'std :: auto_ptr <_Ty>': no copy constructor is available or the copy constructor is declared "explicit"
#include <memory> #include <vector> #include <string> template<typename T> struct test { typedef std::auto_ptr<T> dataptr; typedef std::auto_ptr< test<T> > testptr; test( const T& data ): data_( new T(data) ) { }; void add_other( const T& other ) { others_.push_back( testptr( new test(other) ) ); } private: dataptr data_; std::vector< testptr > others_; }; int main(int argc, char* argv[]) { test<std::string> g("d"); //this is the line that causes the error. g.add_other("d"); return 0; }
In principle, std::auto_ptrcannot be used in this way.
std::auto_ptr
others_.push_back( testptr( new test(other) ) );
, , const&, std::auto_ptr. , std::auto_ptr ! , , Herb Sutter, , " , " 3/4 .
const&
auto_ptr std::vector
auto_ptr
std::vector
auto_ptr stl.
. : fooobar.com/questions/22980/...
auto_ptr, :
std::vector< testptr > others_;
. , shared_ptr.
shared_ptr
, , std:: unique_ptr std:: shared_ptr ++ 0x, auto_ptr, , (gcc 4.5 +)
http://www2.research.att.com/~bs/C++0xFAQ.html#std-unique_ptr http://www2.research.att.com/~bs/C++0xFAQ.html#std-shared_ptr