Ah, templating. A really handy technique many people forget about when writing C/C++ code, as we've been burned by the horror that is the C macro system. (Which, BTW, may well be of use here by itself)
We're using a custom templating system pretty aggressively in parts of Parrot, I've used custom built templaters to preprocess a chunk of source before cut'n'pasting it into another source file somewhere--while it's not the most elegant solution, and you usually only do it once, it takes a lot of the error-proneness out of the drugework sourcecode.