I have a vector class :
template <typename T>
class Vector2 {
public:
T x, y;
Vector2(T x, T, y) : x{x}, y{y} {}
}
And I would like get different accessors depending on the context:
Vector2<float, x, y> xy;
xy.x = 42;
Vector2<float, u, v> uv;
uv.u = 42;
Is something like that possible in C++? More specifically, is this possible in C++03?
Context
I work on an embedded firmware in C++ for a DC/DC voltage converter. I have different values such as voltages, current... Some transformations can be applied from a 3-phase current (u,v,w) into 2-phase static (a,b) and 2-phase rotating frame (d, q). It would be more readable to have specific types such as:
using CurrentDQ = Vector2<float, d, q>
using CurrentAB = Vector2<float, a, b>
using Current3 = Vector<float, u, v, w>
The closest you can do is this:
You can of course split
uandvinto separate structures, but a single big one looks more convenient to me.Then the
Elems??structures would have a uniform interface forVector<...>to interact with, e.g.:And
Vectorwould internally use onlyget_x(),get_y()to access the element.