I implement 5 coordinate systems (let them be called A, B, C, D, E), each with its own unique type Coordinates<System>containing coordinates. I want the C ++ compiler to correctly transform between the coordinates of different systems. Coordinate transformations form a graph: ABCD and CE. Thus, we need to specify only 8 elementary transformations, and the compiler will generate the rest:
enum CoordSys { A,B,C,D,E };
template<CoordSys System> struct Coordinates;
template<> struct Coordinates<A> {
operator Coordinates<B>() const;
};
template<> struct Coordinates<B> {
operator Coordinates<A>() const;
operator Coordinates<C>() const;
};
template<> struct Coordinates<C> {
operator Coordinates<B>() const;
operator Coordinates<D>() const;
operator Coordinates<E>() const;
};
template<> struct Coordinates<D> {
operator Coordinates<C>() const;
};
template<> struct Coordinates<E> {
operator Coordinates<C>() const;
};
The compiler will perform the conversion between the coordinates of any two systems, for example
double radiusE(Coordinates<E> const&x);
template<CoordSys Sys> double radius(Coordinates<Sys> const&x)
{ return radiusE(x); }
, // ... ( , ). Coordinates<> - ? ?