title: Understanding QVector append() - Why does it copy conditionally?
body: <p>Here is the implementation of QVector's append() from <a href="https://github.com/qt/qt/blob/0a2f2382541424726168804be2c90b91381608c6/src/corelib/tools/qvector.h" rel="nofollow noreferrer">github</a>:</p>
  
  <pre><code>template &lt;typename T&gt;
  void QVector&lt;T&gt;::append(const T &amp;t)
  {
      if (d-&gt;ref != 1 || d-&gt;size + 1 &gt; d-&gt;alloc) {
          const T copy(t);
          realloc(d-&gt;size, QVectorData::grow(sizeOfTypedData(), d-&gt;size + 1, sizeof(T),
                                             QTypeInfo&lt;T&gt;::isStatic));
          if (QTypeInfo&lt;T&gt;::isComplex)
              new (p-&gt;array + d-&gt;size) T(copy);
          else
              p-&gt;array[d-&gt;size] = copy;
      } else {
          if (QTypeInfo&lt;T&gt;::isComplex)
              new (p-&gt;array + d-&gt;size) T(t);
          else
              p-&gt;array[d-&gt;size] = t;
      }
      ++d-&gt;size;
  }
  </code></pre>
  
  <p>Why does it need to make a copy of <code>t</code> if the number of references to the vector != 1 or it needs to resize and why does it make a copy only for these conditions?</p>
  
  <p>A related question has been asked <a href="https://stackoverflow.com/questions/30790817/qvectorappend-what-reason-for-explicit-copying">here</a>, but in the code there a copy of <code>t</code> is always made before appending to the underlying array.</p>