views:

132

answers:

2

x__x

I want to do something like this:

typedef long (* fp)(BaseWindow< fp > & wnd, HWND hwnd, long wparam, long lparam);

But I get a compile error:

error C2065: 'fp' : undeclared identifier

Is it possible to implement this somehow?

+8  A: 

No it isn't, because the type of the template parameter would include itself. This would yield to an endless recursion in the type.

If instead of the class template specialization, you accept a base-class of it, that's very possible

struct TemplateBase {

};

typedef long (*fpType)(TemplateBase&, HWND, long, long);

template<fpType FP>
struct BaseWindow : TemplateBase {

};


long sampleFunc(TemplateBase &b, HWND hwnd, long wparam, long lparam) {
  ...
}

int main() {
    BaseWindow<sampleFunc> bw;
    sampleFunc(bw, ...);
}

What do you want to do with this?

Johannes Schaub - litb
It was a wild idea for a problem I'm facing. But maybe there are better ways. It would be too long to explain the problem as a comment here, so perhaps I'll make an official question of it a while later.Thank you for the answer.
Rao
Note that you could add virtual functions to the base class, and calls to them from within sampleFunc will give control to the derived class, like with virtual function in non-template classes.
Johannes Schaub - litb
A: 

From the related links section on this very page: How can I typedef a function pointer that takes a function of its own type as an argument?

outis