Passing temporary struct as template argument
<p>I'm in the process of creating a vector class and am trying to figure out ways to reuse the maximum amount of code for different size vectors.
Here's a basic example:</p>

<pre><code>template&lt;typename T, unsigned int D&gt;
class Vector
{
public:
    union {
        T v[D];
        struct {
            /* T x;
             * T y;
             * T z;
             * T w;
             */
        };
    };

    Vector()
    {
        for(unsigned int i=0; i&lt;D; ++i)
            (*this)[i] = T(0);
    }
    Vector(T scalar)
    {
        for(unsigned int i=0; i&lt;D; ++i)
            (*this)[i] = scalar;
    }

    inline T operator[](int i) { return (*this).v[i]; }
};
</code></pre>

<p>I want the member variables to be publicly accessible. Ex:</p>

<pre><code>Vector&lt;float,2&gt; vec;
printf("X: %.2f, Y: %.2f\n", vec.x, vec.y);
</code></pre>

<p>What I'd like to do is something along the lines of this:</p>

<pre><code>template&lt;typename T&gt;
class Vector2 : public Vector&lt;T,2, struct { T x; T y; }&gt; {};

template&lt;typename T&gt;
class Vector3 : public Vector&lt;T,2, struct { T x; T y; T z; }&gt; {};
</code></pre>

<p>and have it override a struct in the union:</p>

<pre><code>template&lt;typename T, unsigned int D, struct C&gt;
class Vector
{
public:
    union {
        T v[D];
        // Place the passed struct here
    };
};
</code></pre>

<p>Is there any feasible way to do this? I do not want to use anything other than the standard library if possible. Thanks in advance.</p>

<p><strong>EDIT:</strong> After reading upon all of the answers, I have understood that the way I am using unions is incorrect! Thank you to @M.M for pointing this out. I have since chosen to go a different route, but I have selected the answer that best fit what I was looking for at the time. Once again, thank you for all of the welcomed responses below!</p>