简体   繁体   中英

How to create function pointer to template member function?

I have two template classes as below:

template<class T>
class pub {
public:
    void fun(T a, T b)
    {
       //do something with a & b
    }
}

template<class T>
class sub
{
private:
    std::set<funPtr> funPtr;
public:

    void fun2( funPtr f)
    {
        funPtr.insert(funPtr);
    }

};

I need to get a function pointer to the member function fun .

The problem is both the classes are template classes, so I don't know how to do it.

How may I solve this problem in C++?

template<typename T>
using funPtr = void (pub<T>::*)(T, T);

Usage:

std::set<funPtr<T>> funPtrs;

Or simply (inside sub where the template parameter T is visible):

using funPtr = void (pub<T>::*)(T, T);

and

std::set<funPtr> funPtrs;
template<class T>
class sub
{
private:
    // typedef void (pub<T>::*funPtr)(T,T);   // C++98
    using funPtr = void (pub<T>::*)(T,T);     // C++11
    std::set<funPtr> fset;
public:

    void fun2( funPtr f)
    {
        fset.insert(f);
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM