c++ - 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 ...
解决方案
这是书中的一个已知错误。
来自http://www.stroustrup.com/4th_printing3.html:
第 125 页:
s/decltype(*beg)/Value_type<For>/
修复 decltype 的使用将占用比我这里更多的空间。
推荐阅读
- node.js - MongoDB聚合在日期字段分组后添加两个日期之间的缺失月份
- java - 一次性局部变量是 final 是什么意思?
- go - Go 如何解析模块 URL
- segmentation-fault - 将可选的可分配数组与可选的不可分配的虚拟参数相关联时出现分段错误
- python - 不同的盒子做不同的事情
- amazon-ec2 - Redis集群创建副本绑定公网IP
- python - 在 pandas DataFrame 中检查相似地址的最快方法是什么?
- kubernetes - 带有 traefik 的 k8s:打开 udp 端口作为入口规则
- c# - 对 LINQ 或 Lambda 的 SQL 查询
- excel - 想要超链接到某个单元格