I want to be able to print any std::list iterator by printing its value. My initial code looked like this:
template<typename T>
std::ostream& operator<<(std::ostream& os, const typename std::list<T>::const_iterator& x)
{
return os << "&" << *x;
}
Which did not work, since the compiler can not determine the parameter T. I then tried making it generic over the iterator type itself and using iterator_traits to limit it to iterators.
template<
typename It,
typename = typename std::iterator_traits<It>::value_type
>
std::ostream &operator<<(std::ostream &os, const It &x)
{
return os << "&" << *x;
}
But then, of course, I get two conflicting implementations for std::ostream << *const char, since pointers are also iterators.
How can I limit the implementation to std::list iterators so I don't get a conflict?