LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 12-17-2004, 09:14 AM   #1
cyu021
Member
 
Registered: Oct 2004
Posts: 43

Rep: Reputation: 15
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
 
Old 12-17-2004, 11:15 AM   #2
bm17
Member
 
Registered: Sep 2004
Location: Santa Cruz, CA, USA
Distribution: Redhat 9.0
Posts: 104

Rep: Reputation: 15
> 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.
 
Old 12-17-2004, 06:13 PM   #3
cyu021
Member
 
Registered: Oct 2004
Posts: 43

Original Poster
Rep: Reputation: 15
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
 
Old 12-17-2004, 07:52 PM   #4
bm17
Member
 
Registered: Sep 2004
Location: Santa Cruz, CA, USA
Distribution: Redhat 9.0
Posts: 104

Rep: Reputation: 15
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Telling people to use "Google," to "RTFM," or "Use the search feature" Ausar General 77 03-21-2010 11:26 AM
Replacing "function(x)" with "x" using sed/awk/smth Griffon26 Linux - General 3 11-22-2006 10:47 AM
"undefined reference" to a template class function btb Programming 3 08-25-2005 05:02 PM
"Function not implemented" error in call to "sem_open()" Krishnendu8 Linux - Newbie 1 06-07-2003 02:52 AM
"Function not imlemented" error in call to "sem_open()" Krishnendu8 Linux - Networking 0 06-07-2003 02:19 AM


All times are GMT -5. The time now is 09:31 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration