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.
I’m creating a custom iterator type, and the only use case right now is
Share
If you want to use a type
Tas an iterator, you must ensure thatstd::iterator_traitscan 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 specializestd::iterator_traitsyourself. The five nested typedefs it requires aredifference_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 bevalue_type*orvalue_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 overloadsoperator->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 bevalue_type&(orvalue_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, andrandom_access_iterator_tag(all in namespacestd). 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
charelements on the fly, your iterator may not implementoperator->(becausecharis not a class type). In this case, you might consider just usingvoidfor thepointertype, since it should never be used anyway.