Starting in 1996, Alexa Internet has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to the Wayback Machine after an embargo period.
There are probably many of you out there who, like me, have investigated the possibilities that templates and template meta-programming can bring to your code.
An article on Dr Dobbs introduced me to typelists and some of the meta-programming techniques, with the Visitor pattern used as an example.
Unfortunately, I soon found that Visual C++ was not entirely happy with the syntax used and it took some time before I managed to get a version that didn't give the compiler indigestion! That didn't last long, though. Instantiating my first Visitor object caused the compiler to generate the dreaded "Internal compiler error. Try
simplifying your code". Well, I gave up at that point, deciding I had better things to do with my life than battling my compiler. So, I decided to try and achieve the similar objective to the Visitor example without meta-programming.
Anyone unfamiliar with the Visitor pattern should read this first.
The objective of the Dr Dobbs example was merely to remove a small amount of boilerplate code and
ensure that all handling omissions in all code would be flagged at compile time.
The use of this technique in the Visitor pattern may seem somewhat trivial, but I hope that you may find the methods used useful in more
complex situations, despite it not being as 'cool' as using typelists and meta-programming.
Ok, it's time to start.
The Classes
This is the null type. It is merely there to act as a place-holder for default template parameters. It's a template structure that is
parameterised with an integer id.
//*****************************************************************// The null type class.// For default template parameters.//*****************************************************************template <constint id>
structNull_Type
{
};
This is the class used for actual visitor template parameter types. It's a pure virtual class and requires a derived class to override the Visit function.
//*****************************************************************// The virtual visit class.// Declares a pure virtual visit function for the type.//*****************************************************************template<typename T>
class Visit_Type
{
public:
virtual Visit(T &) = 0;
};
This is a specialised class for the default null visit types. It defines a concrete non-virtual protected function. Derived classes do not need to override it.
//*****************************************************************// Specialised visit class.// Declares a non-virtual concrete visit function for null// visitors.//*****************************************************************template <constint id>
class Visit_Type<Null_Type<id> >
{
protected:
void Visit(Null_Type<id> &);
}
About the Author
I've been in the electronics business for 28 years, gradually moving over the years from pure hardware design to mostly software with hardware interface elements in a real-time environment. Over the years I've programmed in Assembler (6502, 6809, 68000), BASIC, Pascal, C & C++. I've dabbled a little in PHP/MySQL as I run a climbing club website and forum. http://www.southamptonrats.org
Projects have included DCT based image compression (pre-jpeg), remote imaging security systems, CCTV cameras, images analysis, real-time conveyor control.
I'm currently working on a template library based on the STL for images and image algorithms.
Add www.codeguru.com to your favorites Add www.codeguru.com to your browser search box IE 7 | Firefox 2.0 | Firefox 1.5.xReceive news via our XML/RSS feed
RATE THIS ARTICLE:
Excellent Very Good Average Below Average Poor
(You must be signed in to rank an article. Not a member? Click here to register)
Latest Comments:
No Comments Posted.
Add a Comment:
Title:
Comment:
Pre-Formatted:
Check this if you want the text to display with the formatting as typed (good for source code)
(You must be signed in to comment on an article. Not a member? Click here to register)