how to implement is_pointer?

后端 未结 4 1318
长情又很酷
长情又很酷 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:35

    template 
    struct is_pointer_type
    {
        enum { value = false };
    };
    
    template 
    struct is_pointer_type
    {
        enum { value = true };
    };
    
    template 
    bool is_pointer(const T&)
    {
        return is_pointer_type::value;
    }
    

    Johannes noted:

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

    Solution:

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

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

提交回复
热议问题