It there a way to control existence of data members in C++ template based on template arguments?
I have vec template which represents N-dimensional vector (like 2D, 3D, 4D…). Here is the cut:
template <typename T, size_t dim>
struct vec {
union {
T d[dim];
struct {
T x, y, z, w;
};
};
};
The idea is that I can access the fields like
vec<float,3> vec3;
vec.d[0]=0.0;
vec.d[1]=1.0;
with any number of dimensions and for "classical" first four dimensions with the notation above I can use well-known aliases x,y,z,w, like here:
vec.x = 0.0
Here “x” is the alias for the d[0].
Having the direct access to the fields is key here, I don't want to have accessors notation; so methods to get access to d don't work for me.
The problem is that for vec<float,2> I don’t need z and w. Is there a way to use something (expect full template specialization) like std::conditional, std::enable_if to control existence of these members? I’d like to write in the template something like:
struct {
T x, std::enable(dim>1,y), std::enable(dim>2,z), std::enable(dim>3,w);
};
It would be nice to avoid boost and other extra libraries. STL would work fine.
Is there any control for this?