【发布时间】:2011-06-14 19:44:13
【问题描述】:
是否有一种简洁、通用的方法来转换常规/哑指针的std 容器(例如vector):
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.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: boost::shared_ptr 不允许从原始指针隐式构造(因为这可能会隐藏语义的变化),因此编译器会认为类型不相关。
-
@templatetypedef:添加了我的测试存根和错误
-
如果可以,请考虑使用
::std::tr1::shared_ptr。避免对第三方库的依赖是很好的,即使是像 boost 这样壮观的库。
标签: c++ pointers boost stl smart-pointers