In the C++ standard library, why is
std::iterator_traits<const T*>::value_type
the same type as
std::iterator_traits<T*>::value_type
Why it is designed like that? Shouldn't the first be const T
and the second only T
? How are you supposed to take the underlying const-correct type of an iterator? I know you can write your own template class and specialization and get it from
std::iterator_traits<const T*>::pointer
but shouldn't there be a member typedef that holds it?
It allows me to do this:
But that's harder if
value_type
is const, because I need to useremove_const
.If I don't want to get a modifiable value then it doesn't matter whether
value_type
is const or not:Both of these work for const iterators and non-const iterators, and both work whether
value_type
is const or not, but the first example only works for const iterators if theirvalue_type
is non-const.An iterator doesn't necessarily have an underlying type of its own, an iterator usually refers to some range or some collection, and that collection is what has an underlying type. e.g
std::list<int>::const_iterator
'svalue_type
isstd::list<int>::value_type
, which isint
notconst int
.You don't necessarily want to know what the underlying type is anyway, it's more likely you want to know what the result of
*iter
is, and that's whatiterator_traits<I>::reference
tells you.