(예 : vector
등)/일반 벙어리 포인터의 std
컨테이너를 변환하는 간결하고 일반적인 방법이 있나요 :포인터의 컨테이너를 스마트 포인터로 변환 하시겠습니까?
vector< T* >
에, 예를 들어, boost::shared_ptr
:
vector< boost::shared_ptr<T> >
가 내가 생각 vector
의 범위 생성자를 사용하여 해낼 수 :
vector< T* > vec_a;
...
vector< boost::shared_ptr<T> > vec_b(vec_a.begin(), vec_a.end());
을하지만 COM에 거부 더미 (Visual Studio 2008).
편집 : 테스트 코드 :
void test()
{
vector< int* > vec_a;
vector< boost::shared_ptr<int> > vec_b(vec_a.begin(), vec_a.end());
}
컴파일 오류 :
1>c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\memory(131) : error C2664: 'std::allocator<_Ty>::construct' : cannot convert parameter 2 from 'int *' to 'const boost::shared_ptr<T> &'
1> with
1> [
1> _Ty=boost::shared_ptr<int>
1> ]
1> and
1> [
1> T=int
1> ]
1> Reason: cannot convert from 'int *' to 'const boost::shared_ptr<T>'
1> with
1> [
1> T=int
1> ]
1> Constructor for class 'boost::shared_ptr<T>' is declared 'explicit'
1> with
1> [
1> T=int
1> ]
1> c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\memory(822) : see reference to function template instantiation '_FwdIt std::_Uninit_copy<int**,_FwdIt,_Alloc>(_InIt,_InIt,_FwdIt,_Alloc &,std::_Nonscalar_ptr_iterator_tag,std::_Range_checked_iterator_tag)' being compiled
1> with
1> [
1> _FwdIt=boost::shared_ptr<int> *,
1> _Alloc=std::allocator<boost::shared_ptr<int>>,
1> _InIt=int **
1> ]
1> c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\vector(1141) : see reference to function template instantiation '_FwdIt stdext::unchecked_uninitialized_copy<_Iter,boost::shared_ptr<T>*,std::allocator<_Ty>>(_InIt,_InIt,_FwdIt,_Alloc &)' being compiled
1> with
1> [
1> _FwdIt=boost::shared_ptr<int> *,
1> _Iter=std::_Vector_iterator<int *,std::allocator<int *>>,
1> T=int,
1> _Ty=boost::shared_ptr<int>,
1> _InIt=std::_Vector_iterator<int *,std::allocator<int *>>,
1> _Alloc=std::allocator<boost::shared_ptr<int>>
1> ]
1> c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\vector(956) : see reference to function template instantiation 'boost::shared_ptr<T> *std::vector<_Ty>::_Ucopy<_Iter>(_Iter,_Iter,boost::shared_ptr<T> *)' being compiled
1> with
1> [
1> T=int,
1> _Ty=boost::shared_ptr<int>,
1> _Iter=std::_Vector_iterator<int *,std::allocator<int *>>
1> ]
1> c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\vector(889) : see reference to function template instantiation 'void std::vector<_Ty>::_Insert<_Iter>(std::_Vector_const_iterator<_Ty,_Alloc>,_Iter,_Iter,std::forward_iterator_tag)' being compiled
1> with
1> [
1> _Ty=boost::shared_ptr<int>,
1> _Iter=std::_Vector_iterator<int *,std::allocator<int *>>,
1> _Alloc=std::allocator<boost::shared_ptr<int>>
1> ]
1> c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\vector(537) : see reference to function template instantiation 'void std::vector<_Ty>::insert<_Iter>(std::_Vector_const_iterator<_Ty,_Alloc>,_Iter,_Iter)' being compiled
1> with
1> [
1> _Ty=boost::shared_ptr<int>,
1> _Iter=std::_Vector_iterator<int *,std::allocator<int *>>,
1> _Alloc=std::allocator<boost::shared_ptr<int>>
1> ]
1> c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\vector(514) : see reference to function template instantiation 'void std::vector<_Ty>::_Construct<_Iter>(_Iter,_Iter,std::input_iterator_tag)' being compiled
1> with
1> [
1> _Ty=boost::shared_ptr<int>,
1> _Iter=std::_Vector_iterator<int *,std::allocator<int *>>
1> ]
1> .\test.cpp(8364) : see reference to function template instantiation 'std::vector<_Ty>::vector<std::_Vector_iterator<int,_Alloc>>(_Iter,_Iter)' being compiled
1> with
1> [
1> _Ty=boost::shared_ptr<int>,
1> _Alloc=std::allocator<int *>,
1> _Iter=std::_Vector_iterator<int *,std::allocator<int *>>
1> ]
VS2008에서 어떤 오류가 발생 했습니까? – templatetypedef
@templatetypedef : boost :: shared_ptr은 원시 포인터에서 암시 적으로 생성을 허용하지 않으므로 (컴파일러는 의미 변경을 숨길 수 있기 때문에) 컴파일러는 관련없는 유형을 고려합니다. – peterchen
@templatetypedef : 테스트 스텁 및 오류 추가 – genpfault