2010-04-15 38 views
7

考慮下面的代碼:與矢量編制問題<auto_ptr<>>

#include <iostream> 
#include <memory> 
#include <vector> 

using namespace std; 

struct A 
{ 
    int a; 
    A(int a_):a(a_) {} 
}; 

int main() 
{ 
    vector<auto_ptr<A> > as; 
    for (int i = 0; i < 10; i++) 
    { 
     auto_ptr<A> a(new A(i)); 
     as.push_back(a); 
    } 
    for (vector<auto_ptr<A> >::iterator it = as.begin(); it != as.end(); ++it) 
     cout << (*it)->a << endl; 
} 

當試圖編譯它,我得到以下晦澀編譯器錯誤的G ++:

g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/proba.d" -MT"src/proba.d" -o"src/proba.o" "../src/proba.cpp" 
/usr/include/c++/4.1.2/ext/new_allocator.h: In member function ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = std::auto_ptr<A>]’: 
/usr/include/c++/4.1.2/bits/stl_vector.h:606: instantiated from ‘void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = std::auto_ptr<A>, _Alloc = std::allocator<std::auto_ptr<A> >]’ 
../src/proba.cpp:19: instantiated from here 
/usr/include/c++/4.1.2/ext/new_allocator.h:104: error: passing ‘const std::auto_ptr<A>’ as ‘this’ argument of ‘std::auto_ptr<_Tp>::operator std::auto_ptr_ref<_Tp1>() [with _Tp1 = A, _Tp = A]’ discards qualifiers 
/usr/include/c++/4.1.2/bits/vector.tcc: In member function ‘void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = std::auto_ptr<A>, _Alloc = std::allocator<std::auto_ptr<A> >]’: 
/usr/include/c++/4.1.2/bits/stl_vector.h:610: instantiated from ‘void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = std::auto_ptr<A>, _Alloc = std::allocator<std::auto_ptr<A> >]’ 
../src/proba.cpp:19: instantiated from here 
/usr/include/c++/4.1.2/bits/vector.tcc:256: error: passing ‘const std::auto_ptr<A>’ as ‘this’ argument of ‘std::auto_ptr<_Tp>::operator std::auto_ptr_ref<_Tp1>() [with _Tp1 = A, _Tp = A]’ discards qualifiers 
/usr/include/c++/4.1.2/bits/stl_construct.h: In function ‘void std::_Construct(_T1*, const _T2&) [with _T1 = std::auto_ptr<A>, _T2 = std::auto_ptr<A>]’: 
/usr/include/c++/4.1.2/bits/stl_uninitialized.h:86: instantiated from ‘_ForwardIterator std::__uninitialized_copy_aux(_InputIterator, _InputIterator, _ForwardIterator, __false_type) [with _InputIterator = __gnu_cxx::__normal_iterator<std::auto_ptr<A>*, std::vector<std::auto_ptr<A>, std::allocator<std::auto_ptr<A> > > >, _ForwardIterator = __gnu_cxx::__normal_iterator<std::auto_ptr<A>*, std::vector<std::auto_ptr<A>, std::allocator<std::auto_ptr<A> > > >]’ 
/usr/include/c++/4.1.2/bits/stl_uninitialized.h:113: instantiated from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<std::auto_ptr<A>*, std::vector<std::auto_ptr<A>, std::allocator<std::auto_ptr<A> > > >, _ForwardIterator = __gnu_cxx::__normal_iterator<std::auto_ptr<A>*, std::vector<std::auto_ptr<A>, std::allocator<std::auto_ptr<A> > > >]’ 
/usr/include/c++/4.1.2/bits/stl_uninitialized.h:254: instantiated from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>) [with _InputIterator = __gnu_cxx::__normal_iterator<std::auto_ptr<A>*, std::vector<std::auto_ptr<A>, std::allocator<std::auto_ptr<A> > > >, _ForwardIterator = __gnu_cxx::__normal_iterator<std::auto_ptr<A>*, std::vector<std::auto_ptr<A>, std::allocator<std::auto_ptr<A> > > >, _Tp = std::auto_ptr<A>]’ 
/usr/include/c++/4.1.2/bits/vector.tcc:279: instantiated from ‘void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = std::auto_ptr<A>, _Alloc = std::allocator<std::auto_ptr<A> >]’ 
/usr/include/c++/4.1.2/bits/stl_vector.h:610: instantiated from ‘void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = std::auto_ptr<A>, _Alloc = std::allocator<std::auto_ptr<A> >]’ 
../src/proba.cpp:19: instantiated from here 
/usr/include/c++/4.1.2/bits/stl_construct.h:81: error: passing ‘const std::auto_ptr<A>’ as ‘this’ argument of ‘std::auto_ptr<_Tp>::operator std::auto_ptr_ref<_Tp1>() [with _Tp1 = A, _Tp = A]’ discards qualifiers 
make: *** [src/proba.o] Error 1 

在我看來,有這裏是consts的一些問題。這是否意味着auto_ptr不能用於vector

+11

切勿在容器中使用'auto_ptr'。這是一件不幸的事情。 :( – GManNickG 2010-04-15 08:31:40

+2

看到這個問題,爲什麼'auto_ptr'不能用於STL容器:http://stackoverflow.com/questions/111478/why-is-it-wrong-to-use-stdauto-ptr-with-stl -containers – Naveen 2010-04-15 08:33:14

+0

爲什麼你不使用搜索?這個想法存在其他問題 – 2010-04-15 08:51:41

回答

18

正確,std::auto_ptr不能用於std::vector

編譯器抱怨的是auto_ptr的賦值運算符會改變被賦值的對象,所以它不能是const

你要爲使用boost::ptr_vectorboost::shared_ptr小號

+0

謝謝。然後我會去boost :: shared_ptr,我以前用過。 – petersohn 2010-04-15 08:53:31

+1

其實,如果你有權限訪問它,'unique_ptr'如果你不需要共享所有權,它會更好。它需要C++ 0x移動語義。 – 2010-04-15 14:21:03

5

auto_ptr向量與一個非const參數拷貝構造函數,所以因爲後者具有常量參數編譯器不能從vector::push_back()調用它。

的原因是,當你從另一個初始化一個auto_ptr實例的新實例從另一個實例斷開對象,並將其連接到自身,從而避免懸空指針的情況時,一個實例delete S中的對象和另一仍持有指針到它。

相關問題