Jump to content
  • Advertisement
Sign in to follow this  
ZealousEngine

How to boost::bind a template function?

This topic is 4057 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

Lets say I have a template function
Quote:
template <class T> void foo( T arg1 ) {}
How can I create a boost::function, and boost::bind foo() to it? Something like the following...
Quote:
boost::function< class T, void(T) > func; func = boost::bind( &foo, _1 );
...of course that doesnt work. Oh please say its possible..

Share this post


Link to post
Share on other sites
Advertisement
Arggg you gotta be kidding.. damn I was so close to having this thing solved..

*..ack ok. Does anyone have any ideas how I could do something like this...

Quote:

template< class T > void call( int funcId, T arg1 ) {

switch( funcId ) {
case 1 : foo( arg1 ); break;
case 2 : foo2( arg1 ); break;
case 3 : foo3( arg1 ); break;
}

}


The goal is to be able to call different functions by passing in a 'id', and a argument of T type (since you can call multiple functions, the type has to be variable).

The above works, but I hate having to use a switch statement like that, it just looks ugly. There has to be a better way...

Share this post


Link to post
Share on other sites
Did you try something like this?


// We use a class to create a "template typedef" to work around C++'s broken
// rules for typedefs

template <typename T>
class UnaryFunction {
typedef boost::function<T, void(T)> type;
};

template <typename T>
void call(UnaryFunction<T>::type func, T arg1) {
func(arg1);
}

UnaryFunction<int>::type func = boost::bind(&foo<int>, _1);
call(func, 42);

Share this post


Link to post
Share on other sites
Hrmm, the only problem with that is, to avoid the 'switch' when deciding what function to call (based on the funcId), I need to store all the boost::functions in a SINGLE list/vector. That cant be done unless all the functions are uniform... which I fear for type safety reasons, isnt possible...

Im starting to think the only way I can do what im trying to do is with void pointers. That way each function will be uniform ( boost::function<void(void*)> ) so they can all exist in a single list. Then each function can pass arguments of ANY type (which will be cast back into their correct type INSIDE the target function, since it knows what to expect).

I think I could limit the danger a tad by storing the SIZE of the argument when its passed in through call(), and comparing it to the size of the cast when we try and extract the argument inside the target function. That would protect us from bad cast overlap issues at least.. but still, I hate void pointers just as much as the next guy..

Argg I just wish there was a way to pass a TYPE as an ARGUMENT (*boost::any seems to do it somehow, but its slow as nuts). But if there isnt, I guess im gonna have to use the dreaded void pointer... If anyone has any advice/tips I would greatly appreciate it!

*Edit - The 'message system' as described at the bottom of this article pretty much sums up what I want to do...

http://www.gamedev.net/reference/articles/article1930.asp

...if there is a better way to do 'that', please let me know. Otherwise, I will be implementing a similar solution (also using void pointers).

[Edited by - ZealousEngine on May 17, 2007 5:10:01 PM]

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!