简体   繁体   English

模板化函数指针

[英]templated function pointer

I have an approach to call delayed function for class: 我有一种方法来调用类的延迟函数:

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

I want to make templated functor. 我想制作模板化的仿函数。 And the first problem is that: 第一个问题是:

template <typename T>
typedef void (MyClass::*TFunc<T>) (T param);

Causes compiler error: "template declaration of 'typedef'". 导致编译器错误:“typedef'的模板声明”。 What may be a solution? 什么可能是解决方案?

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

There are no template typedefs in C++. C ++中没有模板typedef。 There is such an extension in C++0x. C ++ 0x中有这样的扩展。 In the meantime, do 在此期间,做

template <typename T>
struct TFunc
{
    typedef void (MyClass::*type)(T param);
};

and use TFunc<T>::type (prefixed with typename if in a dependant context) whenever you would have used TFunc<T> . 并使用TFunc<T>::type (前缀typename如果在依赖于上下文)每当你会使用TFunc<T>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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