When "function pointer" meets "template"... I can't get rid of this compiling er
Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 '*'
> 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.
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 ?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.