Search code examples
c++boostpreprocessor

C++ boost PP get set difference on 2 sequence?


How to get set difference on Boost PP seq by macro function? For example, I have

#define FRUIT (apple)(banana)(pear)(orange)
#define EAT (banana)(orange)

#define REST (apple)(pear)

How Could I get boost pp seq: REST?


Solution

  • It is not possible to compare strings in preprocessor. You have to create a dictionary with all possible comparisons. With the help of BOOST_PP_EQUAL the dictionary can just map the values to the range between 0 to BOOST_PP_LIMIT_MAG.

    #include <boost/preprocessor.hpp>
    
    #define FRUIT (apple)(banana)(pear)(orange)
    #define EAT (banana)(orange)
    
    // Dictionary
    #define V_apple   0
    #define V_banana  1
    #define V_pear    2
    #define V_orange  3
    #define TO_V(x)   V_##x
    
    // If elements from lists match, output '1' anything, otherwise nothing.
    #define COMPARE(r, data, elem)  \
            BOOST_PP_IF(BOOST_PP_EQUAL(TO_V(data), TO_V(elem)), 1, )
    // If any element matches EAT, output 0, otherwise 1.
    #define FILTER(s, data, elem)  \
            BOOST_PP_IS_EMPTY(BOOST_PP_SEQ_FOR_EACH(COMPARE, elem, data))
    // Just filter.
    BOOST_PP_SEQ_FILTER(FILTER, EAT, FRUIT)