首页 > 解决方案 > C ++:Stroustrup iterator_traits 示例未编译?

问题描述

我正在尝试从 Stroustrups C++ 4th Ed Page 124 和 125,Iterator Traits 部分编译代码。不幸的是,编译会导致许多错误,这些错误看起来像是在模板系统中。有谁知道这段代码有什么问题?

谢谢

#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 ...

标签: c++c++11

解决方案


这是书中的一个已知错误。

来自http://www.stroustrup.com/4th_printing3.html

第 125 页:s/decltype(*beg)/Value_type<For>/修复 decltype 的使用将占用比我这里更多的空间。


推荐阅读