a =(a + b) - (b = a); C ++ vs php [英] a = (a + b) - (b = a); C++ vs php
问题描述
我一直在寻找,发现公式: a =(a + b) - (b = a)
它应该交换两个变量一些情况)。但是我用C ++和php测试它们,这些给我不同的结果。
php:
<
$ a = 10;
$ b = 20;
$ a =($ a + $ b) - ($ b = $ a); $ b $ d echo $ a,,$ b;
打印 20 10
C ++
int a = 10;
int b = 20;
a =(a + b) - (b = a);
std :: cout<< a<< < b;
打印 10 10
代码看起来一样,但输出不同,我一直在想两个原因:
- C++ code is compiling and php is interpreting.
- This formula is useless because it leads to undefined behavior.
$ b
有人可以解释为什么C ++和php输出在这种情况下不同?
规则是在PHP中,但是在C ++中,单个子表达式的顺序没有严格定义,或者作为技术术语,它是未指定 - 换句话说,允许编译器计算 b = a
之前或之后 a + b
。只要在减法之前它 a + b
和 b = a
使用未指定行为允许编译器在某些情况下生成更高效的代码,或者只是为某些体系结构构建一个编译器。
这也意味着如果你有一个表达式重新计算表达式中的值,并且在表达式的其他地方使用它,你会得到未定义的行为简称)。 UB意味着,行为没有定义 - 几乎任何事情都可能发生,包括你看到的和许多其他选择(例如,编译器允许产生42作为结果,即使逻辑说,答案不会42在这种情况下[这是错误的问题!])。
我也建议如果你想在PHP中交换两个值:
$ t = $ a;
$ a = $ b;
$ b = $ t;
和在C ++中:
#include< algorithm>
std :: swap(a,b);
或如果您坚持写自己的:
int t = a;
a = b;
b = t;
想要聪明,执行它没有临时变量几乎肯定会使它慢使用临时(当然是在像C ++这样的编译语言中) - 在像PHP这样的解释性语言中,创建一个新的变量可能会增加一点额外的开销,但是它不可能是那么大,相比之下, 。
I've been looking around and found formula: a = (a + b) - (b = a)
it is supposed to swap two variables (or objects in some cases). However I tested it with C++ and php, these gave me different result.
php:
$a = 10;
$b = 20;
$a = ($a + $b) - ($b = $a);
echo $a, " ", $b;
This prints 20 10
C++
int a = 10;
int b = 20;
a = (a + b) - (b = a);
std::cout << a << " " << b;
This prints 10 10
Code looks the same but outputs are different, I've been thinking about two reasons:
Can somebody explains, why C++ and php output differs in this situation?
I'm not sure what the rules are in PHP, but in C++, the order of individual sub-expressions isn't strictly defined, or as the technical term is, it is "unspecified" - in other words, the compiler is allowed to calculate b = a
before or after it does a + b
. As long as it does a + b
and b = a
before the subtraction. The use of "unspecified" behaviour allows the compiler to produce more efficient code in some cases, or simply that it's possible to build a compiler for some architectures.
It also means that if you have an expression that "recalculates" a value within the expression itself, and also using it elsewhere in the expression, you get unedefined behaviour (UB for short). UB means just that, the behaviour is not defined - almost anything could happen, including what you are seeing and many other alternatives (e.g. the compiler is allowed to produce 42 as a result as well, even if logic says the answer wouldn't be 42 in this case [it's the wrong question for that!]).
I would also suggest that if you want to swap two values, in PHP:
$t = $a;
$a = $b;
$b = $t;
and in C++:
#include <algorithm>
std::swap(a, b);
or if you insist on writing your own:
int t = a;
a = b;
b = t;
Trying to be clever and perform it "without temporary variable" is almost certainly going to make it slower than the use of a temporary - certainly in a compile language like C++ - in a interpreted language like PHP, creating a new variable may add a bit of extra overhead, but it's unlikely to be that big, compared to the extra effort in the logic required.
这篇关于a =(a + b) - (b = a); C ++ vs php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!