简体   繁体   English

变量评估

[英]Evaluation of variable

What does evaluation of variable mean?变量评估是什么意思?

int main(){
   int variable;
   variable;
}

What does "variable" do? “变量”有什么作用? Why evaluation of "variable" should produce its own value?为什么对“变量”的评价要产生自己的价值?

Evaluating basically means "executing the code".评估基本上意味着“执行代码”。

In an expression, variable "loads" an int containing the value of that variable into the CPU.在表达式中, variable “加载”一个包含该变量值的int到 CPU 中。

Evaluating the statement variable;评估语句variable; "loads" that value to the CPU, which then does nothing with it.将该值“加载”到 CPU,然后它什么也不做。 (The compiler is smart enough to detect that nothing happens, and will just ignore this. If you're lucky, it'll also produce a warning letting you know that this does nothing and that's suspicious.) (编译器足够聪明,可以检测到没有发生任何事情,并且会忽略这一点。如果你幸运的话,它还会产生一个警告,让你知道这什么也没做,这是可疑的。)

Evaluating the statement int a = variable;评估语句int a = variable; "loads" that value to the CPU, and then assigns it to a new local named a .将该值“加载”到 CPU,然后将其分配给名为a的新本地。

To "evaluate an expression" means to compute its result and execute its side effects. “评估一个表达式”意味着计算它的结果并执行它的副作用。

Expression variable has no side effects.表达式variable没有副作用。 (Unlike something like x = 1 or printf("abc") ). (不像x = 1printf("abc") )。

It's result has type int (determining the type is not a part of the evaluation, because it never requires runtime computations), and the result has some arbitrary value (actually reading the value would cause undefined behavior, since the variable is uninitialized).它的结果具有int类型(确定类型不是评估的一部分,因为它从不需要运行时计算),并且结果具有一些任意值(实际上读取该值会导致未定义的行为,因为变量未初始化)。 This result also has an address, which is the address of variable (you can apply & to this expression and get that address).该结果还有一个地址,即variable的地址(您可以将&应用于此表达式并获取该地址)。

In short, variable;简而言之, variable; doesn't do anything.不做任何事情。


cppreference参考

Evaluation of each expression includes:每个表达式的评估包括:

  • value computations : calculation of the value that is returned by the expression.值计算:计算表达式返回的值。 This may involve determination of the identity of the object (glvalue evaluation, eg if the expression returns a reference to some object) or reading the value previously assigned to an object (prvalue evaluation, eg if the expression returns a number, or some other value)这可能涉及确定 object 的身份(glvalue 评估,例如,如果表达式返回对某个对象的引用)或读取先前分配给 object 的值(prvalue 评估,例如,如果表达式返回数字或其他值)
  • Initiation of side effects : access (read or write) to an object designated by a volatile glvalue, modification (writing) to an object, calling a library I/O function, or calling a function that does any of those operations. Initiation of side effects : access (read or write) to an object designated by a volatile glvalue, modification (writing) to an object, calling a library I/O function, or calling a function that does any of those operations.

In C++, as with other languages, expressions are evaluated , typically to return a singular value.在 C++ 中,与其他语言一样,对表达式求值,通常返回一个奇异值。 This happens by following the operator precedence rules to apply operators in the correct order.这是通过遵循 运算符优先级规则以正确顺序应用运算符来实现的。 1 1

In the simplest case:在最简单的情况下:

int x = 2;
int y = x + x * x;

Where the right-hand-side (relative to the = assignment operator) of the expression requires evaluation in each case.表达式的右侧(相对于=赋值运算符)在每种情况下都需要求值。 In the first statement it's easy, it's just 2 .在第一个语句中很容易,它只是2 In the second the evaluation order rules apply and you get 2 + 4 or 6 .在第二个中,评估顺序规则适用,你得到2 + 46

Evaluation is the process of applying every operator in the correct order which will yield a singular value, the result of that expression.评估是以正确的顺序应用每个运算符的过程,这将产生一个奇异值,即该表达式的结果。

Think about evaluation as similar to how you might when determining the result of a mathematical formula.将评估视为类似于确定数学公式结果时的评估方式。 Different rules apply, but the principles are the same.适用的规则不同,但原则是相同的。


1 The order arguments are evaluated may not necessarily be the order in which they're defined. 1 arguments 的评估顺序不一定是定义它们的顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM