Is it possible to get the value type from an arbitrary iterator (C++)?

c++, templates

Solution

Any iterator should provide `iterator_traits<Iterator>::value_type`. If it does not, then it is not an iterator. ISO C++ 2003 24.3.1[lib.iterator.traits] "Iterator traits":

To implement algorithms only in terms of iterators, it is often necessary to determine the value and difference types that correspond to a particular iterator type. Accordingly, it is required that if `Iterator` is the type of an iterator, the types

iterator_traits<Iterator>::difference_type
iterator_traits<Iterator>::value_type
iterator_traits<Iterator>::iterator_category

be defined as the iterator’s difference type, value type and iterator category, respectively.

Aside from that, there's no general way to obtain a type of an arbitrary C++ expression. C++0x will rectify it by providing `decltype`.

Problem

I have a class ``` template <typename Iterator, typename Value> class Foo { public: Foo(const Iterator& it) { ... } ... private: map<Value, int> m_; } }; ``` Is there any way to get rid of Value in the template? The Iterator may or may not be an STL iterator, but it's guaranteed that *it type is Value. I know about `iterator_traits<T>::value_type` for STL iterators, but wonder if there's any way to get Value type automatically for an arbitrary Iterator type? One trick I'm thinking about - say, we have a helper class ``` template <typename Iterator, typename Value> class Bar { public: Bar(const Iterator& dummy_iterator, const Value& dummmy_value) {} ... }; ``` Then if we instantiate Bar as Bar(it, *it), the type of Value will be known inside Bar. But I can't find a good way to combine Bar with Foo.

Original source