简体   繁体   English

iterator_traits中嵌套指针类型的用途是什么?

[英]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_categoryvalue_typedifference_typepointerreference 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_typedifference_typeiterator_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_categoryvalue_typedifference_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 expression i->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 and a-> , respectively. 应德音响定义为迭代器的基准和指针类型,即,用于迭代器对象a ,类型相同的类型*aa->分别。 [...] [...]

The default values T* and T& for pointer and reference of course satisfy the iterator requirements. pointerreference的默认值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's value_type . 可读迭代器(以及今天的输入迭代器)的reference类型实际上不必是引用,只要它可以转换为迭代器的value_type When the value_type is a class, however, it must still be possible to access members through operator-> . 但是,当value_type是一个类时,仍然可以通过operator->访问成员。 Therefore, an iterator whose reference type is not in fact a reference must return a proxy containing a copy of the referenced value from its operator-> . 因此,其reference类型实际上不是reference的迭代器必须从其operator->返回包含引用值的副本的代理。

The return types for iterator_facade 's operator-> and operator[] are not explicitly specified. 未明确指定iterator_facadeoperator->operator[]的返回类型。 Instead, those types are described in terms of a set of requirements, which must be satisfied by the iterator_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_traitspointer类型,即使使用代理迭代器,也需要boost::iterator_facade会做正确的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在struct std :: iterator_traits &lt;...&gt;中没有名为“ pointer”的类型 - no type named ‘pointer’ in struct std::iterator_traits<…> 专门化iterator_traits - specializing iterator_traits &quot;typename iterator_traits&quot; 是什么意思<InputIt> ::difference_type”是什么意思? - What does "typename iterator_traits<InputIt>::difference_type" mean? “struct std::iterator_traits”中没有名为“value_type”的类型 - no type named ‘value_type’ in ‘struct std::iterator_traits std::iterator_traits 中类型别名的“默认值”总是正确的吗? - Are the “defaults” for the type alises in std::iterator_traits always correct? iterator_traits SFINAE友好 - iterator_traits SFINAE friendliness 前向迭代器iterator_traits :: reference的要求 - Requirements for forward iterator iterator_traits::reference std::iterator_traits 中的 iterator_category 与 iterator_category() 有什么区别 - What is a difference between iterator_category vs iterator_category() in std::iterator_traits “struct std::iterator_traits”中没有名为“iterator_category”的类型<int* const> '</int*> - No type named 'iterator_category' in 'struct std::iterator_traits<int* const>' 遇到错误:“struct std::iterator_traits”中没有名为“iterator_category”的类型<std::vector<int> &gt; - Encountring error: no type named ‘iterator_category’ in ‘struct std::iterator_traits<std::vector<int> >
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM