Search code examples
c++boost-preprocessor

BOOST_PP_TUPLE_SIZE() and empty tuple '()'


I have sequence of tuples: '(int,double)(char)()'

I need to detect tuple is empty or not. But BOOST_PP_TUPLE_SIZE() returns 1 even if tuple is empty.

Tell me please, how can I check that a tuple does not contain the elements?

Edit1 Example:

#include <boost/preprocessor/tuple/size.hpp>

#define tuple0 ()
#define tuple1 (1)
#define tuple2 (1,2)

0:BOOST_PP_TUPLE_SIZE(tuple0) // -> 1
1:BOOST_PP_TUPLE_SIZE(tuple1) // -> 1
2:BOOST_PP_TUPLE_SIZE(tuple2) // -> 2

Solution

  • Solution based on the: http://gustedt.wordpress.com/2010/06/08/detect-empty-macro-arguments/

    #include <boost/preprocessor.hpp>
    
    /***************************************************************************/
    
    #define _ARG16(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, ...) _15
    #define HAS_COMMA(...) _ARG16(__VA_ARGS__, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0)
    #define _TRIGGER_PARENTHESIS_(...) ,
    
    #define ISEMPTY(...) \
        _ISEMPTY( \
            /* test if there is just one argument, eventually an empty one */ \
            HAS_COMMA(__VA_ARGS__), \
            /* test if _TRIGGER_PARENTHESIS_ together with the argument adds a comma */ \
            HAS_COMMA(_TRIGGER_PARENTHESIS_ __VA_ARGS__),                 \
            /* test if the argument together with a parenthesis adds a comma */ \
            HAS_COMMA(__VA_ARGS__ (/*empty*/)), \
            /* test if placing it between _TRIGGER_PARENTHESIS_ and the parenthesis adds a comma */ \
            HAS_COMMA(_TRIGGER_PARENTHESIS_ __VA_ARGS__ (/*empty*/)) \
        )
    
    #define PASTE5(_0, _1, _2, _3, _4) _0 ## _1 ## _2 ## _3 ## _4
    #define _ISEMPTY(_0, _1, _2, _3) HAS_COMMA(PASTE5(_IS_EMPTY_CASE_, _0, _1, _2, _3))
    #define _IS_EMPTY_CASE_0001 ,
    
    #define TUPLE_TO_ARGS(...)  __VA_ARGS__
    #define PRINT_TUPLE( TUPLE_NAME ) TUPLE_TO_ARGS TUPLE_NAME
    
    /***************************************************************************/
    
    #define tuple0 ()
    #define tuple1 (1)
    #define tuple2 (1,2)
    #define tuple3 (1,2,3)
    #define tuple4 (1,2,3,4)
    #define tuple5 (1,2,3,4,5)
    #define tuple6 (1,2,3,4,5,6)
    #define tuple7 (1,2,3,4,5,6,7)
    #define tuple8 (1,2,3,4,5,6,7,8)
    #define tuple9 (1,2,3,4,5,6,7,8,9)
    
    /***************************************************************************/
    
    int main() {
        0:ISEMPTY(PRINT_TUPLE(tuple0))
        1:ISEMPTY(PRINT_TUPLE(tuple1))
        2:ISEMPTY(PRINT_TUPLE(tuple2))
        3:ISEMPTY(PRINT_TUPLE(tuple3))
        4:ISEMPTY(PRINT_TUPLE(tuple4))
        5:ISEMPTY(PRINT_TUPLE(tuple5))
        6:ISEMPTY(PRINT_TUPLE(tuple6))
        7:ISEMPTY(PRINT_TUPLE(tuple7))
        8:ISEMPTY(PRINT_TUPLE(tuple8))
        9:ISEMPTY(PRINT_TUPLE(tuple9))
    }
    
    /***************************************************************************/
    

    Result:

    int main() {
     0:1
     1:0
     2:0
     3:0
     4:0
     5:0
     6:0
     7:0
     8:0
     9:0
    }