Thread: Templates: What does this do?

  1. #1
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431

    Templates: What does this do?

    Code:
    struct
    {
        template <class T, typename U>
        operator U T::* () const { return static_cast<U T::*> (0); }
    } null_ptr_to_member;
    I'm confused by the template in this code, which I found here: Solving the C++ NULL Type Safety Problem specifically the U T::*, which doesn't make any sense to me. What is the ::*, and how is there more than one thing as the operator type and single template argument?
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, ::* is pointer-to-member. See for instance here. (ETA: So to be clear, U is the type pointed to, and T is the class it's supposed to be a member of, so pointing to an int that's a member of class X would be <int X::*>.)
    Last edited by tabstop; 07-13-2011 at 01:35 PM.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The point should be that in

    Code:
    #include <cstddef>
     
    namespace
    {
      struct
      {
        template <class T, typename U>
        operator U T::* () const { return static_cast<U T::*> (0); }
      } null_ptr_to_member;
    }
     
    struct X
    {
        int n;
    };
     
    void want_pointer(int X::*)
    {
    }
     
    void want_number(int)
    {
    }
     
    int main()
    {
       want_pointer(null_ptr_to_member);
       want_number(null_ptr_to_member);
     
       //compare with NULL
       want_pointer(NULL);
       want_number(NULL);
    }
    the line

    Code:
    want_number(null_ptr_to_member);
    produces an error whereas

    Code:
    want_number(NULL);
    doesn't. It is probably an error to pass a pointer value to want_number, but the compiler won't stop you with NULL.

    I guess the language is going to fix the whole issue with the null_ptr keyword.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Thanks for the responses, guys. I've seen pointer to member before, but I have never used it or needed it. I'm not even sure what you would need it for. I'm going to learn a little bit about it today.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    Quote Originally Posted by NeonBlack View Post
    Thanks for the responses, guys. I've seen pointer to member before, but I have never used it or needed it. I'm not even sure what you would need it for. I'm going to learn a little bit about it today.
    In my opinion, it makes sense.

    Code:
    class T
    {
    public:
    	int foo();
    };
    
    void foo(int (T::*)());
    void foo(int);
    
    int main()
    {
    	foo(0); //??
    }
    The compile-time integral constant equal to zero is confused in some context, such as previous example. And now, codes using 0 as a null pointer may not compile in C++0x, because 0 will be matched by non-const rvalue-reference, and then it is not a compile-time integral constant anymore. For example

    Code:
    std::vector<int*> v;
    v.insert(v.begin(), 0); //error in C++0x
    so C++0x provided a new keyword nullptr to fix this problem, use nullptr instead of your null_ptr_to_memeber in C++0x.
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. templates
    By l2u in forum C++ Programming
    Replies: 18
    Last Post: 08-30-2006, 12:09 PM
  2. templates
    By lilhawk2892 in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2006, 02:28 PM
  3. help with templates
    By drdodirty2002 in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2004, 05:25 AM
  4. Templates
    By Trauts in forum C++ Programming
    Replies: 3
    Last Post: 05-16-2003, 03:58 PM
  5. Templates - why?
    By nickname_changed in forum C++ Programming
    Replies: 5
    Last Post: 03-23-2003, 11:14 AM