如何创建模板成员函数的函数指针?

时间:2016-05-17 16:24:48

标签: c++ c++11 visual-c++

我有两个模板类,如下所示:

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);
    }

};

我需要获取一个指向成员函数fun的函数指针。

问题是这两个类都是模板类,所以我不知道该怎么做。

如何在C ++中解决这个问题?

2 个答案:

答案 0 :(得分:4)

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

用法:

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

或简单地(在模板参数sub可见的T内):

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

std::set<funPtr> funPtrs;

答案 1 :(得分:2)

xin@ubuntu:~/pipes$ gcc -arch i386 -arch x86_64 channel.cpp
gcc: error: i386: No such file or directory
gcc: error: x86_64: No such file or directory
gcc: error: unrecognized command line option ‘-arch’
gcc: error: unrecognized command line option ‘-arch’
相关问题