What's the best way to implement automatic type conversion between related types in C++?
<p>I'm implementing 5 coordinate systems (let's call them A,B,C,D,E), each with its own unique type <code>Coordinates&lt;System&gt;</code> holding the coordinates. I want the C++ compiler to correctly transform automatically between coordinates of different systems. The coordinate transformations form a graph: A-B-C-D and C-E. Thus, we need to specify only 8 elementary conversion and the compiler will generate the rest:</p>

<pre><code>enum CoordSys { A,B,C,D,E };
template&lt;CoordSys System&gt; struct Coordinates;
template&lt;&gt; struct Coordinates&lt;A&gt; {  // specialisation for system A
   // ...
   operator Coordinates&lt;B&gt;() const; // implements transformation A -&gt; B
};
template&lt;&gt; struct Coordinates&lt;B&gt; {  // specialisation for system B
   // ...
   operator Coordinates&lt;A&gt;() const; // implements transformation B -&gt; A
   operator Coordinates&lt;C&gt;() const; // implements transformation B -&gt; C
};
template&lt;&gt; struct Coordinates&lt;C&gt; {  // specialisation for system C
   // ...
   operator Coordinates&lt;B&gt;() const; // implements transformation C -&gt; B
   operator Coordinates&lt;D&gt;() const; // implements transformation C -&gt; D
   operator Coordinates&lt;E&gt;() const; // implements transformation C -&gt; E
};
template&lt;&gt; struct Coordinates&lt;D&gt; {  // specialisation for system D
   // ...
   operator Coordinates&lt;C&gt;() const; // implements transformation D -&gt; C
};
template&lt;&gt; struct Coordinates&lt;E&gt; {  // specialisation for system E
   // ...
   operator Coordinates&lt;C&gt;() const; // implements transformation E -&gt; C
};
</code></pre>

<p>The compiler will work out the conversion between coordinates of any two systems, for example</p>

<pre><code>double radiusE(Coordinates&lt;E&gt; const&amp;x); // radius is best computed from system E

template&lt;CoordSys Sys&gt; double radius(Coordinates&lt;Sys&gt; const&amp;x)
{ return radiusE(x); }       // implicit type conversion to system E
</code></pre>

<p>However, the specialisations are tedious, as the <code>// ...</code> parts may be lengthy and largely identical (it is still tedious if I inherit from a common base). Ideally, I would want to avoid the specialisations and have just one definition of the class template <code>Coordinates&lt;&gt;</code> -- how can I do that? Or is there another useful option?</p>
