Search code examples
c++c++03boost-preprocessor

macro parameter pack with boost preprocessor


I don't know there similar question, or not.

How to simplify

(a == 78 || a == 98 || a == 73 || a == 11 || a == 90 || a==103 || a==45 )

expression to

MY_CHECK(a, (78, 98, 73, 11, 90, 103, 45) )

With boost preprocessor ??
Is there any ready solution in boost preprocessor.


Solution

  • I couldn't find better than a transform followed by a fold, since BOOST_PP_SEQ_FOLD_LEFT does not carry a data parameter around. Still:

    #define MY_CHECK_FOLD_OR(s, a, b) a || b
    #define MY_CHECK_FOLD_EQ(s, var, elem) (var) == (elem)
    
    #define MY_CHECK(var, values)             \
        (BOOST_PP_SEQ_FOLD_LEFT(              \
            MY_CHECK_FOLD_OR, false,          \
            BOOST_PP_SEQ_TRANSFORM(           \
                MY_CHECK_FOLD_EQ, var,        \
                BOOST_PP_TUPLE_TO_SEQ(values) \
            )                                 \
        ))
    

    ... which expands:

    if (MY_CHECK(a, (1, 2, 3)))
    

    ... into:

    if ((false || (a) == (1) || (a) == (2) || (a) == (3)))
    

    See it live on Coliru