Consider using the struct tag
struct tagPosition {}; struct tagDirection {}; struct tagGeneric {}; namespace detail { template <typename Tag=tagGeneric> class Vector3 {
For bonus points, there are conversion operators / constructors:
template <typename Tag=tagGeneric> class Vector3 { template <typename OtherTag> explicit Vector3(const Vector3<OtherTag>& rhs) { /* ... */ } // template <typename OtherTag> // operator Vector3<OtherTag>() const { return /* ... */ } };
If you enjoy living dangerously, you can opt out of the explicit keyword or enable the implicit conversion operator. This would have the βbenefitβ of being able to allow promiscuous operating permissions:
Position pos; Direction dir; Generic gen; dir = gen + pos; // you see why I call it 'promiscuous'?
I would recommend (instead) define explicit operators for such cases (free functions :)
Position operator+(const Position& v, const Translation& d) { }
Thus, the model of your class reflects the semantics of your classes.
C ++ 0x probably contains things to include explicit conversion operators , IIRC:
In the case of constructor conversions, you can turn off implicit conversions by declaring the constructor as explicit. Proposal N1592 extends the semantics of this keyword to all conversion operators. An explicit conversion statement will not perform an implicit conversion. Instead, the programmer will have to explicitly call it
source share