STL rotating const_iterators of unique_ptrs



我在const_iterator上使用std::rotate在unique_ptr middle上有问题。

I have try:

std::vector<std::unique_ptr<Object> >::const_iterator middle;
// middle is pointing at somewhere within vec defined elsewhere.
std::rotate(vec.begin(), middle, vec.end());

这导致调用rotate(…)没有匹配的函数。迭代器,……Const_iterator &,…iterator)

,因此我试着这样做:Std::vector>::const_iterator first = vec.begin();Std::vector>::const_iterator last = vector .end();

则给出如下所示的一堆错误消息。这是一个错误还是我犯了什么错误?

In file included from /usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/algorithm:63:0,
             from file.cpp:8:
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/unique_ptr.h: In function ‘void std::__rotate(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’:
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1677:7:   instantiated from ‘void std::rotate(_FIter, _FIter, _FIter) [with _FIter = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
file.cpp:76:35:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/unique_ptr.h:207:7: error: deleted function ‘std::unique_ptr<_Tp, _Tp_Deleter>::unique_ptr(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = Object, _Tp_Deleter = std::default_delete<Object>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<Object>]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1601:22: error: used here
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1677:7:   instantiated from ‘void std::rotate(_FIter, _FIter, _FIter) [with _FIter = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
file.cpp:76:35:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1603:5: error: passing ‘const std::unique_ptr<Object>’ as ‘this’ argument of ‘std::unique_ptr<_Tp, _Tp_Deleter>& std::unique_ptr<_Tp, _Tp_Deleter>::operator=(std::unique_ptr<_Tp, _Tp_Deleter>&&) [with _Tp = Object, _Tp_Deleter = std::default_delete<Object>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<Object>]’ discards qualifiers
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1677:7:   instantiated from ‘void std::rotate(_FIter, _FIter, _FIter) [with _FIter = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
file.cpp:76:35:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/unique_ptr.h:207:7: error: deleted function ‘std::unique_ptr<_Tp, _Tp_Deleter>::unique_ptr(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = Object, _Tp_Deleter = std::default_delete<Object>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<Object>]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1624:22: error: used here
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1677:7:   instantiated from ‘void std::rotate(_FIter, _FIter, _FIter) [with _FIter = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
file.cpp:76:35:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1626:5: error: passing ‘const std::unique_ptr<Object>’ as ‘this’ argument of ‘std::unique_ptr<_Tp, _Tp_Deleter>& std::unique_ptr<_Tp, _Tp_Deleter>::operator=(std::unique_ptr<_Tp, _Tp_Deleter>&&) [with _Tp = Object, _Tp_Deleter = std::default_delete<Object>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<Object>]’ discards qualifiers
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_tree.h:62:0,
             from /usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/map:60,
             from dir/file3.hpp:4,
             from dir/file2.hpp:3,
             from file.hpp:3,
             from file.cpp:1:
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/unique_ptr.h: In static member function ‘static void std::__iter_swap<_BoolType>::iter_swap(_ForwardIterator1, _ForwardIterator2) [with _ForwardIterator1 = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >, _ForwardIterator2 = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >, bool _BoolType = false]’:
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:138:7:   instantiated from ‘void std::iter_swap(_ForwardIterator1, _ForwardIterator2) [with _ForwardIterator1 = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >, _ForwardIterator2 = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1609:5:   instantiated from ‘void std::__rotate(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1677:7:   instantiated from ‘void std::rotate(_FIter, _FIter, _FIter) [with _FIter = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
file.cpp:76:35:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/unique_ptr.h:207:7: error: deleted function ‘std::unique_ptr<_Tp, _Tp_Deleter>::unique_ptr(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = Object, _Tp_Deleter = std::default_delete<Object>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<Object>]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:88:31: error: used here
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:89:11: error: passing ‘const std::unique_ptr<Object>’ as ‘this’ argument of ‘std::unique_ptr<_Tp, _Tp_Deleter>& std::unique_ptr<_Tp, _Tp_Deleter>::operator=(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = Object, _Tp_Deleter = std::default_delete<Object>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<Object>]’ discards qualifiers
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/unique_ptr.h:208:19: error: deleted function ‘std::unique_ptr<_Tp, _Tp_Deleter>& std::unique_ptr<_Tp, _Tp_Deleter>::operator=(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = Object, _Tp_Deleter = std::default_delete<Object>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<Object>]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:89:11: error: used here
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:90:11: error: passing ‘const std::unique_ptr<Object>’ as ‘this’ argument of ‘std::unique_ptr<_Tp, _Tp_Deleter>& std::unique_ptr<_Tp, _Tp_Deleter>::operator=(std::unique_ptr<_Tp, _Tp_Deleter>&&) [with _Tp = Object, _Tp_Deleter = std::default_delete<Object>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<Object>]’ discards qualifiers
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h: In static member function ‘static _OI std::__copy_move<true, false, std::random_access_iterator_tag>::__copy_m(_II, _II, _OI) [with _II = const std::unique_ptr<Object>*, _OI = const std::unique_ptr<Object>*]’:
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:404:70:   instantiated from ‘_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = true, _II = const std::unique_ptr<Object>*, _OI = const std::unique_ptr<Object>*]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:442:39:   instantiated from ‘_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = true, _II = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >, _OI = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:506:47:   instantiated from ‘_OI std::move(_II, _II, _OI) [with _II = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >, _OI = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1602:5:   instantiated from ‘void std::__rotate(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1677:7:   instantiated from ‘void std::rotate(_FIter, _FIter, _FIter) [with _FIter = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
file.cpp:76:35:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:368:8: error: passing ‘const std::unique_ptr<Object>’ as ‘this’ argument of ‘std::unique_ptr<_Tp, _Tp_Deleter>& std::unique_ptr<_Tp, _Tp_Deleter>::operator=(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = Object, _Tp_Deleter = std::default_delete<Object>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<Object>]’ discards qualifiers
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/unique_ptr.h:208:19: error: deleted function ‘std::unique_ptr<_Tp, _Tp_Deleter>& std::unique_ptr<_Tp, _Tp_Deleter>::operator=(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = Object, _Tp_Deleter = std::default_delete<Object>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<Object>]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:368:8: error: used here
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/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::unique_ptr<Object>*, _BI2 = const std::unique_ptr<Object>*]’:
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:601:18:   instantiated from ‘_BI2 std::__copy_move_backward_a(_BI1, _BI1, _BI2) [with bool _IsMove = true, _BI1 = const std::unique_ptr<Object>*, _BI2 = const std::unique_ptr<Object>*]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:610:34:   instantiated from ‘_BI2 std::__copy_move_backward_a2(_BI1, _BI1, _BI2) [with bool _IsMove = true, _BI1 = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >, _BI2 = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:681:15:   instantiated from ‘_BI2 std::move_backward(_BI1, _BI1, _BI2) [with _BI1 = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >, _BI2 = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1625:5:   instantiated from ‘void std::__rotate(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1677:7:   instantiated from ‘void std::rotate(_FIter, _FIter, _FIter) [with _FIter = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
file.cpp:76:35:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:566:6: error: passing ‘const std::unique_ptr<Object>’ as ‘this’ argument of ‘std::unique_ptr<_Tp, _Tp_Deleter>& std::unique_ptr<_Tp, _Tp_Deleter>::operator=(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = Object, _Tp_Deleter = std::default_delete<Object>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<Object>]’ discards qualifiers
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/unique_ptr.h:208:19: error: deleted function ‘std::unique_ptr<_Tp, _Tp_Deleter>& std::unique_ptr<_Tp, _Tp_Deleter>::operator=(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = Object, _Tp_Deleter = std::default_delete<Object>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<Object>]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:566:6: error: used here
make: *** [file.o] Error 1

您不能将rotateconst_iterator s一起使用,因为rotate改变指定范围的元素。相反,您必须使用iterator s。

如果要信任cplusplus文档,那么rotate实际上调用swap(T &, T &),因此您不能使用const_iterator s。

(我想有人可能会要求为在const_iterator上操作的列表提供一个专门的rotate版本。我认为你可以很容易地建立一个。)

相关内容

  • 没有找到相关文章

最新更新