What is the priority of statements in C # preprocessor directives?

If I have a piece of code written in C # wrapped in the #if directive, what (if any) priority applies to any logical operators that can be used in this directive?

In other words:

 #if DEBUG || MYTEST && PLATFORM_WINDOWS // ... Some code here #endif 

Will it just be judged left to right as

 #if (DEBUG || MYTEST) && PLATFORM_WINDOWS 

And similarly,

 #if PLATFORM_WINDOWS && DEBUG || MYTEST 

Estimated as

 #if (PLATFORM_WINDOWS && DEBUG) || MYTEST 

Or is there some priority order for && vs ||?

Edit: To be clear, I know well that I can run the code itself to test it, and I have it. I am looking for an answer that gives me something official - a link to documentation or the like, which can give me a deeper understanding of the underlying mechanics of directives. I would like to know if there is a specific intention or if it is pure that which is undefined.

+6
source share
4 answers

2.5.2 Preprocessing Formulas

Evaluating the preprocessing expression always results in a Boolean value. Evaluation rules for preprocessing expressions: the same as for constant expressions (ยง7.19) , except that the only user-defined objects that can be referenced are conditional compilation symbols

7.19 Constant Expressions

Estimating the execution time of constants using compilation uses the same rules as evaluating the execution time of constant expressions * , except that when the evaluation of the execution time throws an exception, the compilation time evaluation causes a compile-time error.

Thus, the same operator priority applies to preprocessing expressions, constant expressions, and runtime evaluations.

7.3.1 Operator priority and associativity

(...)

7.11 Logical AND

7.11 Logical XOR ^

7.11 Logical OR |

7.12 Conditional AND & </p>

7.12 Conditional OR ||

(...)

From highest to lowest priority.

+12
source

See section 2.5.2 Formatting Preprocessing in the C # Version 5.0 Language Specification.

The specification does not talk about operator precedence, but it follows from the BNF grammar specified in this section.

  • Brackets, constants ( true , false ) and conditional characters ( PLATFORM_WINDOWS , DEBUG , etc.)
  • Unary !
  • Equality == !=
  • And &&
  • Or ||

It also says:

When specifying a preprocessing expression in a certain conditional compilation symbol, the logical value is true, and the conditional compilation symbol undefined has the value boolean false.

Evaluation of the preprocessing expression always gives a logical value. The evaluation rules for a preprocessing expression are the same as for a constant expression (ยง7.19), except that the only user-defined objects that can be referenced are conditional compilation symbols.

+7
source

Priority in preprocessor directives is the same as usual priority : && has a higher priority than || . To demonstrate this, run the following code:

 #undef A #define B #define C #if A && B || C Console.WriteLine(1); #endif #if (A && B) || C Console.WriteLine(2); #endif #if A && (B || C) Console.WriteLine(3); #endif #if B || C && A Console.WriteLine(4); #endif #if B || (C && A) Console.WriteLine(5); #endif #if (B || C) && A Console.WriteLine(6); #endif 

Output:

 1 2 4 5 

Which shows that parentheses are equivalent when they are around && , and not two elements on the left.

+2
source

I believe that the && operator has a higher priority than || The operator in C #, as shown here, is http://msdn.microsoft.com/en-us/library/aa691323 (v = vs .71) .aspx .

So your code will be appreciated:

 #if DEBUG || MYTEST && PLATFORM_WINDOWS // ... Some code here #endif 

will be rated as

 #if (MYTEST && PLATFORM_WINDOWS) || DEBUG // ... Some code here #endif 
0
source

Source: https://habr.com/ru/post/971115/


All Articles