how to implement is_pointer?

后端 未结 4 1291
长情又很酷
长情又很酷 2020-12-31 19:09

I want to implement is_pointer. I want something like this:

template 
bool is_pointer( T t )
{
   // implementation
} // return true or fa         


        
相关标签:
4条回答
  • 2020-12-31 19:23

    From Dr. Dobbs.

    template <typename T> 
    struct is_pointer 
    { static const bool value = false; };
    
    template <typename T> 
    struct is_pointer<T*> 
    { static const bool value = true; };
    

    You can't do exactly what you want to do. You'll have to use this like:

    is_pointer<int*>::value
    

    It's not possible to determine this at run time.

    0 讨论(0)
  • 2020-12-31 19:35
    template <typename T>
    struct is_pointer_type
    {
        enum { value = false };
    };
    
    template <typename T>
    struct is_pointer_type<T*>
    {
        enum { value = true };
    };
    
    template <typename T>
    bool is_pointer(const T&)
    {
        return is_pointer_type<T>::value;
    }
    

    Johannes noted:

    This is actually missing specializations for T *const, T *volatile and T * const volatile i think.

    Solution:

    template <typename T>
    struct remove_const
    {
        typedef T type;
    };
    
    template <typename T>
    struct remove_const<const T>
    {
        typedef T type;
    };
    
    template <typename T>
    struct remove_volatile
    {
        typedef T type;
    };
    
    template <typename T>
    struct remove_volatile<volatile T>
    {
        typedef T type;
    };
    
    template <typename T>
    struct remove_cv : remove_const<typename remove_volatile<T>::type> {};
    
    template <typename T>
    struct is_unqualified_pointer
    {
        enum { value = false };
    };
    
    template <typename T>
    struct is_unqualified_pointer<T*>
    {
        enum { value = true };
    };
    
    template <typename T>
    struct is_pointer_type : is_unqualified_pointer<typename remove_cv<T>::type> {};
    
    template <typename T>
    bool is_pointer(const T&)
    {
        return is_pointer_type<T>::value;
    }
    

    ...but of course this is just reinventing the std::type_traits wheel, more or less :)

    0 讨论(0)
  • 2020-12-31 19:35

    You can use "typeid" operator defined in typeinfo.h for this. check this link : http://en.wikipedia.org/wiki/Typeid

    The typeid operator will give an object of std::type_info class, which has a name() function returning char *. Once you get the type in string form, you can identify the pointer easily.

    Hope it helps.

    Romil.

    0 讨论(0)
  • 2020-12-31 19:40
    template <typename T>
    bool is_pointer(T const &t) // edited: was "T t"; see the comments
    {
       return false;
    }
    
    template <typename T>
    bool is_pointer(T *t)
    {
       return true;
    }
    

    You might not believe it, but it works. The reason is that the most specific template implementation will be chosen, which is the one which takes the pointer type.

    0 讨论(0)
提交回复
热议问题