c++ - Why does std::iterator not define const_reference and const_pointer? -
so, made own "standard compliant" container. defined nested classes called iterator
, const_iterator
derive from
std::iterator<std::bidirectional_iterator_tag, value_type>
where value_type
typedef inside new container class. derive std::iterator
can 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 nested iterator classes. however, cppreference.com says std::iterator
not have typedefs const_reference nor const_pointer. can typedef them myself, puzzled why these typedefs omitted.
i've reviewed spec. const_pointer
defined part of allocator types, std::allocator
, std::allocator_traits
, , container types, , has nothing whatsoever iterators. therefore, makes sense std::iterator
not define const_pointer
typedef.
this makes sense when think it, because constness of value pointed @ inferred type of iterator itself. in containers, iterator
refers mutable value, , const_iterator
refers const
value, neither has need const_pointer
typedef.
since concept of const_iterator
confusing, lets review const
. key thing take away const iterator
, const_iterator
very different things, , yes, there const const_iterator
s.
regular iterator models these 2 concepts int*; //changable pointer, changable int. modeled iterator. int* const; //constant pointer, changable int. modeled const iterator. //note iterator doesn't ever refer `const int`. const_iterator models these 2 concepts const int*; //changable pointer constant int. modeled const_iterator const int* const; //constant pointer constnat int. modeled const const_iterator. //note const_iterator refers `const int`.
Comments
Post a Comment