Jump to content
  • Advertisement
Sign in to follow this  
ZealousEngine

[Solved] How to write a template function that takes 0-n arguments?

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

This has to be possible, because boost::bind() does it somehow... How can I write a template function that takes 0-n arguments? Something like this...
template < class T, class T2 > void foo( T val = 0, T2 val2 = 0 ) {}
...so you could call...
int myInt = 1; float myFloat = 2.2f;
foo();
foo( myInt );
foo( myInt, myFloat );
Thanks! [Edited by - ZealousEngine on May 16, 2007 8:01:02 PM]

Share this post


Link to post
Share on other sites
Advertisement
Yes im digging through it now (its pretty large compared to the other boost components). It looks like maybe they are defining multiple functions for all possible argument combinations..

*edit - Wow, ok, so thats why bind.hpp is so big? They just defined the same function 9 times, for each possible number of arguments?

Is that really the 'proper' way to do it?

Share this post


Link to post
Share on other sites
yes, if you look at the book "Modern C++ Design" you will see many of the advanced C++ template preprocessing tricks.

The whole thing about that level of template trick is it gives either amazingly near optimal performance or really great expressive capability, at the cost of completely crappy, pre=C++ style implementation messiness.

I love templates (the normal ones, where you write 1 class to get 2-20 typesafe versions of it - nearly pure design modeling), but when you get to specialization and such, it is exactly the opposite design goal and tradeoffs as templates. Its INCREASED development cost, for performance or overall system design gains.

Although of course, once a body of such hard to implement, hard to debug and maintain libraries exist (like boost::bind) then the abilities of the normal C++ programmer are greatly expanded, at almost no cost (compared to the way such expressiveness usually costs in more pure OO languages).

Share this post


Link to post
Share on other sites
Ok well I should have known the 'boost way' == 'the right way'.. heh.

Writing 3-4 versions wont be too bad, I dont really need to support 9 arguments. And the body of the function itself will be pretty simple.

Cool, thanks guys.

Share this post


Link to post
Share on other sites
Quote:
Ok well I should have known the 'boost way' == 'the right way'.. heh.

Writing 3-4 versions wont be too bad, I dont really need to support 9 arguments. And the body of the function itself will be pretty simple.

Cool, thanks guys.


If its only 3-4 versions then your probally better of just writting it out by hand but I thought I should point out that boost.preprocessor can be used to generate all of the overloads.

Example:

// This generates a series of overloads get_arg_n which return the n'th argument
// that was passed to them.

/*
template<typename T0, ..., typename Targ_num, ..., typename Tn>
Targ_num get_arg_arg_num(T0 t0, ..., Targ_num targ_num, ..., Tn tn)
{
return targ_num;
}
*/

/*****************************************************************************/
#define generate_get_arg_n_overloads(z, n, arg_num) \
template<BOOST_PP_ENUM_PARAMS(n, typename T)> \
BOOST_PP_CAT(T, arg_num) \
BOOST_PP_GET(get_arg_, arg_num)(BOOST_PP_ENUM_BINARY_PARAMS(n, T, t)) \
{ \
return BOOST_PP_CAT(t, arg_num); \
} \
/*****************************************************************************/

/*****************************************************************************/
#define generate_get_arg_overloads(z, n, unused) \
BOOST_PP_REPEAT_FROM_TO(n, 10, generate_get_arg_n_overloads, n) \
/*****************************************************************************/

BOOST_PP_REPEAT(10, generate_get_arg_overloads, ~)




This lets you use a macro to set the maximum number of arguments supported so the user can increase it if they need but its also harder to read.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!