[preprocessor] BOOST_PP_REPEAT with multiple "data"
Hello, I'm trying to make a macro for the following case (essentially, I have to translate a run-time decision to a compile-time one): switch(i) { case 1: my_func(arg, offsetof(my_struct, arr[1].my_field))); break; case 2: my_func(arg, offsetof(my_struct, arr[2].my_field))); break; // etc. up to 10 } If I could hard-code the whole function call into the macro, I could do something like this: #define EXPR(unused, x, text) my_func(arg, offsetof(my_struct, arr[x].my_field))); break; #define MY_MACRO(x) \ switch(x)\ {\ BOOST_PP_REPEAT(x, EXPR, _)\ } However, I'd like to pass "arg" and "my_field" as parameters to this macro. What would be the right way to do this? Thanks.
2014-05-28 21:53 GMT+08:00 Igor R
Hello,
I'm trying to make a macro for the following case (essentially, I have to translate a run-time decision to a compile-time one):
switch(i) { case 1: my_func(arg, offsetof(my_struct, arr[1].my_field))); break; case 2: my_func(arg, offsetof(my_struct, arr[2].my_field))); break; // etc. up to 10 }
If I could hard-code the whole function call into the macro, I could do something like this: #define EXPR(unused, x, text) my_func(arg, offsetof(my_struct, arr[x].my_field))); break;
#define MY_MACRO(x) \ switch(x)\ {\ BOOST_PP_REPEAT(x, EXPR, _)\ }
However, I'd like to pass "arg" and "my_field" as parameters to this macro. What would be the right way to do this?
Perhaps: #define EXPR(unused, x, data) \ case x: my_func(BOOST_PP_TUPLE_ELEM(2, 0, data) , offsetof(my_struct, arr[x].BOOST_PP_TUPLE_ELEM(2, 1, data)))); break; #define MY_MACRO(x, arg, my_field) \ switch(i)\ {\ BOOST_PP_REPEAT(x, EXPR, (arg, my_field))\ } Or if you don't insist on a PP solution, lambda may help as well: #define EXPR(unused, x, data) case x: f(x); break; #define MY_MACRO(x, arg, my_field) \ auto f = [&](int i){ my_func(arg, offsetof(my_struct, arr[i].my_field))); } \ switch(i)\ {\ BOOST_PP_REPEAT(x, EXPR, (arg, my_field))\ } (note: code not tested) HTH
2014-05-28 22:20 GMT+08:00 TONGARI J
2014-05-28 21:53 GMT+08:00 Igor R
: Hello,
I'm trying to make a macro for the following case (essentially, I have to translate a run-time decision to a compile-time one):
switch(i) { case 1: my_func(arg, offsetof(my_struct, arr[1].my_field))); break; case 2: my_func(arg, offsetof(my_struct, arr[2].my_field))); break; // etc. up to 10 }
If I could hard-code the whole function call into the macro, I could do something like this: #define EXPR(unused, x, text) my_func(arg, offsetof(my_struct, arr[x].my_field))); break;
#define MY_MACRO(x) \ switch(x)\ {\ BOOST_PP_REPEAT(x, EXPR, _)\ }
However, I'd like to pass "arg" and "my_field" as parameters to this macro. What would be the right way to do this?
Perhaps:
#define EXPR(unused, x, data) \ case x: my_func(BOOST_PP_TUPLE_ELEM(2, 0, data) , offsetof(my_struct, arr[x].BOOST_PP_TUPLE_ELEM(2, 1, data)))); break;
#define MY_MACRO(x, arg, my_field) \ switch(i)\ {\ BOOST_PP_REPEAT(x, EXPR, (arg, my_field))\ }
Or if you don't insist on a PP solution, lambda may help as well:
#define EXPR(unused, x, data) case x: f(x); break; #define MY_MACRO(x, arg, my_field) \ auto f = [&](int i){ my_func(arg, offsetof(my_struct, arr[i].my_field))); } \ switch(i)\ {\ BOOST_PP_REPEAT(x, EXPR, (arg, my_field))\ }
(note: code not tested)
I guess your example is a contrived one, since you don't even need a switch...
2014-05-28 17:30 GMT+03:00 TONGARI J
2014-05-28 22:20 GMT+08:00 TONGARI J
: 2014-05-28 21:53 GMT+08:00 Igor R
: Hello,
I'm trying to make a macro for the following case (essentially, I have to translate a run-time decision to a compile-time one):
switch(i) { case 1: my_func(arg, offsetof(my_struct, arr[1].my_field))); break; case 2: my_func(arg, offsetof(my_struct, arr[2].my_field))); break; // etc. up to 10 }
If I could hard-code the whole function call into the macro, I could do something like this: #define EXPR(unused, x, text) my_func(arg, offsetof(my_struct, arr[x].my_field))); break;
#define MY_MACRO(x) \ switch(x)\ {\ BOOST_PP_REPEAT(x, EXPR, _)\ }
However, I'd like to pass "arg" and "my_field" as parameters to this macro. What would be the right way to do this?
Perhaps:
#define EXPR(unused, x, data) \ case x: my_func(BOOST_PP_TUPLE_ELEM(2, 0, data) , offsetof(my_struct, arr[x].BOOST_PP_TUPLE_ELEM(2, 1, data)))); break;
#define MY_MACRO(x, arg, my_field) \ switch(i)\ {\ BOOST_PP_REPEAT(x, EXPR, (arg, my_field))\ }
Or if you don't insist on a PP solution, lambda may help as well:
#define EXPR(unused, x, data) case x: f(x); break; #define MY_MACRO(x, arg, my_field) \ auto f = [&](int i){ my_func(arg, offsetof(my_struct, arr[i].my_field))); } \ switch(i)\ {\ BOOST_PP_REPEAT(x, EXPR, (arg, my_field))\ }
(note: code not tested)
I guess your example is a contrived one, since you don't even need a switch...
The expression "offsetof(my_struct, arr[i].my_field))" requires compile-time "i" - that's the point, so your 2nd suggestion won't work (and certainly, my own example was wrong - I can't put "switch(x)" into the macro). I'll try to apply the 1st one. Thanks!
#define EXPR(unused, x, data) \
case x: my_func(BOOST_PP_TUPLE_ELEM(2, 0, data) , offsetof(my_struct, arr[x].BOOST_PP_TUPLE_ELEM(2, 1, data)))); break;
#define MY_MACRO(x, arg, my_field) \ switch(i)\ {\ BOOST_PP_REPEAT(x, EXPR, (arg, my_field))\ }
Great. PP_TUPLE was exactly was I needed. Thanks a lot.
participants (2)
-
Igor R
-
TONGARI J