使用包含宏扩展定义宏

Expand define macro with include macro

我正在尝试定义一个宏。这个想法是,当它扩展时,它将包含一个 header。例如:

#define function() \
                   include <CustomHeader.h>

非常感谢。

这做不到。

The resulting completely macro-replaced preprocessing token sequence is not processed as a preprocessing directive even if it resembles one, [...]

这句话来自最近的 C++ 标准草案,但在措辞上有细微的变化,几乎永远存在相同的基本思想。

正如其他人所指出的,您不能从宏中生成指令。

然而,您可以从宏中为指令生成参数:

#define INCF(F) INCF_(F)
#define INCF_(F) #F
#define BAR foo.h

#include INCF(BAR)  // same as #include "foo.h"

但是您无法删除该显式 #include,或将其插入另一行的中间,或类似的东西。