I hit a small C++ grammar trick which I have never met before in my project. The problem is how to deal with the situation that two defined identifiers have the same compilation behavior. A simple solution to this is to write two same block code like this:
#ifdef identifier1 #ifdef identifier2 // do something thing #endif #endif
Doing this seems a little odd , and I finally write the code like below:
#if defined(identifier1) && defined(identifier2) // do something #endif
Similarly, if the situation is #ifndef, it can be written as below:
#if !defined(identifier1) && !defined(identifier2) // do something #endif
However the defined directive can only be used in an #if and an #elif directive, but nowhere else. The following code block shows an example like a “if…else if… else…” flow chart.
#if defined(ALICE) alice(); #elif defined(BOB) bob(); #else printerror(); #endif
The function call to alice is compiled if the identifier ALICE is defined. If the identifier BOB is defined, the function call to bob is compiled. If neither identifier is defined, the call to printerror is compiled.
nbysy,
Thanks for your correction. I have modified the entry and welcome to come here and give me some advice again.
第一段代ç ,第二个应该是 identifier2 å§ï¼Ÿ
第二段代ç ,为什么è¦ç”¨ && 呢? æ— è®ºæ˜¯ç”¨ ||还是&&, 我觉得其编译的结果都会和第一段ä¸ä¸€æ ·ã€‚
第三段代ç 的解释,应该是:
The function call to alice is compiled if the identifier ALICE is defined. If ALICE is not defined and the identifier BOB is defined, the function call to bob is compiled. If neither identifier is defined, the call to printerror is compiled.