Valid
        XHTML 1.1! Valid CSS!
Created between 1980-199?   Modified 2009-04-11
Chelton Evans

proj Simulating Templated Virtual Functions home

Intro
Theoretical Implementation

See One Way Visitor Pattern for another technique related to the visitor pattern.

Intro

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.

Source

Files

Makefile
element.h
main.cpp
simtemplatedvirtualfunc.cpp
simtemplatedvirtualfunc.h
tvirtfuncPrintfunc.cpp
tvirtfuncPrintfunc.h
visitor.h
visitorprint.h
visitorprint2.h

projcompile.txt
unittestsreport.txt

Doxygen

main.cpp
Makefile
Element
ElementA
ElementB
Visitor
simtemplatedvirtualfunc
tvirtfuncPrintfunc
visitorPrint
visitorPrint2

Theoretical Implementation

Templated virtual function diagram.

Discussion

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.