简体   繁体   中英

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. Is this possible, or is the preprocessor used by g++ too limited for this?

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

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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