[英]Is it worth replacing argument in a switch with a constant?
In my code I have 在我的代码中
switch (cd->op)
{
...
}
and I'm wondering whether I should do 我想知道我是否应该做
CalcWizConsts::eqOps thisOp = cd->op;
switch (thisOp)
{
...
}
The argument to switch
will only be evaluated once, so there is no need to store it in a temporary first. switch
的参数只会被评估一次,因此不需要先存储它。 There is no performance difference, and you don't have to worry about changing the value in one of the case
clauses either. 性能没有差别,您也不必担心在
case
子句之一中更改值。 The only reason I can think of for assigning to a variable first is to make the code more readable, if the expression is lengthy. 我可以想到的首先要分配给变量的唯一原因是,如果表达式很长,则使代码更具可读性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.