Using a std::vector<char> as a variant
<p>I want a simple variant with </p>

<ul>
<li>minimal overhead</li>
<li>that can be passed to functions written in the C language</li>
</ul>

<p>so I decided to use a std::vector like this<br></p>

<pre><code>typedef std::vector&lt;char&gt; SimpleVariant;
SimpleVariant data;
</code></pre>

<p>(1) store a std::string<br></p>

<pre><code>for(std::string::iterator it = str.begin(); it != str.end(); ++it)
    {
        data.push_back( *it );
    }
    data.push_back('\0');
</code></pre>

<p>(2) store a double</p>

<pre><code>data.resize(64);
std::sprintf(&amp;data[0], "%.*g", 20, val);
</code></pre>

<p>(3) get a string<br></p>

<pre><code>std::string str = std::string( m_data.begin(), m_data.end() );
</code></pre>

<p>(4) get a double<br></p>

<pre><code>double dbl = boost::lexical_cast&lt;double&gt;(&amp;data[0]);
</code></pre>

<p>Is this a reasonable approach given my requirements? Is there lightweight variant that I can use instead of trying to reinvent the wheel? <br>
I am aware of boost::variant and boost::any they are too heavyweight for my needs</p>
