简体   繁体   中英

A function pointer that points to a function that takes an object of a template class with said function pointer as template argument. Possible?

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?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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