use boost preprocessor to generate T1 t1, T2 t2 ...

hi there, I would like to use boost preprocessor to generate something like this: (for n from 0 to N with n = 2 in the following example) template <typename T1, typename T2> void foo(T1 t1, T2 t2) I have tried to use BOOST_PP_ENUM_PARAMS but cannot figure out how to get both T_i and t_i changed each time. is there some other macros I can use? any help is highly appreciated. Thanks, -- View this message in context: http://boost.2283326.n4.nabble.com/use-boost-preprocessor-to-generate-T1-t1-... Sent from the Boost - Users mailing list archive at Nabble.com.

On 26 October 2012 19:41, weiqingh <weiqingh@mail.com> wrote:
hi there,
I would like to use boost preprocessor to generate something like this: (for n from 0 to N with n = 2 in the following example)
template <typename T1, typename T2> void foo(T1 t1, T2 t2)
I have tried to use BOOST_PP_ENUM_PARAMS but cannot figure out how to get both T_i and t_i changed each time. is there some other macros I can use? any help is highly appreciated.
You want BOOST_PP_ENUM_BINARY_PARAMS.

2012/10/26 Daniel James <dnljms@gmail.com>
On 26 October 2012 19:41, weiqingh <weiqingh@mail.com> wrote:
hi there,
I would like to use boost preprocessor to generate something like this: (for n from 0 to N with n = 2 in the following example)
template <typename T1, typename T2> void foo(T1 t1, T2 t2)
I have tried to use BOOST_PP_ENUM_PARAMS but cannot figure out how to get both T_i and t_i changed each time. is there some other macros I can use? any help is highly appreciated.
You want BOOST_PP_ENUM_BINARY_PARAMS.
Here's my shot at the this, *not* tested: #define FOO( n ) \ BOOST_PP_EXPR_IF( N, template < ) \ BOOST_PP_ENUM_PARAMS( n, typename T ) \ BOOST_PP_EXPR_IF( N, template < ) \ void foo( BOOST_PP_ENUM_BINARY_PARAMS( n, T, t) ) HTH, Kris

2012/10/27 Krzysztof Czainski <1czajnik@gmail.com>
2012/10/26 Daniel James <dnljms@gmail.com>
On 26 October 2012 19:41, weiqingh <weiqingh@mail.com> wrote:
hi there,
I would like to use boost preprocessor to generate something like this: (for n from 0 to N with n = 2 in the following example)
template <typename T1, typename T2> void foo(T1 t1, T2 t2)
I have tried to use BOOST_PP_ENUM_PARAMS but cannot figure out how to get both T_i and t_i changed each time. is there some other macros I can use? any help is highly appreciated.
You want BOOST_PP_ENUM_BINARY_PARAMS.
Here's my shot at the this, *not* tested:
#define FOO( n ) \
BOOST_PP_EXPR_IF( N, template < ) \ BOOST_PP_ENUM_PARAMS( n, typename T ) \
BOOST_PP_EXPR_IF( N, > ) \
void foo( BOOST_PP_ENUM_BINARY_PARAMS( n, T, t) )
HTH, Kris

On Fri, Oct 26, 2012 at 11:41 AM, weiqingh <weiqingh@mail.com> wrote:
I would like to use boost preprocessor to generate something like this: (for n from 0 to N with n = 2 in the following example)
template <typename T1, typename T2> void foo(T1 t1, T2 t2)
I have tried to use BOOST_PP_ENUM_PARAMS but cannot figure out how to get both T_i and t_i changed each time. is there some other macros I can use? any help is highly appreciated.
I haven't tried this code but something like this should work: #include <boost/preprocessor.hpp> #define TPARAM(z, m, unused) typename BOOST_PP_CAT(T, m) #define PARAM(z, m, unused) BOOST_PP_CAT(T, m) BOOST_PP_CAT(t, m) #define FOO(z, n, unused) \ BOOST_PP_EXPR_IF(n, template<) \ BOOST_PP_ENUM_ ## z(n, TPARAM, ~) \ BOOST_PP_EXPR_IF(n, >) \ void foo ( BOOST_PP_ENUM_ ## z(n, PARAM, ~) ) BOOST_PP_REPEAT(2, FOO, ~) Compile it with -E -P (GCC), /EP (MSVC), etc to so see the pp output. HTH, --Lorenzo

I would like to use boost preprocessor to generate something like this: (for n from 0 to N with n = 2 in the following example)
template <typename T1, typename T2> void foo(T1 t1, T2 t2)
I have tried to use BOOST_PP_ENUM_PARAMS but cannot figure out how to get both T_i and t_i changed each time. is there some other macros I can use? any help is highly appreciated.
Well I hope this can work for you: // This wont output a template for nullary function #define TEMPLATE_IF(n) BOOST_PP_IF(n, BOOST_PP_TUPLE_REM(1), BOOST_PP_TUPLE_EAT(1)) #define EACH(z, n, data) \ TEMPLATE_IF(n)(template<BOOST_PP_ENUM_PARAMS_Z(z, n, typename T)>) \ void foo(BOOST_PP_ENUM_BINARY_PARAMS_Z(z, n, T, t) ) BOOST_PP_REPEAT(3, EACH, ~) This will output: void foo( ) template< typename T0> void foo( T0 t0 ) template< typename T0 , typename T1> void foo( T0 t0 , T1 t1 )
participants (5)
-
Daniel James
-
Krzysztof Czainski
-
Lorenzo Caminiti
-
paul Fultz
-
weiqingh