Both native and array names for N-dim vector in C++

To avoid confusion hereinafter in the text with the "vector" I mean 2D, 3D, 4D, etc. vector as used in graphics, not a just vector of the numbers from std library.

I want to have a vector class that could be easily extended to an arbitrary number of dimensions (set at compile time) and at the same time the primary indexes should be accessible trough the regular x,y,z,w notation.

One important reservation: I need to have all its data members as public (as in most of vector implementations) to simplify access in the code. So, the advice to use access methods doesn't work as well as the "advice" to have all data private.

I implemented this in the following way:

template <typename T, size_t dim>
struct vec_base {
    union {
        T d[dim];
        struct {
        };
    };
};

template <typename T>
struct vec_base<T,1> {
    union {
        T d[1];
        struct {
            T x;
        };
    };
};

template <typename T>
struct vec_base<T, 2> {
    union {
        T d[2];
        struct {
            T x,y;
        };
    };
};

template <typename T>
struct vec_base<T, 3> {
    union {
        T d[3];
        struct {
            T x, y, z;
        };
    };
};

template <typename T, size_t dim>
struct vec : public vec_base<T,dim> {
    using vec_base<T, dim>::d;
   // Implementation with may methods like add, sub, mul, length, dot product, cross product, etc.
};

The drawback of the approach is this crazy number of template specializations and some issues with the debugging in IDE.

Is there a better and shorter approach to achieve the same goal? Maybe a reference to the code which implements something close to this?

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum