[英]Is it possible to simulate the effect that place #ifdef inside another #ifdef #endif?
I was checking some import statement, and found some import statement is in the pattern like this: 我正在检查一些导入语句,发现某些导入语句的格式如下:
#ifdef A
#ifdef B
//SOME SETTINGS 1 (some include,define,ifdef...)
#else
//SOME SETTINGS 2 (some include,define,ifdef...)
#endif
#else
#ifndef B
//SOME SETTINGS 1 (some include,define,ifdef...)
#else
//SOME SETTINGS 2 (some include,define,ifdef...)
#endif
#endif
which the macro of A is just inverse the macro of B,but "//SOME SETTINGS 1" and "//SOME SETTINGS 2" need to appear twice, so I try to rewrite that: 其中A的宏与B的宏相反,但是“ // SOME SETTINGS 1”和“ // SOME SETTINGS 2”需要出现两次,所以我尝试重写一下:
#ifdef A
#ifdef B
#else
#ifndef B
#endif
//SOME SETTINGS 1 (some include,define,ifdef...)
#else
//SOME SETTINGS 2 (some include,define,ifdef...)
#endif
but it failed to compile, is there any syntax to simulate this case that "//SOME SETTINGS 1" and "//SOME SETTINGS 2" only need to appear once? 但是它无法编译,是否有任何语法可以模拟这种情况,即“ // SOME SETTINGS 1”和“ // SOME SETTINGS 2”只需要出现一次?
Your problem is this: 您的问题是这样的:
#ifdef A
#ifdef B
#else
#ifndef B
#endif
//SOME SETTINGS 1 (some include,define,ifdef...)
#else
//SOME SETTINGS 2 (some include,define,ifdef...)
#endif
You have a mismathed number of #ifdefs, elses and endifs. 您的#ifdef,else和endif数量错误。
To do what you asked for: 要执行您要求的操作:
#if (defined(A) && defined(B)) || (!defined(A) && !defined(B))
// settings 1
#else
// settings 2
#endif
This is how you generally approach these kind of problems. 这就是您通常解决这类问题的方式。 You set up a Karnaugh truth table:
您设置了一个卡诺表真值表:
A B => 1
A !B => 2
!A B => 2
!A !B => 1
Reading this table alound means you need to use settings 1 for when (A and B) are defined OR when (A is not defined and B is not defined)... hence in C: 仔细阅读此表意味着您需要使用设置1来定义(A和B)或何时(未定义A且未定义B)...因此在C中:
(A && B) || (!A && !B)
Convert that to ifdef syntax and you're good to go. 将其转换为ifdef语法,就可以了。
Yes, you can use the defined
operator (evaluates to 1
if the identifier is defined and 0
otherwise). 是的,您可以使用
defined
运算符(如果定义了标识符,则计算为1
,否则为0
)。 You basically seem to want SOME SETTING 1
if A
and B
are defined or both undefined and SOME SETTINGS 2
otherwise: 如果
A
和B
都已定义或未定义,则似乎基本上需要SOME SETTING 1
,否则,则需要SOME SETTINGS 2
:
#if defined(A) == defined(B)
//SOME SETTINGS 1 (some include,define,ifdef...)
#else
//SOME SETTINGS 2 (some include,define,ifdef...)
#endif
The direct comparision is because either you want defined(A)
and defined(B)
to be both one or both zero - and one and zero is the only alternatives for them. 直接的比较是因为您想使
defined(A)
和defined(B)
都为1或都为零-并且一和零是它们的唯一替代方案。
Note that if you want to extend this to more than two alternatives you have to be careful to use #elif
because SOME SETTINGS 1
can alter subsequent tests if you repeat #if
- #endif
groups - that's why we need to use #else
for the SOME SETTINGS 2
part. 请注意,如果您要将其扩展到两个以上的选择,则必须小心使用
#elif
因为如果重复#if
#endif
组, SOME SETTINGS 1
会更改后续测试-这就是为什么我们需要对#else
使用#else
SOME SETTINGS 2
部分。 For example: 例如:
#if COND1(A,B):
// SOME SETTINGS 1
#elif COND2(A,B):
// SOME SETTINGS 2
// etc
#else
// DEFAULT SETTINGS
#endif
You can combine the checks for definition like this. 您可以像这样组合检查定义。
#if (defined(A) && defined(B)) || (!defined(A) && !defined(B))
// SOME SETTINGS 1
#else
// SOME SETTINGS 2
#endif
You want settings 1 when A & B are defined, or niether A or B are defined. 您需要在定义A和B或同时定义A或B时设置1。 This should do the trick:
这应该可以解决问题:
#if (defined (A) && defined(B)) || (!defined (A) && !defined(B))
//SOME SETTINGS 1 (some include,define,ifdef...)
#else
//SOME SETTINGS 2 (some include,define,ifdef...)
#endif
What about this: 那这个呢:
#if (defined A && defined B) || (!defined A && !defined B)
//SOME SETTINGS 1 (some include,define,ifdef...)
#else
//SOME SETTINGS 2 (some include,define,ifdef...)
#endif
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.