Initialise a std::vector with data from an array of unions
<p>How might I initialise a std::vector from an array of structs, where the struct contains a union of different types. In other words, the array is used to store a number of values of a specific type, which can be int, char* etc.</p>

<p>This is my solution so far but I'm looking for a better approach:</p>

<p>The convert function returns a <code>vector&lt;int&gt;</code> if it stores <code>int</code>s or a <code>vector&lt;std::string&gt;</code> if it stores <code>char*</code>.</p>

<p>The Value type below is a struct containing a union called value. The Container class below points to a buffer of such Values.</p>

<pre><code>// union member getter
class Getter
{
public:

    void operator()(int8_t&amp; i, const Value&amp; value)
    {
        i = value.value.i;
    }

    void operator()(std::string&amp; s, const Value&amp; value)
    {
       s = std::string(value.value.s);
    }

    ...
};

template&lt;class T&gt;
std::vector&lt;T&gt; convert(Container* container)
{
    std::vector&lt;T&gt; c;
    c.reserve(container-&gt;nrOfValues);
    Getter g;
    for(int i=0;i&lt;container-&gt;nrOfValues;i++)
    {
        T value;
        g(value, container-&gt;values[i]);
        c.push_back(value);
    }
    return c;
}
</code></pre>
