How to cast variadic arguments to a container class?
<p>I'm working on something that allows me to make it easier to call functions in a scripting language (Lua) and one idea was to use variadic arguments. There are two ways to do that:</p>

<pre><code>Call( int count, ... )
</code></pre>

<p>and</p>

<pre><code>Call( const char* args, ... )
</code></pre>

<p>like printf. I thought the first one would be a little easier to maintain, so I tried doing it by making a wrapper to manage types of arguments. It looks like this:</p>

<pre><code>class ArgWrapper
{
public:
    ArgWrapper();

    ArgWrapper( const int&amp; v ) { val.i = v; type = INT; }
    ArgWrapper( const bool&amp; v ) { val.b = v; type = BOOL; }
    ArgWrapper( const float&amp; v ) { val.f = v; type = FLOAT; }
    ArgWrapper( const double&amp; v ) { val.d = v; type = DOUBLE; }

    operator int() { return val.i; }
    operator bool() { return val.b; }
    operator float() { return val.f; }
    operator double() { return val.d; }

    enum {
        INT,
        BOOL,
        FLOAT,
        DOUBLE
    } type;

    union
    {
        int i;
        bool b;
        float f;
        double d;
    } val;
};
</code></pre>

<p>This works fine and dandy, but when I actually try to make this work in a varadic arguments construction, it turns out that the values aren't actually casted.</p>

<pre><code>void printArgs( int c, ArgWrapper... )
{
    va_list ap;
    va_start( ap, c );

    for ( int i = 0; i &lt; c; i++ )
    {
        ArgWrapper arg = va_arg( ap, ArgWrapper );
        if ( arg.type == arg.INT ) std::cout &lt;&lt; "integer - " &lt;&lt; (int)arg &lt;&lt; std::endl;
        if ( arg.type == arg.BOOL ) std::cout &lt;&lt; "boolean - " &lt;&lt; std::boolalpha &lt;&lt; (bool)arg &lt;&lt; std::endl;
        if ( arg.type == arg.FLOAT ) std::cout &lt;&lt; "float - " &lt;&lt; (float)arg &lt;&lt; std::endl;
        if ( arg.type == arg.DOUBLE ) std::cout &lt;&lt; "double - " &lt;&lt; (double)arg &lt;&lt; std::endl;
    }

    va_end( ap );
}

...

printArgs( 4, 1337.0f, 18, 37.0, true );
</code></pre>

<p>The above results in garbage, while this works perfectly fine:</p>

<pre><code>printArgs( 4, (ArgWrapper)1337.0f, (ArgWrapper)18, (ArgWrapper)37.0, (ArgWrapper)true );
</code></pre>

<p>How do I specify that each variadic argument should have the same common type?</p>
