c++ - BOOST_PP_TUPLE_SIZE() and empty tuple '()' -
i have sequence of tuples: '(int,double)(char)()'
i need detect tuple empty or not. boost_pp_tuple_size() returns 1 if tuple empty.
tell me please, how can check tuple not contain 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 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 1 argument, empty 1 */ \ has_comma(__va_args__), \ /* test if _trigger_parenthesis_ argument adds comma */ \ has_comma(_trigger_parenthesis_ __va_args__), \ /* test if argument parenthesis adds comma */ \ has_comma(__va_args__ (/*empty*/)), \ /* test if placing between _trigger_parenthesis_ , parenthesis adds 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 }
Comments
Post a Comment