I am trying to use a struct as a wrapper for a member function of a class. I have a templated struct that is supposed to hold a pointer to the member function, but in a generic sense not pointing to the instance's specific function. I already figured out the template specilization, but have not been able to figure out the syntax for the storage of the function.

More clearly: I have class X that has non-static function doY() and doZ(). In the class I have made, class A, I have an instance of X named ex. Depending on the situation I want to call one of the functions. I have made a struct called wrapper. Wrapper should store a reference to doY() or doZ() in a static variable named function by using template specilization. I should then be able to call ex.wrapper::function to run either ex.doY() or ex.doZ().

This is the current version of my code (showing the specialization for some class B), which leads to a func is not a member of wrapper error


template <class T>
class A
    T timer;
    X ex;
    template<class Type>
    struct create_timer_getter{};

    void doSomething()
        timer = ex.wrapper<T>::func();


template <>
struct A<B>::wrapper<B>
    // V-- this is where I am having issues --V
    static const X::(func)(int, int, char) = &X::doY

1 Answers

Austin On Best Solutions

Thank you molbdnilo, that syntax compiled correctly.

The pointer-to-member declaration syntax is return_type (class_name::variable_name)(parameter_types). The pointer-to-member dereferencing operators are . and ->*

Since then, I have also found an alternative syntax that also works:

typedef return_type (class_name::user_defined_type_name)(parameter_types);
user_defined_type_name variable_name = &namespace(if needed)::class_name::member_function_name;