开发者

how to get the type of a deferred template parameter

开发者 https://www.devze.com 2023-01-03 23:14 出处:网络
Is there a way to get the defered type of a class template parameter ? template <class TPtr> struct foo {

Is there a way to get the defered type of a class template parameter ?

template <class TPtr>
struct foo {
    typedef TPtr ptr_type;
    typedef ??? element_type; /* shall be the type of a deferred TPtr*/
};

so foo<const char*>::element_type results in const char, and foo<std::vector<int>::iterator_type>::elemen开发者_JS百科t_type results in int.

i am aware of that i can use the value_type typedef for c++ iterators (like std::vector<int>::iterator_type::value_type), but raw pointers havent got a value_type typedef, so i am out of luck there.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消