Another option is to output from the marker template
struct Base { }; template<typename T> struct BaseOutputtable { T *getDerived() { return static_cast<T*>(this); } T const *getDerived() const { return static_cast<T const*>(this); } protected: ~BaseOutputtable() { } };
Then print them out of both
struct MyDerived : Base, BaseOutputtable<MyDerived> { };
Now you can write it as
template<typename Derived> ostream &operator<< (ostream &o, BaseOutputtable<Derived> &derived) { }
The advantage of this is its simplicity. And if Base already templated, it is even more useful (I believe this is not the case for your code).
Johannes Schaub - litb
source share