Simulating Templated Virtual Functions
Intro
Theoretical Implementation
See One Way Visitor Pattern for another technique related to the visitor pattern.
class applicationX
{
public:
template<class T>
virtual void print(T t) { ... }
};
C++ does not provide language support for templated virtual functions. Lets call templated virtual function functions that are virtual function and templated.
Each data type could be manually placed in the interface but this is not a templated solution but a manual one.
Using the visitor pattern it is possible to simulate templated virtual functions. Where the visitor pattern separated the data and function in two trees, with templated virtual functions the function is coupled because while the function is in the function(visitor) tree, the implementation is in the data tree as a templated function.
Implements the visitor pattern with a variation. The function (Visitor) forwards its task back to the correct member function in the data tree. Since the exact type is known it forwards to a non-virtual templated function.
I believe that I developed this from a theatrical point of view, rather than as a pattern solving an application.