If TPtr
can only be a pointer, you're looking for Boost's remove_pointer
.
If you're wondering how in the world that works, it uses partial specialization in this manner:
template<typename T>
struct RemovePointer;
template<typename T>
struct RemovePointer<T*>
{
typedef T Type;
};
int main()
{
RemovePointer<int*>::Type foobar; // foobar has the type int
return 0;
}
If TPtr
can be either an iterator or a pointer, you need the iterator_traits
class, which is part of the standard library. In your case, it's used like this:
template <class TPtr>
struct foo {
typedef TPtr ptr_type;
// The compiler won't know for sure if value_type is actually
// a type until TPtr is known. The typename keyword is a hint
// to the compiler so it doesn't cause an error.
typedef typename iterator_traits<TPtr>::value_type element_type;
};
Believe it or not, it works by partial specialization as well. It's basically defined like this:
// Primary template for iterators
template<class Iterator>
struct iterator_traits
{
typedef typename Iterator::difference_type difference_type;
typedef typename Iterator::value_type value_type;
typedef typename Iterator::pointer pointer;
typedef typename Iterator::reference reference;
typedef typename Iterator::iterator_category iterator_category;
};
// Partial specializations for pointers
template<class T>
struct iterator_traits<T*>
{
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef T* pointer;
typedef T& reference;
typedef random_access_iterator_tag iterator_category;
};
template<class T>
struct iterator_traits<const T*>
{
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef const T* pointer;
typedef const T& reference;
typedef random_access_iterator_tag iterator_category;
};
That's why the iterator_traits
class works on both iterators and pointers.