Why does std::iterator not define const_reference and const_pointer?

c++, c++11, iterator

Solution

I've just reviewed the spec. `const_pointer` is only defined as part of the allocator types, `std::allocator`, `std::allocator_traits`, and container types, and has nothing whatsoever to do with iterators. Therefore, it makes sense that `std::iterator` does not define a `const_pointer` typedef.

This makes sense when you think about it, because the constness of the value pointed at is inferred by the type of the iterator itself. In the containers, `iterator` always refers to a mutable value, and `const_iterator` always refers to a `const` value, so neither has any need for a `const_pointer` typedef.

Since the concept of a `const_iterator` is slightly confusing, lets quickly review `const`. The key thing to take away is that a `const iterator` and a `const_iterator` are very different things, and yes, there are `const const_iterator`s.

 a regular iterator models these two concepts
 int*; //changable pointer, to a changable int.  Modeled by iterator.
 int* const; //constant pointer, to a changable int.  Modeled by const iterator.
 //note that iterator doesn't ever refer to `const int`.

 a const_iterator models these two concepts
 const int*; //changable pointer to constant int. Modeled by const_iterator
 const int* const; //constant pointer to constnat int. Modeled by const const_iterator.
 //note that const_iterator ALWAYS refers to `const int`.

Problem

So, I made my own "standard compliant" container. I defined nested classes called `iterator` and `const_iterator` which derive from ``` std::iterator<std::bidirectional_iterator_tag, value_type> ``` where `value_type` is a typedef inside my new container class. I derive from `std::iterator` so that I can easily do ``` typedef typename std::iterator<std::bidirectional_iterator_tag,value_type>::value_type value_type; typedef typename std::iterator<std::bidirectional_iterator_tag,value_type>::difference_type difference_type; // ... etcetera ``` inside my nested iterator classes. However, cppreference.com says `std::iterator` does not have typedefs for a const_reference nor for a const_pointer. I can typedef them myself, but I am puzzled as to why these typedefs are omitted.

Original source