Types of Metaprogramming

templates (c++, haskell), macros (lisp, c, #define), runtime primitives (python's type(…) c#'s reflections, eval or exec )

Compile Time


Syntax tree aware syntax tree (or source code) generation. Macros take an AST and return another AST which is the expanded form of the original.

Text Preprocessors

Very rudimentary search and replace feature of programming languages such as C/C++. Unfortunately these languages sometimes refer to their text preprocessors as macros, which causes great confusion between true macros which are aware of ASTs, and a text preprocessor, which has no context.


Seemingly a cross between macros and a metaprogramming primatives. Templates are keywords you can use within a language which will tell the compiler to create additional compiled code. Note that unlike macros, templates do not produce source code. Rather they are instructions which allow for other code to

Run Time

Metaprogramming primatives

These are any built-in primatives which have code altering or generation abilities. This includes python's type(...) function, python metaclasses, C# Reflections, exec and eval .

The gotcha about these is that the metaprogramming is done at runtime. If you are attempting to speed up compilation time or if you will not be running the metaprogramming primatives frequently, this could be a good thing, but most of the time, it would be best to do the work once while compiling, and never again during runtime.