模板化函数指针

bri*_*dir 3 c++ templates pointers function

我有一种方法来调用类的延迟函数:

//in MyClass declaration:
typedef void (MyClass::*IntFunc) (int value);
void DelayedFunction (IntFunc func, int value, float time);
class TFunctorInt
{
public:
    TFunctorInt (MyClass* o, IntFunc f, int v) : obj (o), func (f), value (v) {}
    virtual void operator()();
protected:
    MyClass* obj;
    IntFunc func;
    int value;
};
//in MyClass.cpp file:
void MyClass::DelayedFunction (IntFunc func, int value, float time)
{
    TFunctorBase* functor = new TFunctorInt (this, func, value);
    DelayedFunctions.push_back (TDelayedFunction (functor, time)); // will be called in future
}
void MyClass::TFunctorInt::operator()()
{
    ((*obj).*(func)) (value);
}
Run Code Online (Sandbox Code Playgroud)

我想制作模板化的仿函数.第一个问题是:

template <typename T>
typedef void (MyClass::*TFunc<T>) (T param);
Run Code Online (Sandbox Code Playgroud)

导致编译器错误:"typedef'的模板声明".什么可能是解决方案?

PS:基于http://www.coffeedev.net/c++-faq-lite/en/pointers-to-members.html#faq-33.5的代码

Ale*_* C. 10

C++中没有模板typedef.C++ 0x中有这样的扩展.在此期间,做

template <typename T>
struct TFunc
{
    typedef void (MyClass::*type)(T param);
};
Run Code Online (Sandbox Code Playgroud)

无论什么时候使用,都要使用TFunc<T>::type(typename在依赖的上下文中加上if)TFunc<T>.

  • `+ 1`比我快36秒!`:)` (2认同)