C#预处理程序指令中运算符的优先级是什么? [英] What is the precedence of operators in C# Preprocessor Directives?
问题描述
如果我用C#编写的一段代码封装在#if
指令中,那么该指令中可能使用的任何布尔运算符都具有什么优先级(如果有)?
If I have a piece of code written in C# wrapped in an #if
directive, what (if any) precedence is applied to any boolean operators that might be used in that directive?
换句话说:
#if DEBUG || MYTEST && PLATFORM_WINDOWS
// ... Some code here
#endif
将其简单地从左至右评估为
Will that be simply evaluated left to right as
#if (DEBUG || MYTEST) && PLATFORM_WINDOWS
类似地,
#if PLATFORM_WINDOWS && DEBUG || MYTEST
被评估为
#if (PLATFORM_WINDOWS && DEBUG) || MYTEST
或者&& vs ||?
Or is there some precedence order for && vs ||?
明确地说,我很清楚我可以自己运行代码进行测试,而且我已经拥有了.我正在寻找一个能给我一些正式信息的答案-对文档等的引用,它可以使我对指令的基本机制有更深入的了解.我想知道是否存在特定目的的行为,或者这纯粹是未定义的行为.
To be clear, I am well aware that I can run the code myself to test it, and I have. I'm looking for an answer that gives me something official - a reference to documentation or the like, which can give me a deeper understanding of the underlying mechanics of directives. I'd like to know if there is a specifically intended behaviour or if this is purely something that is undefined.
推荐答案
2.5.2预处理表达式
预处理表达式的求值总是产生一个布尔值 价值. 预处理表达式的求值规则是 与常量表达式(§7.19)相同,只是唯一 可以引用的用户定义实体是有条件的 编译符号
Evaluation of a pre-processing expression always yields a boolean value. The rules of evaluation for a pre-processing expression are the same as those for a constant expression (§7.19), except that the only user-defined entities that can be referenced are conditional compilation symbols
7.19 常量表达式
常量表达式的编译时评估与非常量表达式的运行时评估使用相同的规则* ,不同之处在于,在运行时评估会抛出异常的情况下,编译时评估会导致发生编译时错误.
The compile-time evaluation of constant expressions uses the same rules as run-time evaluation of non-constant expressions*, except that where run-time evaluation would have thrown an exception, compile-time evaluation causes a compile-time error to occur.
因此,相同的运算符优先级适用于预处理表达式,常量表达式和运行时评估.
So the same operator precedence applies to pre-processing expressions, constant expressions and runtime evaluation.
7.3.1运算符优先级和关联性
(...)
7.11逻辑与&
7.11 Logical AND &
7.11逻辑XOR ^
7.11 Logical XOR ^
7.11逻辑或|
7.12有条件AND&
7.12 Conditional AND &&
7.12有条件的OR ||
7.12 Conditional OR ||
(...)
从最高优先级到最低优先级.
From highest to lowest precedence.
这篇关于C#预处理程序指令中运算符的优先级是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!