Я пытаюсь скомпилировать код из Stroustrups C ++, 4-е издание, стр. 124 и 125, раздел «Особенности итератора». К сожалению, компиляция приводит к многочисленным ошибкам, которые выглядят так, как будто они есть в системе шаблонов. Кто-нибудь знает, что не так с этим кодом?
Спасибо
#include <iostream>
#include <iterator>
#include <algorithm>
#include <forward_list>
#include <vector>
using namespace std;
template<typename Ran>
void sort_helper(Ran beg, Ran end, random_access_iterator_tag)
{
sort(beg, end);
}
template<typename For>
void sort_helper(For beg, For end, forward_iterator_tag)
{
vector<decltype(*beg)> v {beg, end};
sort(v.begin(), v.end());
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>; // ex. vector<int>::iterator
sort_helper(c.begin(), c.end(), Iterator_category<Iter>{});
}
int main(int argc, char *argv[])
{
forward_list<int> fl = {2, 1, 0};
vector<int> v = {2, 1, 0};
sort(fl); // this line causes compilation error
sort(v);
return 0;
}
Ошибки:
In file included from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/x86_64-linux-gnu/bits/c++allocator.h:33,
from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/allocator.h:46,
from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/string:41,
from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/locale_classes.h:40,
from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/ios_base.h:41,
from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/ios:42,
from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/ostream:38,
from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/iostream:39,
from <source>:1:
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/ext/new_allocator.h: In instantiation of 'class __gnu_cxx::new_allocator<int&>':
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/allocator.h:116:11: required from 'class std::allocator<int&>'
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/stl_vector.h:87:21: required from 'struct std::_Vector_base<int&, std::allocator<int&> >'
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/stl_vector.h:389:11: required from 'class std::vector<int&, std::allocator<int&> >'
<source>:17:28: required from 'void sort_helper(For, For, std::forward_iterator_tag) [with For = std::_Fwd_list_iterator<int>]'
<source>:31:16: required from 'void sort(C&) [with C = std::forward_list<int>]'
<source>:39:12: required from here
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/ext/new_allocator.h:62:26: error: forming pointer to reference type 'int&'
62 | typedef _Tp* pointer;
| ^~~~~~~
... and more ...