我正在通过第4版Bjarne Stroustrup的C ++书籍,并遇到了迭代器类别的问题。以下是基本上直接从未编译的书中复制的代码片段(第5.4.2.1章):
#include <algorithm>
#include <iostream>
#include <vector>
#include <forward_list>
#include <iterator>
#include <string>
template<typename Ran> // for random access iterators
void sort_helper(Ran beg, Ran end, std::random_access_iterator_tag) {
std::sort(beg, end);
}
template<typename For> // for forward access iterators
void sort_helper(For beg, For end, std::forward_iterator_tag) {
std::vector<decltype(*beg)> v{beg,end}; // compilation fails here
std::sort(v.begin(),v.end());
std::copy(v.begin(),v.end(),beg);
}
template<typename C>
using Iterator_type = typename C::iterator;
template<typename Iter>
using Iterator_category = typename std::iterator_traits<Iter>::iterator_category;
template<typename C>
void sort(C& c) {
using Iter = Iterator_type<C>;
sort_helper(c.begin(), c.end(), Iterator_category<Iter>());
}
void test(std::vector<std::string>& v, std::forward_list<int>& lst) {
sort(v);
std::sort(v.begin(),v.end());
sort(lst);
}
int main(int argc, char * argv[]) {
std::vector<std::string> sv {"c","b","d","a"};
std::forward_list<int> fli {3,6,1,7,2,9,4};
test(sv,fli);
return 0;
}
如果我注释掉使用forward iterator标记的辅助函数的内容,那么一切都会编译并完美地运行。如果注释掉函数的内容,但用
替换decltypestd::vector<decltype(beg)> v{beg,end};
将编译,但是一旦我尝试包含排序,编译就会中断,因为如果我没有取消引用,那么排序就是试图对前向迭代器进行操作。如果我包含de-ref,则错误喷射难以解析,但看起来编译器试图将参数视为指向int的指针,并且它正在抱怨const违规等。
任何人都可以提供任何见解吗?
编辑:这是编译器错误喷出的第一个错误:
In file included from small_utils2.cpp:1:
In file included from /usr/bin/../lib/c++/v1/algorithm:594:
/usr/bin/../lib/c++/v1/memory:1656:16: error: 'pointer' declared as a pointer to a reference of type 'int &'
typedef _Tp* pointer;
^
/usr/bin/../lib/c++/v1/memory:1448:22: note: in instantiation of template class 'std::__1::allocator<int &>' requested here
typedef typename allocator_type::value_type value_type;
^
/usr/bin/../lib/c++/v1/vector:330:22: note: in instantiation of template class 'std::__1::allocator_traits<std::__1::allocator<int &> >' requested here
typedef typename __alloc_traits::size_type size_type;
^
/usr/bin/../lib/c++/v1/vector:484:15: note: in instantiation of template class 'std::__1::__vector_base<int &, std::__1::allocator<int &> >' requested here
: private __vector_base<_Tp, _Allocator>
^
small_utils2.cpp:22:30: note: in instantiation of template class 'std::__1::vector<int &, std::__1::allocator<int &> >' requested here
std::vector<decltype(*beg)> v{beg,end};
^
small_utils2.cpp:37:2: note: in instantiation of function template specialization 'sort_helper<std::__1::__forward_list_iterator<std::__1::__forward_list_node<int, void *> *> >' requested here
sort_helper(c.begin(), c.end(), Iterator_category<Iter>());
^
small_utils2.cpp:45:2: note: in instantiation of function template specialization 'sort<std::__1::forward_list<int, std::__1::allocator<int> > >' requested here
sort(lst);
^
答案 0 :(得分:2)
std::vector<decltype(*beg)> v{beg,end};
*beg
返回对向量的value_type
对象的引用。您收到错误是因为您无法使用引用向量。
您可以使用std::remove_reference
来放弃引用,也可以使用迭代器类型中公开的value_type
成员:
std::vector<typename For::value_type> v{beg, end}; // ^^^^^^^^^^^^^^^^^^^^^^^^