Boost.VMD(Boost 1.60以降で使用可能)を使用して、目的を達成することができます。要素の型を検出するいくつかのマクロがあります(BOOST_VMD_IS_TUPLE
、BOOST_VMD_IS_SEQ
、BOOST_VMD_IS_NUMBER
、...)。このライブラリには、可変マクロをサポートする最新のコンパイラが必要です。
#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;
}
Hereあなたが通過するタプルの第2の要素の「タイプ」を印刷する例であります