简体   繁体   English

C ++中的宏可以定义宏吗?

[英]Can macros in C++ define macros?

I was wondering if it was possible to define a macro in C++ that defines another macro that can be used in later code. 我想知道是否有可能在C ++中定义一个宏,该宏定义另一个可以在以后的代码中使用的宏。 Is this possible, or is the preprocessor used by g++ too limited for this? 这可能吗,还是g ++所使用的预处理器对此是否过于局限?

不可以,您不能在另一个宏的扩展中定义宏。

不,您不能将宏定义为宏。

You can do something like this, its not exactly what you are looking for, but it might help. 您可以执行类似的操作,虽然它并不是您想要的,但可能会有所帮助。

#ifdef ENABLE_MACRO_1
#define PRINT_MACRO(varName)   \
        std::cout<<varName<<std::endl;
#else
#define PRINT_MACRO(varName)   \
        //do nothing
#endif

So you can define a macro depending on another preprecursor condition which was defined defined. 因此,您可以根据定义的另一个前提条件定义宏。

The preprocessor makes only one pass over the source code, so this is not possible. 预处理器仅对源代码进行一次传递,因此这是不可能的。 However, you could use an external tool to perform some preprocessing ahead of compilation, like m4 . 但是,您可以使用外部工具在编译之前执行一些预处理,例如m4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM