简体   繁体   English

为什么后增量需要复制而预增量不需要

[英]Why post-increment needs to make a copy while pre-increment does not

I know this issue has been discussed several times , but I could not find a post which explains why a copy needs to be made in case of a post-increment operation. 我知道这个问题已经讨论了好几次了,但我找不到一个解释为什么在后增量操作时需要复制的帖子。

Quoting from a stackoverflow reply: 引用stackoverflow回复:

int j = i++; // j will contain i, i will be incremented.
int j = ++i; // i will be incremented, and j will contain i+1.

Which perfectly makes sense when the definition of post/pre increment is considered. 当考虑后/前增量的定义时,这是完全有意义的。 Many times when comparing the performance of pre/post increment it is said that post increment needs to make a copy, increment it and return the copy while pre-increment just increases value and does not create a copy. 很多时候,在比较前/后增量的性能时,可以说后增量需要复制,增加它并返回副本,而预增量只增加值而不创建副本。

Although performance has been compared in tens of posts, I really could not find any explanation on why a copy has to be made in the case of post-increment. 虽然已经在数十篇文章中对性能进行了比较,但我真的找不到为什么在后增量的情况下必须制作副本的任何解释。 Why doesn't it return the old value and then increments the value of the variable by one(or however way operator is overloaded), rather than creating a new object and return that one. 为什么不返回旧值,然后将变量的值增加1(或者操作符重载方式),而不是创建新对象并返回该对象。

The difference is someval++ returns what someval is before the increment and to do that you need remember what someval is with a copy. 不同之处在于someval++返回了在增量之前的someval ,为此你需要记住someval与副本的关系。 How else would you return the original value while updating it if the original value wasn't stored somewhere? 如果原始值未存储在某处,如果更新原始值,您还会如何返回原始值?

Consider pre-incrementation and post-incrementation operators as standard functions: 考虑预增量和后增量运算符作为标准函数:

// ++i
int pre_increment(int &i) {
    i = i + 1;
    return i;
}

// i++
int post_increment(int &i) {
    int original_i = i;
    i = i + 1;
    return original_i;
}

This should help you to understand why in the second case (i++), you MUST perform a copy of the value before doing the incrementation. 这应该可以帮助您理解为什么在第二种情况下(i ++),您必须在执行增量之前执行该值的副本。

It's hard for me to say how compilers can optimize the pre-increment and post-increment operators so that a copy is not necessarily made on primitive types. 我很难说出编译器如何优化预增量和后增量运算符,这样就不必对原始类型进行复制。

I can show how a copy needs to be made for user defined types for the post-increment operators. 我可以展示如何为后增量运算符的用户定义类型创建副本。

Lets' take a simplistic look at std::vector::iterator . 让我们简单地看一下std::vector::iterator

Let's say the iterator stores an index, in addition to the other things. 假设迭代器存储索引,除了其他东西。

struct iterator
{
   size_t index;

   iterator operator++(int )
   {
      iterator copy = *this;
      this->index++;
      return copy;
   }
 };

In such a case, it is not possible to return *this first and then increment the index. 在这种情况下,不可能首先返回*this ,然后递增索引。 Creating a copy, incrementing the index of this , and then returning the copy allows us to preserve the semantics of that operation. 创建一个副本,递增的指数this ,然后返回副本可以让我们保留该操作的语义。

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

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