LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   When "function pointer" meets "template"... I can't get rid of this compiling er (https://www.linuxquestions.org/questions/linux-software-2/when-function-pointer-meets-template-i-cant-get-rid-of-this-compiling-er-267391/)

cyu021 12-17-2004 09:14 AM

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

bm17 12-17-2004 11:15 AM

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

cyu021 12-17-2004 06:13 PM

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

bm17 12-17-2004 07:52 PM

Quote:

Originally posted by cyu021
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 ?
Yes, just so. And you are correct about the way Apply() was handling your argument. You could have used a (unnecessary) cast there [something like Apply(((void)(*)(Thread*))SimpleThread)] if you really wanted to. And like you say, you call Apply just like any other function and allow the template to be instanciated via induction. Tricky stuff, but powerful.

Good luck!


All times are GMT -5. The time now is 03:37 PM.