是否有可能对C preprocessor宏包含preprocessor指令? [英] Is it possible for C preprocessor macros to contain preprocessor directives?

查看:178
本文介绍了是否有可能对C preprocessor宏包含preprocessor指令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我愿做以下的等效的:

 的#define print_max(TYPE)\\
#IFDEF TYPE ## _ MAX \\
     的printf(%LLD \\ n,输入## _ MAX); \\
# 万一print_max(INT);

现在的 #IFDEF 或任何嵌套preprocessor指令是
我可以在一个函数的宏不见远不允许。
任何想法?

更新:所以看起来这是不可能的。即使黑客来检查在运行时似乎无法实现。所以,我想我会喜欢的东西去:

 的#ifndef BLAH_MAX
#定义BLAH_MAX 0
#万一
#等等......每种类型的我很感兴趣#定义print_max(TYPE)\\
    如果(TYPE ## _ MAX)\\
       的printf(%LLD \\ n,输入## _ MAX);print_max(INT);
print_max(BLAH);


解决方案

升压preprocessor ​​(这适用于C和C ++,即使升压作为一个整体是一个C ++库)库可以用这样的任务有所帮助。代替使用的#ifdef一个宏内(这是不允许的)的,它可以帮助你包括一个文件多次,每次用定义的不同的宏,以使该文件可以使用的#ifdef

以下code,如果保存到max.c,应该做你想做的每一个在该文件的顶部的马克塞斯的#define列出的单词。但是,如果任何_MAX值的浮点运算,因为preprocessor不能处理浮点它不会工作。

(加速处理器是一个方便的工具,但它不完全明了。你可以决定这种做法是否是在复制和粘贴的改进版本)

 的#define马克塞斯(SHRT)(INT)(LONG)(PATH)(DOESNT_EXIST)#如果!BOOST_PP_IS_ITERATING/ *此文件(从这里#ELSE)部分是主的文件* /#包括LT&;&values​​.h GT;
#包括LT&;&stdio.h中GT;
#包括LT&;升压/ preprocessor.hpp>/ *定义一个函数print_maxes,超过这个底部迭代
 *文件马克塞斯中的每个字* /
#定义BOOST_PP_FILENAME_1max.c
的#define BOOST_PP_ITERATION_LIMITS(0,BOOST_PP_DEC(BOOST_PP_SEQ_SIZE(马克塞斯)))
无效print_maxes(无效){
的#include BOOST_PP_ITERATE()
}INT主(INT ARGC,CHAR *的argv [])
{
    print_maxes();
}#其他/ *该文件的该部分被评估多次,
 * BOOST_PP_ITERATION()解析为一个不同的数字,每次* // *使用BOOST_PP_ITERATION()来查找当前单词在马克塞斯* /
当前的#define BOOST_PP_SEQ_ELEM(BOOST_PP_ITERATION(),马克塞斯)
#定义CURRENT_MAX BOOST_PP_CAT(电流,_MAX)#如果CURRENT_MAX
的printf(BOOST_PP_STRINGIZE(当前)的最大是用%lld \\ n,(加长)CURRENT_MAX);
#其他
的printf(BOOST_PP_STRINGIZE(当前)的最大是不确定的\\ n);
#万一和#undef CURRENT
和#undef CURRENT_MAX#万一

I would like to do the equivalent of the following:

#define print_max(TYPE) \
#  ifdef TYPE##_MAX \
     printf("%lld\n", TYPE##_MAX); \
#  endif

print_max(INT);

Now the #ifdef or any nested preprocessor directive is not allowed as far as I can see in a function macro. Any ideas?

Update: So it seems like this is not possible. Even a hack to check at runtime seems unachievable. So I think I'll go with something like:

#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);

解决方案

The Boost Preprocessor (which works for C as well as C++, even though Boost as a whole is a C++ library) library can help with this kind of task. Instead of using an #ifdef within a macro (which isn't permitted), it helps you include a file multiple times, with different macros defined each time, so that the file can use #ifdef.

The following code, if saved to max.c, should do what you want for each of the words listed in the MAXES #define at the top of the file. However, it won't work if any of the _MAX values are floating point, since the preprocessor can't handle floating point.

(Boost Processor is a handy tool, but it's not exactly straightforward; you can decide whether or not this approach is an improvement over copy-and-paste.)

#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 <values.h>
#include <stdio.h>
#include <boost/preprocessor.hpp>

/* 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

这篇关于是否有可能对C preprocessor宏包含preprocessor指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆