Jump to content
  • Advertisement
Sign in to follow this  
jmau0438

Generic Function Pointer as template parameter

This topic is 3094 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 suppose this is a two part question - Question #1: What is the syntax for a generic C++ function pointer? Are there any alternatives in the STL to make this easier, and how would that work? Question#2: If I had a generic function pointer, could I use one in a template parameter list?

Share this post


Link to post
Share on other sites
Advertisement
1) The syntax is return-type (*variable-name)([opt-params]); See function pointer tutorials. You could also try looking at boost::function, or the functional header.

2) As long as the parameter is known at compile-time, then yes:

template<typename F>
void doFunction( const F& f ) {
f(1);
}

int func1( int i ) {
return 1 + i;
}

void func2( int i ) {
}

int main() {
doFunction( func1 );
doFunction( func2 );
}





Share this post


Link to post
Share on other sites
Quote:
What is the syntax for a generic C++ function pointer?
It depends on what you mean by 'generic'. A function pointer in C++ can point at any free or static member function that matches the specified signature, and a member function pointer can point at any member function of the specified class that matches the signature. As for 'truly' generic function pointers, however, there's nothing like that built into the language. (For the details of function pointer syntax in C++, just Google 'c++ function pointer'.)
Quote:
Are there any alternatives in the STL to make this easier, and how would that work?
Yes, there are tools in the standard library and/or Boost that can help with this. I think the closest you'll come to a 'generic' function pointer/delegate in C++ is boost/tr1::function (which, when used with e.g. bind and lambda, will cover most cases you're likely to encounter in practice).

Oh, and there's also this (and variants).
Quote:
If I had a generic function pointer, could I use one in a template parameter list?
You can use built-in function pointers as template parameters. Other than that though, I'm not sure what you mean - can you give an example?

Share this post


Link to post
Share on other sites
Not in the STL, but in boost there is. Using boost, you can write:


void foobar(int x, double y, std::string z)
{
}

class C
{
void test(int x, double y, std::string z);
};


C cinstance;
boost::function<void(int,double,std::string)> func(foobar);
boost::function<void(int,double,std::string)> func2(boost::bind(&C::test, &cinstance, boost::_1));



you can then invoke either of these functions with func(3, 4.0, "hello") for example.


Some recent compilers have support for C++ TR1. If that's the case, you can #include <functional> and then replace "boost::" with "std::tr1::" and it should work as well.

Share this post


Link to post
Share on other sites
Quote:
You can use built-in function pointers as template parameters. Other than that though, I'm not sure what you mean - can you give an example?


Using function pointers as template parameters is exactly what I'm trying to do. I have a routine that I'd like to run one of a set functions, but I don't know in advance the functions that make-up the set. I could write a switch of if...else if statements but then I'd have to customize it each time. It just seemed a more reuseable solution to use a function pointer to identify and call the desired routine.

Quote:
1) The syntax is return-type (*variable-name)([opt-params]); See function pointer tutorials. You could also try looking at boost::function, or the functional header.


I have the very page you are refering to up right now. I understand how how a normal function pointer works, I've just never used one as a template parameter. And before I kill an entire day chasing my tail, I thought best to get some expert advise. The concept the author demonstrates (replacing a switch statment with function pointers) is exactly what I'm trying to do. I'm just trying to make this a generic as possible within a templated class object.

Share this post


Link to post
Share on other sites
Quote:
Not in the STL, but in boost there is.


To be honest, I know of boost but I've never actively used it. But I like each of your solutions using boost. I'll go get the libaries and install them into my VSC++ and see if I can get something similiar to work.

Share this post


Link to post
Share on other sites
OK, here was what I was able to do (which is more or less psuedo code at the moment)...

#ifndef __ABSTRACT_CREATOR__
#define __ABSTRACT_CREATOR__

#include "PrototypeInterface.h"
#include <functional>

template

<
typename PRODUCT,
typename IDENTIFIER = std :: tr1 :: function < PRODUCT* ( void ) >
Construct ( std :: tr1 :: bind ( dynamic_cast < AbstractCreator* > ( &this ) :: Create, &this, std :: tr1 :: _1 ) )
>

class AbstractCreator : public IPrototype {

public:

PRODUCT* Create ( IDENTIFIER ) { return Construct ( IDENTIFIER ); };

};



Turns out an updated VSC++ 2005 has TR1 in place, so I didn't have to use boost. How did I do, and could/should I change?

Share this post


Link to post
Share on other sites
Quote:
Original post by jmau0438
Quote:
Not in the STL, but in boost there is.


To be honest, I know of boost but I've never actively used it. But I like each of your solutions using boost. I'll go get the libaries and install them into my VSC++ and see if I can get something similiar to work.


Rather than go off and install yet another library, see if you have tr1 support first. This is fine for hobby projects as you aren't relying on it compiling on lots of different compilers. To test this, just #include <functional> and then try


void foo(int i) {}
std::tr1::function<void(int)> test(foo);



If it works you can probably use that. Boost has tons of awesome stuff, but it's really pretty advanced and no point relying on another library if you're only goign to use 1 thing out of it that you can get from your standard library.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!