Does anybody know of any libraries that use design patterns that are implemented using compile-time techniques e.g. template metaprogramming? I know that Loki implements a few but I need to find other libraries.
Boost.Spirit is a pretty big one.
It depends on what design pattern you are interested in. There are some like "Active Object" and Dispose that would have a hard time being implemented at compile time.
"interpreter" pattern -> boost.ublas and blitz++ both use "expression templates"
"bridge" pattern -> Every standard container takes an "allocator" argument (most of Loki is bridge patterns as well)
"strategy" pattern -> STL template functions choose the best implementation based on the argument types
The only difference in all of these is that the evaluation of the pattern happens when the compiler runs, rather then when the executable runs. So all you need is to adjust your thinking slightly: The templates are the program, and the "C++ compiler" runs and interprets this program. The output of this template program is an object file ready for linking. In other words, your template code's runtime is precisley when your compiler is running. C++ templates are a turing complete functional language, just like lisp or XSLT.
In fact the very first template metaprogram in 1993 had as its output not an executable, but a series of compiler errors that printed the fibonacii sequence or something like that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With