3

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'". 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

brigadir
  • 6,874
  • 6
  • 46
  • 81
  • 1
    possible duplicate of [Alternative to template declaration of typedef](http://stackoverflow.com/questions/3708593/alternative-to-template-declaration-of-typedef) – kennytm Sep 16 '10 at 14:54
  • Correct me if I'm wrong (this syntax is far from easy and I'm too lazy^Hbusy to check for myself), but couldn't `((*obj).*(func)) (value)` be spelled `obj->*func(value)` instead? – sbi Sep 16 '10 at 14:55
  • @sbi: `(obj->*func)(value)` to be precise. – Alexandre C. Sep 16 '10 at 14:57

1 Answers1

10

There are no template typedefs in C++. There is such an extension in 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>.

Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
  • 2
    `+1` for being 36secs faster than me! `:)` – sbi Sep 16 '10 at 14:53
  • Seems not working... The compiler shows an error: "expected `)' before '<' token" in "typedef void (MyClass::*type)(T param);" I tried to remove in the line ("(MyClass::*type)(T param)"), the error appears in "void DelayedFunction (TFunc::type func, T param, float time);" - "expected `)' before 'func'"... I don't understand the reason of errors. – brigadir Sep 17 '10 at 14:38