简体   繁体   English

C ++宏说明

[英]C++ macros explanation

Can somebody explain the following code please? 有人可以解释以下代码吗?

#if 1

// loop type
#define FOR_IS_FASTER 1
#define WHILE_IS_FASTER 0
// indexing type
#define PREINCREMENT_IS_FASTER 1
#define POSTINCREMENT_IS_FASTER 0

#else

// loop type
#define FOR_IS_FASTER 1
#define WHILE_IS_FASTER 0
// indexing type
#define PREINCREMENT_IS_FASTER 0
#define POSTINCREMENT_IS_FASTER 1

#endif


#if PREINCREMENT_IS_FASTER
#define ZXP(z) (*++(z))
#define ZX(z) (*(z))
#define PZ(z) (++(z))
#define ZP(z) (z)
#define ZOFF (1)
#elif POSTINCREMENT_IS_FASTER
#define ZXP(z) (*(z)++)
#define ZX(z) (*(z))
#define PZ(z) (z)
#define ZP(z) ((z)++)
#define ZOFF (0)
#endif

I can understand what the functions are doing but for example how does the pre-processor choose which ZXP will be execute if we call it later? 我可以理解这些功能在做什么,但是例如,如果以后调用它,预处理器将如何选择将执行哪个ZXP? What do the 1 and 0 stand for? 1和0代表什么?

The #if 1 triggers the first group of #define s, which set PREINCREMENT_IS_FASTER to 1. Because of this, #if PREINCREMENT_IS_FASTER triggers the first #define ZXP... . #if 1触发第一组#define ,这将PREINCREMENT_IS_FASTER设置为1。因此, #if PREINCREMENT_IS_FASTER触发第一组#define ZXP...

There is nothing exceptional about 1 and 0 in this context. 在这种情况下,1和0没有什么例外。 The #if preprocessor directive succeeds if its argument is non-zero. 如果其参数为非零,则#if预处理器指令成功。

You can switch to the alternate form by changing the #if 1 at the top of the file with #if 0 . 您可以通过将文件顶部的#if 1更改为#if 0来切换到其他形式。 (Thank you @rabidmachine for the tip.) (谢谢@rabidmachine的提示。)

我可能倾向于同意UncleBens并建议这样做,以使您不理解它,因为整个过程完全没有用。

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

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