我想做相同的以下内容:
#define print_max(TYPE) \ # ifdef TYPE##_MAX \ printf("%lld\n", TYPE##_MAX); \ # endif print_max(INT);
现在#ifdef
,就我在函数宏中看到的那样,不允许使用任何嵌套的预处理程序指令.有任何想法吗?
更新:所以看起来这是不可能的.即使是在运行时检查的黑客也无法实现.所以我想我会选择以下内容:
#ifndef BLAH_MAX # define BLAH_MAX 0 #endif # etc... for each type I'm interested in #define print_max(TYPE) \ if (TYPE##_MAX) \ printf("%lld\n", TYPE##_MAX); print_max(INT); print_max(BLAH);
Josh Kelley.. 13
该升压预处理器(它支持C和C++,即使升压作为一个整体是一个C++库)库可以用这种任务的帮助.它不是在宏中使用#ifdef(不允许),而是帮助您多次包含文件,每次定义不同的宏,以便文件可以使用#ifdef.
如果保存到max.c,下面的代码应该对文件顶部的MAXES #define中列出的每个单词执行所需的操作.但是,如果任何_MAX值都是浮点数,它将无法工作,因为预处理器无法处理浮点数.
(Boost处理器是一个方便的工具,但它并不简单;你可以决定这种方法是否比复制和粘贴更好.)
#define MAXES (SHRT)(INT)(LONG)(PATH)(DOESNT_EXIST) #if !BOOST_PP_IS_ITERATING /* This portion of the file (from here to #else) is the "main" file */ #include#include #include /* Define a function print_maxes that iterates over the bottom portion of this * file for each word in MAXES */ #define BOOST_PP_FILENAME_1 "max.c" #define BOOST_PP_ITERATION_LIMITS (0,BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(MAXES))) void print_maxes(void) { #include BOOST_PP_ITERATE() } int main(int argc, char *argv[]) { print_maxes(); } #else /* This portion of the file is evaluated multiple times, with * BOOST_PP_ITERATION() resolving to a different number every time */ /* Use BOOST_PP_ITERATION() to look up the current word in MAXES */ #define CURRENT BOOST_PP_SEQ_ELEM(BOOST_PP_ITERATION(), MAXES) #define CURRENT_MAX BOOST_PP_CAT(CURRENT, _MAX) #if CURRENT_MAX printf("The max of " BOOST_PP_STRINGIZE(CURRENT) " is %lld\n", (long long) CURRENT_MAX); #else printf("The max of " BOOST_PP_STRINGIZE(CURRENT) " is undefined\n"); #endif #undef CURRENT #undef CURRENT_MAX #endif
Johannes Sch.. 6
我之前尝试过.问题是#
已经保留了字符串化宏参数.它不会被解析为预处理器令牌,如#
define中的那个.
该升压预处理器(它支持C和C++,即使升压作为一个整体是一个C++库)库可以用这种任务的帮助.它不是在宏中使用#ifdef(不允许),而是帮助您多次包含文件,每次定义不同的宏,以便文件可以使用#ifdef.
如果保存到max.c,下面的代码应该对文件顶部的MAXES #define中列出的每个单词执行所需的操作.但是,如果任何_MAX值都是浮点数,它将无法工作,因为预处理器无法处理浮点数.
(Boost处理器是一个方便的工具,但它并不简单;你可以决定这种方法是否比复制和粘贴更好.)
#define MAXES (SHRT)(INT)(LONG)(PATH)(DOESNT_EXIST) #if !BOOST_PP_IS_ITERATING /* This portion of the file (from here to #else) is the "main" file */ #include#include #include /* Define a function print_maxes that iterates over the bottom portion of this * file for each word in MAXES */ #define BOOST_PP_FILENAME_1 "max.c" #define BOOST_PP_ITERATION_LIMITS (0,BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(MAXES))) void print_maxes(void) { #include BOOST_PP_ITERATE() } int main(int argc, char *argv[]) { print_maxes(); } #else /* This portion of the file is evaluated multiple times, with * BOOST_PP_ITERATION() resolving to a different number every time */ /* Use BOOST_PP_ITERATION() to look up the current word in MAXES */ #define CURRENT BOOST_PP_SEQ_ELEM(BOOST_PP_ITERATION(), MAXES) #define CURRENT_MAX BOOST_PP_CAT(CURRENT, _MAX) #if CURRENT_MAX printf("The max of " BOOST_PP_STRINGIZE(CURRENT) " is %lld\n", (long long) CURRENT_MAX); #else printf("The max of " BOOST_PP_STRINGIZE(CURRENT) " is undefined\n"); #endif #undef CURRENT #undef CURRENT_MAX #endif
我之前尝试过.问题是#
已经保留了字符串化宏参数.它不会被解析为预处理器令牌,如#
define中的那个.