In an algorithm, I can determine the value_type
directly from the iterator via iter::value_type
. Why do algorithms use iterator_traits
to do the same?
#include <iostream>
#include <vector>
#include <iterator>
#include <typeinfo>
using namespace std;
template<typename iter>
void for_each(iter first, iter end)
{
cout << "container value type: "
<< typeid(typename iter::value_type).name()
<< endl;
cout << "container value type: "
<< typeid(typename iterator_traits<iter>::value_type).name()
<< endl;
}
int main()
{
vector<int> v1;
for_each(begin(v1), end(v1));
return 0;
}
Output:
container value type: i
container value type: i
For a type
iterator
to be an iterator, it is not necessary to have aniterator::value_type
alias. For example, every pointer is an iterator, namely a ContiguousIterator. The user might write:Your code accepting iterators is expected to work with such a function call. Pointers aren't the only problem though:
std::iterator_traits
is a very old construct from C++98, and back in those days, you weren't able to obtain information such as thevalue_type
of an iterator withdecltype()
, becausedecltype
didn't exist. Nowadays, you could write something like:for some iterators, but not all.
Be careful, the
value_type
can be customized in two ways that break the above code:iterator::value_type
type alias, whichstd::iterator_traits
will look forstd::iterator_traits<iterator>
yourselfBecause these customization points exist, you must always use
std::iterator_traits
when accessing type information about an iterator. Even in situations wheredecltype
orauto
look okay, your code could be incorrect becausestd::iterator_traits
was specialized for the iterator you're working with.See also: What is the design purpose of iterator_traits?