Jump to content
  • Advertisement
Sign in to follow this  
leiavoia

Calling All Gurus: Function Pointers to Templated Functions?

This topic is 5411 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 :-)

Share this post


Link to post
Share on other sites
Advertisement
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;
}


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Quote:
Original post by joanusdmentia
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).
Of 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.

The functor's elegance lies in its simplicity.

Share this post


Link to post
Share on other sites
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;
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!