[英]Is it legal to use #elif with #ifdef?
A simple question that Google doesn't help me with.一个简单的问题,谷歌没有帮助我。 Is it legal in C++ to use #elif
clause in the context of #ifdef
?在 C++ 中在#ifdef
的上下文中使用#elif
子句是否合法? It seems to compile and work as expected with all the major compilers in the c++11 mode (MSVC 2015/2017, clang, GCC), but I'm not certain whether it is standard-compliant.它似乎可以在 c++11 模式(MSVC 2015/2017、clang、GCC)下与所有主要编译器一起按预期编译和工作,但我不确定它是否符合标准。
Yes, the grammar allows an #elif
after preceding, matching #if
, #ifdef
or #ifndef
: 是的,语法允许#elif
在前面,匹配#if
, #ifdef
或#ifndef
:
if-section : if-section :
if-group elif-groups opt else-group opt endif-line if-group elif-groups opt else-group opt endif-lineif-group : if-group :
# if
constant-expression new-line group opt# if
constant-expression new-line group opt
# ifdef
identifier new-line group opt# ifdef
identifier new-line group opt
# ifndef
identifier new-line group opt# ifndef
identifier new-line group opt
Note that #ifdef(X)
is just short for #if defined(X)
, and #ifndef(X)
for #if ! defined(X)
注意#ifdef(X)
只是#if defined(X)
缩写,而#ifndef(X)
是#if ! defined(X)
缩写#if ! defined(X)
#if ! defined(X)
. #if ! defined(X)
。
Yes, it's allowed. 是的,这是允许的。
The grammar is: 语法是:
if-group elif-groups opt else-group opt endif-line if-group elif-groups opt else-group opt endif-line
The definition of if-group
includes not only #if
but also #ifdef
and #ifndef
, so #ifdef ... #elif ... #endif
is fine. if-group
的定义不仅包括#if
,还包括#ifdef
和#ifndef
,所以#ifdef ... #elif ... #endif
很好。
For me, the most important point on this question is actually rosshjb 's comment under the question:对我来说,这个问题最重要的一点实际上是rosshjb在问题下的评论:
@RemyLebeau Yes, we can use #ifdef with #elif. @RemyLebeau 是的,我们可以将#ifdef 与#elif 一起使用。 But, If we #define macro with value 0 for #ifdef case, the #ifdef case tests it to true.但是,如果我们为 #ifdef 案例使用值为 0 的 #define 宏,#ifdef 案例会测试它为真。 Otherwise, if we #define macro with value 0 for #elif case, the #elif case test it to false.否则,如果我们为#elif 案例使用值为0 的#define 宏,#elif 案例将其测试为false。 – rosshjb Jan 19 '20 at 19:40 – rosshjb 20 年 1 月 19 日 19:40
So if you have a block like:所以如果你有一个像这样的块:
#ifdef __linux__
<some Linux code here>
#elif _WIN32
<some Windows code here>
#endif
Then the second test is significantly different from the first - the first is checking whether __linux__
is defined at all, where the second is checking that the symbol _WIN32
evaluates to true.然后第二个测试与第一个明显不同 - 第一个是检查__linux__
是否__linux__
定义,第二个是检查符号_WIN32
计算结果是否为真。 For many cases it will behave the same, but it is not guaranteed to do so.在许多情况下,它的行为是相同的,但不能保证这样做。
The full equivalent is actually:完整的等价物实际上是:
#ifdef __linux__
<some Linux code here>
#elif defined(_WIN32)
<some Windows code here>
#endif
Which is probably not obvious to everyone.这对每个人来说可能并不明显。
Using Kerrick SB 's answer, you can also write the same #if
statement as:使用Kerrick SB的答案,您还可以编写相同的#if
语句:
#if defined(__linux__)
<some Linux code here>
#elif defined(_WIN32)
<some Windows code here>
#endif
Which makes it more obvious that the defined
is the same for both the #if
and the #elif
这使得更明显的是, #if
和#elif
的defined
是相同的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.