Recursively dereference pointer

前端 未结 2 484
再見小時候
再見小時候 2021-01-15 19:42

While trying to answer one question here, I found this question:

How to recursively dereference pointer (C++03)?

Adapted code from the answer is following:

2条回答
  •  抹茶落季
    2021-01-15 19:52

    You can do it with a trait to compute the ultimate return type, here dubbed remove_all_pointers:

    #include 
    
    template  struct remove_all_pointers
    { typedef typename std::remove_reference::type type; };
    template  struct remove_all_pointers
    { typedef typename std::remove_reference::type type; };
    
    template 
    T & dereference(T & p)
    { return p; }
    
    template 
    typename remove_all_pointers::type & dereference(U * p)
    { return dereference(*p); }
    
    int main(int argc, char * argv[])
    {
      return dereference(argv);
    }
    

    You may need to add CV-variants; I'm still thinking about that.

提交回复
热议问题