Automate call with n template parameters
Is there any way to automate this, to support from 0 to n parameters?
template
Hi Kevin!
On Mon, Jul 21, 2008 at 9:03 PM, Kevin Jenkins
Is there any way to automate this, to support from 0 to n parameters?
template
bool DoIt(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
Boost.Preprocessor would do that.
Create two files, which I'll call:
main.hpp
implementation.hpp
In main.hpp:
#include
Thanks! Jeremy Pack wrote:
Hi Kevin!
On Mon, Jul 21, 2008 at 9:03 PM, Kevin Jenkins
mailto:gameprogrammer@rakkar.org> wrote: Is there any way to automate this, to support from 0 to n parameters?
template
bool DoIt(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) { Boost.Preprocessor would do that.
Create two files, which I'll call:
main.hpp implementation.hpp
In main.hpp:
#include
#include #define NUM_FUNCTION_PARAMS 8 // Or however many you need.
// Define the function for 1 to 8 arguments. #define BOOST_PP_ITERATION_LIMITS(1, NUM_FUNCTION_PARAMS); #define BOOST_PP_FILENAME_1 "implementation.hpp" #include BOOST_PP_ITERATE()
And then in implementation.hpp:
// Note: If you want to support 0 arguments, you'll need to use BOOST_PP_IF // to only optionally include the template declaration. I'd recommend just putting the // argless version into main.hpp, unless the function is quite complex. template
inline void MyFunc(BOST_PP_ENUM_BINARY_PARAMS(N, Param, p)) { #define DO_THAT(n) DoThat (BOOST_PP_CAT(p, n)); BOOST_PP_REPEAT(N, DO_THAT,) #undef DO_THAT } Hope that helps - and can't promise it will immediately compile!
I do this in the proposed Extension and Reflection libraries a few times. The best example is probably in the adaptable_factory class, where I wanted to support calling a factory function without knowing its arguments:
http://svn.boost.org/trac/boost/browser/sandbox/boost/extension/impl/adaptab... http://svn.boost.org/trac/boost/browser/sandbox/boost/extension/adaptable_fa...
Let me know if you have problems getting your function to compile. Also, with Boost.Preprocessor, "gcc -E" is your best friend.
Good luck!
Jeremy Pack http://boost-extension.blogspot.com /boost-users
Kevin Jenkins wrote:
Is there any way to automate this, to support from 0 to n parameters?
template
bool DoIt(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) { DoThat<P1>(p1); DoThat<P2>(p2); DoThat<P3>(p3); DoThat<P4>(p4); DoThat<P5>(p5); }
I think you can do it using Boost.Fusion or Boost.Preprocessor. For boost.fusion, I think you can you define the function to take a ::boost::fusion::vector, and use for_each to call a polymorphic function that calls DoThat. Then you make an unfused function from that. For boost.Preprocessor, you can generate a signature using macros like BOOST_PP_ENUM_PARAMS / ENUM_BINARY_PARAMS / REPEAT . --John
participants (3)
-
Jeremy Pack
-
John C. Femiani
-
Kevin Jenkins