简体   繁体   English

在C ++中,A + = B优于A = A + B,同样地,++ A是A ++?

[英]In C++, is A+=B preferable to A=A+B in the same way ++A is to A++?

In C++, is A+=B preferable to A=A+B in the same way ++A is to A++? 在C ++中,A + = B优于A = A + B,同样地,++ A是A ++?

I understand that a "++A" preincrement is guaranteed to be at least as fast as a "A++" postincrement. 我知道“++ A”预增量至少与“A ++”后增量一样快。 This is discussed many places including here and here . 这里讨论了许多地方,包括这里这里 Likewise, A+=B is expected to be at least as fast as A=A+B, as here . 同样,预期A + = B至少与A = A + B一样快,如此处所示

I was looking at this for ++: 我在看这个++:

//From https://herbsutter.com/2013/05/13/gotw-2-solution-temporary-objects/
T T::operator++(int)() {
auto old = *this; // remember our original value
++*this;          // always implement postincr in terms of preincr
return old;       // return our original value
}

My reasoning is that in the worst case (probably from complex object types) A=A+B would have to retrieve and store A and add in B before saving it back to the original A location, while A+=B would take B and add it to A directly. 我的理由是,在最坏的情况下(可能来自复杂的对象类型)A = A + B必须检索并存储A并在将其保存回原始A位置之前添加B,而A + = B将采用B并添加直接到A。 Is my reasoning correct? 我的推理是否正确?

It is expected that basic types are rearranged as the same at compile time, and that this really only applies to complex objects requiring operator overloading. 期望基本类型在编译时重新排列为相同,并且这实际上仅适用于需要运算符重载的复杂对象。

Does this extend generically to most imperative languages? 这是否一般扩展到大多数命令式语言?

I would say the reasons aren't quite the same. 我会说原因并不完全相同。 My main reason for prefering A += B over A = A + B is conciseness. 我更喜欢A += B不是A = A + B主要原因是简洁。 A += B clearly states that you want to add B to A. This is especially true if A is a more complex expression: A += B清楚地表明您要将B添加到A.如果A是更复杂的表达式,则尤其如此:

node_capacities[std::make_pair(node1,node2)] += edge.capacity;

I've never found the performance of A += B to be worse than A = A + B either. 我从未发现A += B的性能比A = A + B差。

The big thing to remember is that C++ have operator overloading which means that te += operator can mean something completely different from what you expect. 要记住的重要一点是C ++有运算符重载 ,这意味着te +=运算符可能意味着与您期望的完全不同的东西。

The += operator only works as add and assign to if the "destination" class doesn't have an += operator defined for it. 如果 “目标”类没有为其定义+=运算符,则+=运算符仅用作添加和赋值。 Operator overloading also means that eg x++ can mean different thing depending on what the class instance of x defines for operator overloads. 运算符重载也意味着,例如x++可能意味着不同的东西,具体取决于x的类实例为运算符重载定义的内容。

You should prefer A += B; 你应该更喜欢A += B; .

If you ever wrote a type where A = A + B; 如果您曾写过A = A + B;的类型A = A + B; turned out to be better than A += B; 结果比A += B; , then you should change the implementation of operator+= to do exactly what A = A + B; 那么你应该改变operator+=的实现来做到A = A + B; does. 确实。

However, the reverse generally can't be done; 但是,反过来一般不能做; you usually can't modify operator+ in any reasonable fashion to make A = A + B; 你通常不能以任何合理的方式修改operator+以使A = A + B; do exactly the same thing as A += B; 做与A += B;完全相同的事情A += B; .

Thus, if there is a difference between A += B; 因此,如果A += B;之间存在差异A += B; and A = A + B; A = A + B; , you should expect A += B; ,你应该期望A += B; to be the better choice. 是更好的选择。

In most cases, A+=B is preferable. 在大多数情况下,A + = B是优选的。 As you have pointed out, there would be more register loads in A = A + B vs A+=B. 正如您所指出的,A = A + B与A + = B的寄存器负载会更多。 Also of interest might be to read up on SSA . 同样有趣的是阅读SSA This is used in compiler optimization, and might help understand how such case is dealt with. 这用于编译器优化,可能有助于理解如何处理这种情况。 As you have said, most of these considerations are taken out of the programmers hands thanks to the compiler, but it is good to be aware of these things. 正如您所说,由于编译器,大多数这些注意事项都是从程序员手中取出的,但是要注意这些事情是很好的。

Another consideration to account for in complex objects is the possible side effects caused by calling getters, and the fallout for calling such getter twice. 在复杂对象中考虑的另一个考虑因素是调用getter可能产生的副作用,以及调用此类getter两次的余量。 (Think, A.get() = A.get() + B.get()). (想想,A.get()= A.get()+ B.get())。 This however should be relatively rare, since getters should not have side effects in most cases. 然而,这应该是相对罕见的,因为在大多数情况下吸气剂不应该有副作用。

In fact, in C++ you should avoid postfixed operators: 实际上,在C ++中你应该避免使用后缀运算符:

  • ++a //here the operator ++ is prefixed ++ a //这里运算符++是前缀的
  • a++ //here the operator ++ is postfixed a ++ //这里的运算符++是后缀的

Just because the postfixed solution will use the value of a then increase its value, which can be dangerous some times, or at least decrease some performances. 仅仅因为后缀解决方案将使用a的值然后增加其值,这可能有些危险,或者至少降低一些性能。

You can take a look here for further informations. 您可以在这里查看更多信息。

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

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