Function template pointer

I have an approach to calling a function with a delay for a 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 a template functor. And the first problem is that:

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

Causes a compiler error: β€œtypedef template declaration.” What could be the solution?

PS: code based on http://www.coffeedev.net/c++-faq-lite/en/pointers-to-members.html#faq-33.5

+3
source share
1 answer

There are no typedef patterns in C ++. In C ++ 0x, there is such an extension. Meanwhile do

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

and use TFunc<T>::type(with a prefix typename, if in a dependent context) whenever you use TFunc<T>.

+10
source

Source: https://habr.com/ru/post/1765123/


All Articles