C 预处理器宏是否可以包含预处理器指令? [英] Is it possible for C preprocessor macros to contain preprocessor directives?

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

问题描述

我想做以下等效的事情:

I would like to do the equivalent of the following:

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

print_max(INT);

现在 #ifdef 或任何嵌套的预处理器指令是就我在函数宏中看到的而言,这是不允许的.有什么想法吗?

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

更新:所以这似乎是不可能的.即使是在运行时检查的 hack 似乎也无法实现.所以我想我会选择类似的东西:

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
", TYPE##_MAX);

print_max(INT);
print_max(BLAH);

推荐答案

Boost Preprocessor(它适用于 C 和 C++,即使 Boost 整体上是一个 C++ 库)库可以帮助完成这种任务.它不是在宏中使用 #ifdef(这是不允许的),而是帮助您多次包含一个文件,每次定义不同的宏,以便该文件可以使用 #ifdef.

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.

以下代码,如果保存到 max.c,应该对文件顶部的 MAXES #define 中列出的每个单词执行您想要的操作.但是,如果任何 _MAX 值是浮点数,它将不起作用,因为预处理器无法处理浮点数.

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 是一个方便的工具,但它并不完全简单;您可以决定这种方法是否比复制和粘贴方法有所改进.)

(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
", (long long) CURRENT_MAX);
#else
printf("The max of " BOOST_PP_STRINGIZE(CURRENT) " is undefined
");
#endif

#undef CURRENT
#undef CURRENT_MAX

#endif

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

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