Policy Based Design Using C++ Typemaps
C++ Meta-programming
Imagine those days of machine level programming, when you had to give the physical address of every used variable. Adding, removing variable, or changing its size required substantial code rewriting. Similarly C++ meta-programming seems in that early stage. Many things have to be done manually, the code becomes very verbose, similar code has to be written several times, little change of design requires rewriting of large amount of code, etc. I would expect the future languages to move towards extending meta-programming capabilities.
Think about programming language that supports only switch statement. The program code would consist of selection statements and recursive calls. Imagine how awkward those programs would look like. C++ meta-programming code looks not much less awkward.
The equivalent of switch statement in meta-program is partial specialization. The recursive type definition serves as equivalent of recursive calls in procedural language. These two constructions are enough to implement virtually any logic, but of course at high cost.
In purpose to develop a better policy based designed, I'm going to describe an associative container - typemap. Look at typemap as meta-std::map, where the key and the value parts refer to C++ types. There are several designs already available, for example [Boost] MPL library. You can use mpl::map for the same purposes as the proposed typemap. Still sometimes it is useful to reimplement the existing design to adjust it better for the particular use. Typemap may be less generic, but designed as one module, located in one file. The shown implementation does not use C macro preprocessor at all. Reimplementation may take less time, than digging somebody else's meta-code, if something goes wrong. In many cases the ability to use meta-programming techniques is more important than actually having ready meta-libraries. Just like years ago, before C/C++ libraries become standardized, every programmer had own libraries of primitive routines.
Copyright (C) 2005,2006,2013 Andre Mirzoyan