What does void(U::*)(void) mean?

后端 未结 5 1100
醉梦人生
醉梦人生 2021-01-18 12:50

I was looking at the implementation of the is_class template in Boost, and ran into some syntax I can\'t easily decipher.

    template 

        
5条回答
  •  执念已碎
    2021-01-18 13:37

    You're right, it is analogous to a function pointer. Rather, this is a pointer to member function, where the member is of the class U.

    The difference in type is necessitated because member functions have an implicit this pointer, as they cannot be called without an instance. Getting rid of the template might make it a bit easier:

    struct foo
    {
        void bar(void);
    };
    

    void(*)(void) won't do, as this has no way to communicate an instance of the class. Rather, we need:

    void (foo::*)(void)
    

    Indicating that this function pointer requires an instance of foo.


    For what it's worth, you use them like this:

    typedef void (foo::*func_ptr)(void);
    
    foo f;
    foo* fp = &f;
    func_ptr func = &foo::bar;
    
    (f.*func)();
    (fp->*func)();
    

提交回复
热议问题