Search code examples
c++11iterator-traits

Using iterator_traits to deduce value type from pair of iterators


I have a type

typedef std::pair<ConstIterator, ConstIterator> Range;

with

typedef typename std::vector<T>::const_iterator ConstIterator;

I would now like to use std::iterator_traits to deduce the type the iterators of a Range are pointing to.

Could anybody please tell me how I can achieve this, from an object of type Range?


Solution

  • You can write a type trait, partially specialized on a pair:

    template <typename T>
    struct value_type {
        using type = typename std::iterator_traits<T>::value_type;
    };
    
    template <typename T>
    struct value_type<std::pair<T, T>>
    : value_type<T>
    { };