When "function pointer" meets "template"... I can't get rid of this compiling er
hi folks,
First, just to let you know that I use g++ to compile the src containing following code segments. Now lets start the business. This is a templated class method I got: template<class T> void SynchList<T>::Apply(void (*func)(T)) { lock->Acquire(); list->Apply(func); lock->Release(); } and this is the function pointed by the function pointer: void SimpleThread(Thread *tmpThread) { int num; for(num = 0; num < 5; num++) { cout << "***thread " << tmpThread->GetId() << " looped " << num << " times" << endl; kernel->currentThread->Yield(); } } I have "bathList" as a pointer to SynchList<Thread *>: SynchList<Thread *> *bathList = new SyncList<Thread *>; and this is how I implement it (@ line 494 of the src): bathList->Apply((void) (SimpleThread)(Thread *)); but I get this error msg everytime I try to compile the code: ../threads/thread.cc:494: parse error before '*' Any help would be greatly appreciated. Thank you all, James |
> and this is how I implement it (@ line 494 of the src):
> bathList->Apply((void) (SimpleThread)(Thread *)); Why are you casting the invocation parameter? Shouldn't this be: bathList->Apply(SimpleThread) ? Since SimpleThread is a "void (*)(Thread*)" this should be sufficient for template instanciation. You are attempting to instanciate the SynchList::Apply template method with the expression "(void)(SimpleThread)(Thread *)" which, as far as I can tell, will call SimpleThread(Thread *) and that is not a valid invocation argument. Think of Apply as a #define macro and that might make things clearer. Templates are complex. Let me know if I am completely off-base here. |
thank you bm17,
bathList->Apply(SimpleThread) actually works !! I did what I did to try to follow the signature and didn't even notice that the argument (Thread *) itself is illegal at all. I think template instantiationg takes care of that by induction, and since "SimpleThread" is already a void function, so no need to re-cast (void) again ? Am I heading the right direction ? Thanks again. James |
Quote:
Good luck! |
All times are GMT -5. The time now is 03:37 PM. |