开发者

Iterator nested typedefs

I'm creating a custom iterator type, and the only use case right now is std::for_each. But apparently, it's not enough to mimic the pointer interface (I'm only doing forward iteration), there are like, a bajillion nested typedefs. I managed to figure out what to put for iterator_category, but I'm having real trouble figuring out what value_type and pointer and reference should be, because, y'know, I'm not building a container here, it's an iterator. Why would for_each even want to know or care? All it's going to do is forward said on to another function开发者_如何学编程.


If you want to use a type T as an iterator, you must ensure that std::iterator_traits can be specialized for that type. That means you either need to provide the five nested typedefs that it defers to by default, or you need to specialize std::iterator_traits yourself. The five nested typedefs it requires are

  • difference_type, which is some type that can represent the distance between two iterators (e.g., as would be returned by std::distance)

  • value_type, which is the type of the object pointed to by the iterator

  • pointer, which is the return type of the iterator type's operator->. This doesn't necessarily need to be a pointer type and it doesn't necessarily need to be value_type* or value_type const*. For example, if you have an iterator that generates elements, you may not have an object to which you can return a pointer. In that case, you might return an object that wraps the returned element and overloads operator-> itself.

  • reference, which is the return type of the iterator type's operator*. This doesn't necessarily need to be a reference type and it doesn't necessarily need to be value_type& (or value_type const&). For example, If you're iterating over an immutable range of integers, you might just return the element by value, for performance reasons.

  • iterator_category, which must be one of the iterator category tags or a type derived from one of those tags: input_iterator_tag, output_iterator_tag, forward_iterator_tag, bidirectional_iterator_tag, and random_access_iterator_tag (all in namespace std). Algorithms can use these to select an optimal algorithm based on the iterator category.

You can't omit any of these; they all have to be defined. That said, sometimes one or more of the typedefs may not make sense. For example, if you have an iterator that generates char elements on the fly, your iterator may not implement operator-> (because char is not a class type). In this case, you might consider just using void for the pointer type, since it should never be used anyway.


value_type is what your iterator iterates over. If iter is an iterator, it's the type of *iter. pointer is the pointer to that, and reference is the reference to that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜