[英]What is the use of the nested pointer type in iterator_traits?
The std::iterator_traits
class template defines 5 nested types: iterator_category
, value_type
, difference_type
, pointer
and reference
. std::iterator_traits
类模板定义了5种嵌套类型: iterator_category
, value_type
, difference_type
, pointer
和reference
。 Browsing the sources of the <algorithm>
header of both libc++ and libstdc++, one can see many uses of value_type
, difference_type
and iterator_category
, but only one for reference
(inside std::iter_swap
) and none for pointer
. 浏览libc ++和libstdc ++的
<algorithm>
标头的源代码,可以看到value_type
, difference_type
和iterator_category
许多用法,但只有一个用于reference
(在std::iter_swap
)而没有用于pointer
。
My application uses a hand-built proxy iterator / proxy reference pair. 我的应用程序使用手工构建的代理迭代器/代理引用对。 I want to transition to using the Boost
iterator_facade
which lets me configure the reference type from the default T&
to an arbitrary type, but not so for the pointer type which is T*
by default. 我想转换到使用Boost
iterator_facade
,它允许我将引用类型从默认的T&
配置为任意类型,但对于默认情况下为T*
的指针类型则不然。 I want to avoid being bitten by some deeply hidden use of the nested pointer
type. 我想避免被嵌套
pointer
类型的一些深度隐藏使用所困扰。
Note : the iterator is a proxy for a builtin type without nested members, so I do not need compatibility with operator->
(for which the return type would be pointer
). 注意 :迭代器是没有嵌套成员的内置类型的代理,因此我不需要与
operator->
兼容(返回类型将是pointer
)。
Question : what use cases are there in the Standard Library for the nested type pointer
inside iterator_traits
? 问题 :标准库中有哪些用例用于
iterator_traits
的嵌套类型pointer
?
In contrast to the first three types iterator_category
, value_type
and difference_type
(which are heavily used by the standard algorithms for tag dispatching to efficient versions depending on the iterator capabilities) the last two types pointer
and reference
inside iterator_traits
appear not to be used by any algorithms but are used to define compliance with the iterator requirements. 与此相反的前三种类型
iterator_category
, value_type
和difference_type
(其大量使用标准算法用于tag分派到取决于迭代器功能有效的版本)中的最后两个类型的pointer
并且reference
内部iterator_traits
出现不被任何算法中使用但用于定义符合迭代器要求。
24.2.1 In general [iterator.requirements.general] 24.2.1一般[iterator.requirements.general]
1 [..] All iterators
i
for which the expression(*i).m
is well-defined, support the expressioni->m
with the same semantics as(*i).m
.1 [..]表达式
(*i).m
被很好地定义的所有迭代器i
支持表达式i->m
,其语义与(*i).m
。 [...][...]
24.4.1 Iterator traits [iterator.traits] 24.4.1迭代器特征[iterator.traits]
[...] In addition, the types
[...]此外,类型
iterator_traits<Iterator>::reference iterator_traits<Iterator>::pointer
shall be defined as the iterator's reference and pointer types, that is, for an iterator object
a
, the same type as the type of*a
anda->
, respectively.应德音响定义为迭代器的基准和指针类型,即,用于迭代器对象
a
,类型相同的类型*a
和a->
分别。 [...][...]
The default values T*
and T&
for pointer
and reference
of course satisfy the iterator requirements. pointer
和reference
的默认值T*
和T&
当然满足迭代器要求。 Regarding proxy references, the Boost.Iterator documentation specifies 关于代理引用, Boost.Iterator文档指定
The
reference
type of a readable iterator (and today's input iterator) need not in fact be a reference, so long as it is convertible to the iterator'svalue_type
.可读迭代器(以及今天的输入迭代器)的
reference
类型实际上不必是引用,只要它可以转换为迭代器的value_type
。 When thevalue_type
is a class, however, it must still be possible to access members throughoperator->
.但是,当
value_type
是一个类时,仍然可以通过operator->
访问成员。 Therefore, an iterator whosereference
type is not in fact a reference must return a proxy containing a copy of the referenced value from itsoperator->
.因此,其
reference
类型实际上不是reference
的迭代器必须从其operator->
返回包含引用值的副本的代理。The return types for
iterator_facade
'soperator->
andoperator[]
are not explicitly specified.未明确指定
iterator_facade
的operator->
和operator[]
的返回类型。 Instead, those types are described in terms of a set of requirements, which must be satisfied by theiterator_facade
implementation.相反,这些类型是根据一组需求来描述的,这些需求必须由
iterator_facade
实现来满足。
Conclusion : as long as a proxy iterator does not require accessing members of its underlying value_type
through .m
or ->m
, one does not need to worry about the pointer
type inside iterator_traits
, and even if one does use proxy iterators, boost::iterator_facade
will do the right thing. 结论 :只要代理迭代器不需要通过
.m
或->m
访问其底层value_type
成员,就不需要担心iterator_traits
的pointer
类型,即使使用代理迭代器,也需要boost::iterator_facade
会做正确的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.