The C++ standard library has both iterator
and iterator_traits
templates defined. It seemed that in general, the iterator_traits
just extracts properties that defined explicitly in iterator
. If a iterator_traits
is to be used, one can use the corresponding iterator
directly. If a specialization of certain type (e.g. T*
) is required, a specialization of iterator
could match the requirement. What's the benefit this indirection?

- 28,148
- 6
- 56
- 87

- 89
- 7
-
What's the connection between `T*` and `iterator`? The latter is a deprecated class that you inherit from to get some typedefs; `T*` can't inherit from anything. – HolyBlackCat Apr 04 '22 at 12:58
-
If I have the function `template
void for_each(It begin, It end)` I don't want to have to write any specializations or boilerplate to convert a `U*` into a `iterator`. I just want to work with any type of iterator, so I use `iterator_traits` as it takes care of it for me. – NathanOliver Apr 04 '22 at 13:01 -
Try to implement template with iterators which will work for any kind of iterator: raw pointers (in c-array) or vector integrator or list iterator, and try get type of pointed item or kind of iterator (random access iterator, sequential iterator). – Marek R Apr 04 '22 at 13:02
-
2Note that `std::iterator` is deprecated in c++17 and shouldn't be used anymore. – François Andrieux Apr 04 '22 at 13:27
1 Answers
It seemed that in general, the iterator_traits just extracts properties that defined explicitly in iterator. If a iterator_traits is to be used, one can use the corresponding iterator directly.
Not all iterators can type aliases as members.
What's the benefit this indirection?
To allow all iterators to have a uniform interface. More specifically, pointers are iterators too and as non-class types, pointers cannot have members.
So, when for example the user of an iterator wants the answer to the question "What is the type of the object pointed by an_iterator_type
", they cannot use an_iterator_type::value_type
because that cannot be defined for all iterators - specifically pointers. What they can use is std::iterator_traits<an_iterator_type>::value_type
which can be defined for all iterators - including pointers. That is the purpose of std::iterator_traits
.
The standard defines the std::iterator_traits<T*>
specialisations for pointers. Although you can, you don't need to define specialisations for custom iterator types because you can instead define the types as members, which will be used by the generic definition of std::iterator_traits
.
The intention of std::iterator
was to be optionally used as a base class to help define the member types for custom iterators. However its use was never necessary, it is considered to be a flawed design and its use has long been discouraged. Since C++17, it has been deprecated and it may be removed from future standards.
An example that hopefully demonstrates the problem of the design of std::iterator
:
// using the deprecated std::iterator
struct iterator : std::iterator<
std::input_iterator_tag, // OK so far
long, // can you guess what member this argument defines?
long, // how about this?
const long*, // you can probably guess this one
long> // still, no clue
{
...
// recommended way without using std::iterator
struct iterator
{
using iterator_category = std::input_iterator_tag;
using value_type = long;
using difference_type = long;
using pointer = const long*;
using reference = long;
...

- 232,697
- 12
- 197
- 326
-
As @François Andrieux pointed out, the `iterator template` has been deprecated. I've read some threads. Can I interpret this way: although it was not the root cause of deprecation, there were redundancy between `iterator template` and `iterator_traits template`. Then indirection of `iterator template` had been removed, making the function of `iterator_traits template` more sensible? – user3059627 Apr 04 '22 at 14:49
-
The thread mentioned above: [Preparation for std::iterator Being Deprecated](https://stackoverflow.com/questions/37031805/preparation-for-stditerator-being-deprecated) and [Why is std::iterator deprecated?](https://stackoverflow.com/questions/43268146/why-is-stditerator-deprecated) – user3059627 Apr 04 '22 at 14:49
-
@user3059627 `As @ François Andrieux pointed out, the iterator template has been deprecated.` I also mention the deprecation in my answer. `Can I interpret this way: although it was not the root cause of deprecation, there were redundancy between iterator template and iterator_traits template.` No, I wouldn't agree with that interpretation. `Then indirection of iterator template had been removed` It hasn't been removed yet. You can see the latter part of my answer, as well as the accepted answers to the questions you linked. The deprecation has little to do with `std::iterator_traits`. – eerorika Apr 04 '22 at 15:00
-
Thank you. I've got something. Maybe the question should have been "design purpose of `iterator template`" and it has been being gone. – user3059627 Apr 05 '22 at 14:12