我在不同的系统下采用下面的方式使用shared_ptr以及vector:
std::vector<const std::shared_ptr<Object> > pointer_arrays; std::vector<const std::shared_ptr<Object > >::const_iterator pointer_Iter;
并采用如下初始化:
const std::shared_ptr<WordData> object(new Object); wordDataTab.push_back(object);
这样在mac os下面编译,通过并且获得正确的结果。但是我在centos 下的使用gcc编译这段代码时出现如下的编译错误:
./test.cpp:19: instantiated from here /usr/lib/gcc/x86_64-redhat-linux6E/4.4.7/../../../../include/c++/4.4.7/ext/new_allocator.h:95: error: invalid conversion from ‘const void*’ to ‘void*’ /usr/lib/gcc/x86_64-redhat-linux6E/4.4.7/../../../../include/c++/4.4.7/ext/new_allocator.h:95: error: initializing argument 1 of ‘void operator delete(void*)’ In file included from /usr/lib/gcc/x86_64-redhat-linux6E/4.4.7/../../../../include/c++/4.4.7/bits/char_traits.h:41, from /usr/lib/gcc/x86_64-redhat-linux6E/4.4.7/../../../../include/c++/4.4.7/ios:41, from /usr/lib/gcc/x86_64-redhat-linux6E/4.4.7/../../../../include/c++/4.4.7/ostream:40, from /usr/lib/gcc/x86_64-redhat-linux6E/4.4.7/../../../../include/c++/4.4.7/iostream:40, from ./test.h:12, from ./test.cpp:9: /usr/lib/gcc/x86_64-redhat-linux6E/4.4.7/../../../../include/c++/4.4.7/bits/stl_algobase.h: In static member function ‘static _BI2 std::__copy_move_backward<true, false, std::random_access_iterator_tag>::__copy_move_b(_BI1, _BI1, _BI2) [with _BI1 = const std::shared_ptr<Object>*, _BI2 = const std::shared_ptr<Object>*]’: /usr/lib/gcc/x86_64-redhat-linux6E/4.4.7/../../../../include/c++/4.4.7/bits/stl_algobase.h:595: instantiated from ‘_BI2 std::__copy_move_backward_a(_BI1, _BI1, _BI2) [with bool _IsMove = true, _BI1 = const std::shared_ptr<Object>*, _BI2 = const std::shared_ptr<Object>*]’ /usr/lib/gcc/x86_64-redhat-linux6E/4.4.7/../../../../include/c++/4.4.7/bits/stl_algobase.h:605: instantiated from ‘_BI2 std::__copy_move_backward_a2(_BI1, _BI1, _BI2) [with bool _IsMove = true, _BI1 = const std::shared_ptr<Object>*, _BI2 = const std::shared_ptr<Object>*]’ /usr/lib/gcc/x86_64-redhat-linux6E/4.4.7/../../../../include/c++/4.4.7/bits/stl_algobase.h:676: instantiated from ‘_BI2 std::move_backward(_BI1, _BI1, _BI2) [with _BI1 = const std::shared_ptr<Object>*, _BI2 = const std::shared_ptr<Object>*]’ /usr/lib/gcc/x86_64-redhat-linux6E/4.4.7/../../../../include/c++/4.4.7/bits/vector.tcc:308: 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> >, _Args&& ...) [with _Args = const std::shared_ptr<Object>&, _Tp = const std::shared_ptr<Object>, _Alloc = std::allocator<const std::shared_ptr<Object> >]’ /usr/lib/gcc/x86_64-redhat-linux6E/4.4.7/../../../../include/c++/4.4.7/bits/stl_vector.h:741: instantiated from ‘void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = const std::shared_ptr<Object>, _Alloc = std::allocator<const std::shared_ptr<Object> >]’
可以看到原来是stl_vector与const std::shared_ptr<Object>
结合使用出现问题,于是我这样定义:
std::vector<std::shared_ptr<Object> > pointer_arrays; std::vector<std::shared_ptr<Object > >::const_iterator pointer_Iter;再次编译,通过了。
这是因为:
shared_ptr<const
T> p;
is similar to const
T * p;
(or, equivalently, T
const * p;
), that is, the pointed object is const
whereas const
shared_ptr<T> p;
is similar to T*
const p;
which means that p
is const
.
In summary:
shared_ptr<T> p; ---> T * p; : nothing is const
const shared_ptr<T> p; ---> T * const p; : p is const
shared_ptr<const T> p; ---> const T * p; <=> T const * p; : *p is const
const shared_ptr<const T> p; ---> const T * const p; <=> T const * const p; : p and *p are const.
The same holds for weak_ptr
and unique_ptr
.