c – 将指针容器转换为智能指针?
发布时间:2020-12-16 06:48:15 所属栏目:百科 来源:网络整理
导读:是否有一种简洁,通用的方法来转换常规/哑指针的std容器(如向量): vector T* 例如,boost :: shared_ptr?: vector boost::shared_ptrT 我以为我可以使用vector的范围构造函数将其拉出来: vector T* vec_a;...vector boost::shared_ptrT vec_b( vec_a.begin
是否有一种简洁,通用的方法来转换常规/哑指针的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() ); 但那拒绝编译(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.0VCincludememory(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.0VCincludememory(822) : see reference to function template instantiation '_FwdIt std::_Uninit_copy<int**,_FwdIt,_Alloc>(_InIt,_InIt,_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.0VCincludevector(1141) : see reference to function template instantiation '_FwdIt stdext::unchecked_uninitialized_copy<_Iter,boost::shared_ptr<T>*,std::allocator<_Ty>>(_InIt,_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 *,1> _Alloc=std::allocator<boost::shared_ptr<int>> 1> ] 1> c:Program Files (x86)Microsoft Visual Studio 9.0VCincludevector(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,std::allocator<int *>> 1> ] 1> c:Program Files (x86)Microsoft Visual Studio 9.0VCincludevector(889) : see reference to function template instantiation 'void std::vector<_Ty>::_Insert<_Iter>(std::_Vector_const_iterator<_Ty,_Alloc>,std::forward_iterator_tag)' being compiled 1> with 1> [ 1> _Ty=boost::shared_ptr<int>,1> _Alloc=std::allocator<boost::shared_ptr<int>> 1> ] 1> c:Program Files (x86)Microsoft Visual Studio 9.0VCincludevector(537) : see reference to function template instantiation 'void std::vector<_Ty>::insert<_Iter>(std::_Vector_const_iterator<_Ty,_Iter)' being compiled 1> with 1> [ 1> _Ty=boost::shared_ptr<int>,1> _Alloc=std::allocator<boost::shared_ptr<int>> 1> ] 1> c:Program Files (x86)Microsoft Visual Studio 9.0VCincludevector(514) : see reference to function template instantiation 'void std::vector<_Ty>::_Construct<_Iter>(_Iter,std::input_iterator_tag)' being compiled 1> with 1> [ 1> _Ty=boost::shared_ptr<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,1> _Alloc=std::allocator<int *>,std::allocator<int *>> 1> ] 解决方法
将:: std :: back_inserter与:: std :: transform结合使用,以及一个将执行转换的小函数.如果你也使用保留,这应该是相当有效的.一旦扩展了所有模板,您将基本上获得以下代码:
template <class T> static inline ::std::tr1::shared_ptr<T> to_shared_ptr(T *val) { return ::std::tr1::shared_ptr<T>(val); } void test() { ::std::vector< int* > vec_a; ::std::vector< ::std::tr1::shared_ptr<int> > vec_b; vec_b.reserve(vec_a.size()); ::std::transform(vec_a.begin(),vec_a.end(),::std::back_inserter(vec_b),to_shared_ptr<int>); vec_a.clear(); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |