元组是由parens包围的逗号分隔列表,例如
()
(,)
(thing,)
(2,3)
如果我有
#define ISTUPLE(x) \\...
我希望将ISTUPLE(nope)
解析为0,将ISTUPLE((yep))
解析为1。
[FWIW,我有足够的_RTFM_'d。]
答案 0 :(得分:1)
可能会在预处理库中完成一些工作,但Variadic宏数据库(自发布此问题后添加到Boost)有一个现成的解决方案。在boost / vmd / is_tuple.hpp中定义的BOOST_VMD_IS_TUPLE
应该执行您正在寻找的内容:
#include <iostream>
#include <boost/vmd/is_tuple.hpp>
#if BOOST_VMD_IS_TUPLE() != 0
#error BOOST_VMD_IS_TUPLE() != 0
#endif
#if BOOST_VMD_IS_TUPLE(nope) != 0
#error BOOST_VMD_IS_TUPLE(nope) != 0
#endif
#if BOOST_VMD_IS_TUPLE((yep)) != 1
#error BOOST_VMD_IS_TUPLE((yep)) != 1
#endif
#if BOOST_VMD_IS_TUPLE(()) != 1
#error BOOST_VMD_IS_TUPLE(()) != 1
#endif
#if BOOST_VMD_IS_TUPLE((,)) != 1
#error BOOST_VMD_IS_TUPLE((,)) != 1
#endif
#if BOOST_VMD_IS_TUPLE((thing,)) != 1
#error BOOST_VMD_IS_TUPLE((thing,)) != 1
#endif
#if BOOST_VMD_IS_TUPLE((2,3)) != 1
#error BOOST_VMD_IS_TUPLE((2,3)) != 1
#endif
static_assert(BOOST_VMD_IS_TUPLE() == 0,"BOOST_VMD_IS_TUPLE() != 0");
static_assert(BOOST_VMD_IS_TUPLE(nope) == 0,"BOOST_VMD_IS_TUPLE(nope) != 0");
static_assert(BOOST_VMD_IS_TUPLE((yep)) == 1,"BOOST_VMD_IS_TUPLE((yep)) != 1");
static_assert(BOOST_VMD_IS_TUPLE(()) == 1,"BOOST_VMD_IS_TUPLE(()) != 1");
static_assert(BOOST_VMD_IS_TUPLE((,)) == 1,"BOOST_VMD_IS_TUPLE((,)) != 1");
static_assert(BOOST_VMD_IS_TUPLE((thing,)) == 1,"BOOST_VMD_IS_TUPLE((thing,)) != 1");
static_assert(BOOST_VMD_IS_TUPLE((2,3)) == 1,"BOOST_VMD_IS_TUPLE((2,3)) != 1");
int main(void)
{
using std::cout;
using std::endl;
cout << "BOOST_VMD_IS_TUPLE() == " << BOOST_VMD_IS_TUPLE() << endl;
cout << "BOOST_VMD_IS_TUPLE(nope) == " << BOOST_VMD_IS_TUPLE(nope) << endl;
cout << "BOOST_VMD_IS_TUPLE((yep)) == " << BOOST_VMD_IS_TUPLE((yep)) << endl;
cout << "BOOST_VMD_IS_TUPLE(()) == " << BOOST_VMD_IS_TUPLE(()) << endl;
cout << "BOOST_VMD_IS_TUPLE((,)) == " << BOOST_VMD_IS_TUPLE((,)) << endl;
cout << "BOOST_VMD_IS_TUPLE((thing,)) == " << BOOST_VMD_IS_TUPLE((thing,)) << endl;
cout << "BOOST_VMD_IS_TUPLE((2,3)) == " << BOOST_VMD_IS_TUPLE((2,3)) << endl;
return 0;
}
输出:
BOOST_VMD_IS_TUPLE() == 0
BOOST_VMD_IS_TUPLE(nope) == 0
BOOST_VMD_IS_TUPLE((yep)) == 1
BOOST_VMD_IS_TUPLE(()) == 1
BOOST_VMD_IS_TUPLE((,)) == 1
BOOST_VMD_IS_TUPLE((thing,)) == 1
BOOST_VMD_IS_TUPLE((2,3)) == 1