开发者

STL rotating const_iterators of unique_ptrs

I have problems using std::rotate on a const_iterator over a unique_ptr middle.

I have tried:

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());

This results in no matching function for call to rotate(...iterator, ...const_iterator &, ...iterator)

and therefore I tried this: std::vector >::const_iterator first = vec.begin(); std::vector >::const_iterator last = vec.end(); std::rotate(first, middle, last);

then it gives a bunch of error messages shown below. Is this a bug or did I make some mistake?

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


You can't use rotate with const_iterators, since rotate changes the elements of the range specified. Instead, you have to use iterators.


If the cplusplus documentation is to be trusted, then rotate essentially calls swap(T &, T &), so you cannot use const_iterators.

(I suppose one might ask for a specialized version of rotate for lists that operates on const_iterators. I think you can build one fairly easily, though.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜