私はunique_ptrの中でconst_iteratorでstd :: rotateを使うのに問題があります。STLはunique_ptrsのconst_iteratorを回転します
私が試みた:
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());
コールは(...イテレータ、... const_iteratorの&、...イテレータ)
を回転させるためにこれが該当する機能をもたらし、したがって、私が試しこれは: std :: vector :: const_iterator first = vec.begin(); std :: vector :: const_iterator last = vec.end(); std :: rotate(最初、中、最後);
次に、以下に示す一連のエラーメッセージが表示されます。 これはバグですか、私は間違いをしましたか? rotate
は、指定された範囲の要素を変化するため
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
が、オブジェクト自体は変更されませんが、唯一の右に移動しました? – chemelnucfin
@chemelnucfin:イテレータは変更されませんが、それらが指し示す内容は変更されます。 'const iterator'と' const_iterator'の違いに注目し、回転させるイテレータは実際にコピーによって渡されます。 – tomasz
@tomasz、私は上記の項目でスワップを使用しようとしましたが、それはconst_iteratorsで動作するように思われましたか?唯一のものはconst_iterとiterを混合していませんでした。 swap(players.begin()、middle)はしませんでしたが、const_iter i = vec.begin()=> swap(i、middle)が働いていました。私は実際の結果をテストしなかったが、少なくともコンパイルした。 – chemelnucfin