如果我有预处理器序列,那么
FOO((TEST, (BAR,BAZ)),
(TEST2, 25, (BART, BAC)
)
有没有办法确定在第一个参数中第二个元素是元组,而在第二个参数中第二个元素是一个数字?
1st arg,2nd element =(BAR,BAZ) 第2个arg,第2个元素= 25
我正在使用boost预处理器。我知道只是标准化操作是最简单的。但是,我很好奇预处理器是否有可能确定类型并以有用的方式将该信息返回给我。
答案 0 :(得分:2)
你可以使用Boost.VMD(自Boost 1.60起可用)来达到你想要的效果。它有几个宏来检测元素的类型(BOOST_VMD_IS_TUPLE
,BOOST_VMD_IS_SEQ
,BOOST_VMD_IS_NUMBER
,...)。该库需要一个足够现代的编译器,支持可变参数宏。 Here是打印"类型"的示例。你传递的元组的第二个元素:
#include <iostream>
#include <boost/preprocessor.hpp>
#include <boost/vmd/is_tuple.hpp>
#include <boost/vmd/is_number.hpp>
#define IT_IS_A_TUPLE(TUPLE) BOOST_PP_STRINGIZE(TUPLE) " is a tuple"
#define IT_IS_NOT_A_TUPLE(ARG) BOOST_PP_STRINGIZE(ARG) " is something else"
#define IT_IS_A_NUMBER(NUMBER) BOOST_PP_STRINGIZE(NUMBER) " is a number"
#define IT_IS_NOT_A_NUMBER(ARG) BOOST_PP_IIF(BOOST_VMD_IS_TUPLE(ARG),IT_IS_A_TUPLE,IT_IS_NOT_A_TUPLE)(ARG)
#define DESCRIBE_ARGUMENT(ARG) BOOST_PP_IIF(BOOST_VMD_IS_NUMBER(ARG),IT_IS_A_NUMBER,IT_IS_NOT_A_NUMBER)(ARG)
#define FOO(TUPLE) DESCRIBE_ARGUMENT(BOOST_PP_TUPLE_ELEM(1,TUPLE))
int main()
{
std::cout << FOO((TEST, (BAR,BAZ))) << std::endl;
std::cout << FOO((TEST2, 25, (BART, BAC))) << std::endl;
}
Another example使用BOOST_VMD_GET_TYPE:
#include <iostream>
#include <boost/preprocessor.hpp>
#include <boost/vmd/get_type.hpp>
#define WHEN_ARG_IS_A_BOOST_VMD_TYPE_TUPLE(TUPLE) BOOST_PP_STRINGIZE(TUPLE) " is a tuple"
#define WHEN_ARG_IS_A_BOOST_VMD_TYPE_NUMBER(NUMBER) BOOST_PP_STRINGIZE(NUMBER) " is a number"
#define WHEN_ARG_IS_A_BOOST_VMD_TYPE_SEQ(SEQ) BOOST_PP_STRINGIZE(SEQ) " is a sequence"
#define DESCRIBE_ARGUMENT(ARG) BOOST_PP_EXPAND(BOOST_PP_CAT(WHEN_ARG_IS_A_,BOOST_VMD_GET_TYPE(ARG))(ARG))
#define FOO(TUPLE) DESCRIBE_ARGUMENT(BOOST_PP_TUPLE_ELEM(1,TUPLE))
int main()
{
std::cout << FOO((TEST, (BAR,BAZ))) << std::endl;
std::cout << FOO((TEST2, 25, (BART, BAC))) << std::endl;
std::cout << FOO((TEST3,(BAR)(BAZ),(BAR,BAZ))) << std::endl;
}