Calling All Gurus: Function Pointers to Templated Functions?
I have a table of function pointers at present. I need this for speed and some other reasons. But the functions themselves are all basically the same code and there are a like a dozen of them. So i thought, why not templatize them? Ah, but i need a function pointer to each of the functions. Is it legal and portable to have a pointer to a template function? If so, what on earth would the syntax look like? Google isn't putting out. Please help!
Rating++ for anyone with a satisfying answer :-)
Yes, it is legal and possible.
#include <typeinfo>#include <stdio.h>template< typename I >void func() { (void)I(1); puts(typeid(I).name());}void (*table[])() = { &func<int>, &func<char>,};int main() { (*table[0])(); (*table[1])(); return 0;}
I think the type you want would be template<typename A, typename B> typedef A (* foobar)(B);, but that doesn't work. I'll just steal Oluseyi's reason: Template typedefs are not part of C++.
I think there was some trick of sticking it in a struct and templating that but I don't remember exactly. Nevermind, flangazor posted it.
I think there was some trick of sticking it in a struct and templating that but I don't remember exactly. Nevermind, flangazor posted it.
Quote:Original post by OluseyiSutter to the rescue!
Big problem: template typedefs are not part of C++.
Are we saying that hplus's doesn't work? It looks scary and i'm afraid of it :-O
I'm refactoring my whole collision detection scheme this evening and i think i may have found a way around the template-function-pointer problem, but i'm still curious if they work or are practical at all.
I'm refactoring my whole collision detection scheme this evening and i think i may have found a way around the template-function-pointer problem, but i'm still curious if they work or are practical at all.
Use a template functor instead:
template <typename T>struct BaseFunctor{ T operator() (T arg) { ... }}
Have your other functors derive from BaseFunctor<T> and polymorphism will do the rest.
While your absolute right Oluseyi, I'm not sure how that relates to pointers to template functions. hplus0603's code works fine (and just to be sure I tested it under gcc3.2).
Quote:Original post by joanusdmentiaOf course it works. hplus0603 is highly competent. The problem is that less competent programmers may have to maintain that code, and the code isn't particularly readable or easily comprehensible.
While your absolute right Oluseyi, I'm not sure how that relates to pointers to template functions. hplus0603's code works fine (and just to be sure I tested it under gcc3.2).
The functor's elegance lies in its simplicity.
You don't need/can't use template typedefs for this (though I'd use just use boost::functor as well)
#include <typeinfo>#include <cstdio>template< typename I >void func() { (void)I(1); puts(typeid(I).name());}typedef void (*func_t)();func_t table[2];int main() { table[0] = &func<int>; table[1] = &func<char>; (*table[0])(); (*table[1])(); return 0;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement