Boost logo

Boost Users :

Subject: Re: [Boost-users] [preprocessor] BOOST_PP_REPEAT with multiple "data"
From: TONGARI J (tongari95_at_[hidden])
Date: 2014-05-28 10:20:26


2014-05-28 21:53 GMT+08:00 Igor R <boost.lists_at_[hidden]>:

> 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



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net