BOOST_PP_TUPLE_SIZE()和空元组'()'

时间:2013-08-16 08:50:13

标签: c++ boost-preprocessor

我有元组序列:'(int,double)(char)()'

我需要检测元组是否为空。但是,即使元组为空,BOOST_PP_TUPLE_SIZE()也会返回1。

请告诉我,如何检查元组是否包含元素?

EDIT1 例如:

#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

2 个答案:

答案 0 :(得分:2)

基于: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))
}

/***************************************************************************/

结果:

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

答案 1 :(得分:1)

()确实被认为是一个元素元组。所以大小是1

这已被提升为ticket

在Boost 1.54.0 BOOST_PP_TUPLE_SIZE(())中,结果也是1。

你可以从here找出一些东西。

相关问题