On moving/copying c++ object instances
<p>I'm attempting to create a type-agnostic vector in C++ that is distinguished by two things. First, it allocates the memory in the object itself, at least until a certain point, rather than maintaining the actual array of objects on the heap. Secondly, it can't use C++'s copy/assign constructors, which appears to slow down the code and is not necessary.</p>

<p>While looking through the codebase I maintain on my computer, I found a class in LLVM's codebase that pretty much perfectly describes what I'm looking for: <a href="https://gist.github.com/1335307" rel="nofollow">SmallVector.h</a>. Being relatively new to C++, I'm not entirely sure why some of the design decisions were made. For instance, why is the array allocated in terms of <code>U</code> rather than <code>T</code>? The comment gives a clue:</p>

<blockquote>
  <p>If T has a ctor or dtor, we don't want it to be automatically run, so we need to represent the space as something else.  An array of char would work great, but might not be aligned sufficiently.  Instead we use some number of union instances for the space, which guarantee maximal alignment.</p>
</blockquote>

<p><code>U</code>, of course, refers to the following union:</p>

<pre><code>union U {
    double D;
    long double LD;
    long long L;
    void *P;
} FirstEl;
</code></pre>

<p>So, I guess, here are my true questions: Why does allocating an array of <code>T</code> imply that constructors/destructors are called? Is there any way to move c++ object instances around, i.e. in and out of the vector, without calling these constructors/destructors? I guess I can just use LLVM's <code>SmallVector</code> implementation, but I hate using code without understanding it.</p>

<p>Best,
Duane</p>
