Neil Kirk
Neil Kirk

Reputation: 21773

A template function that takes iterators or pointers and gets the pointed to type

Unfortunately I am not using C++11 (then I would use auto).

Suppose I have a function like the following (very simple example)

template<class ITR>
void f(ITR begin, ITR end)
{
    TYPE temp = *begin;
}

I want to store some temp values from the iterators in local variables, but I don't know how to get TYPE. Furthermore, the function will be called with std iterators and raw pointers.

Any help? Thanks

Upvotes: 2

Views: 197

Answers (2)

jrok
jrok

Reputation: 55395

Use std::iterator_traits

template<class ITR>
void f(ITR begin, ITR end)
{
    typename std::iterator_traits<ITR>::value_type temp = *begin;
}

Upvotes: 7

Benjamin Lindley
Benjamin Lindley

Reputation: 103703

typedef typename std::iterator_traits<ITR>::value_type value_type;

Upvotes: 3

Related Questions