Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What libraries use design patterns implemented with compile-time metaprogramming techniques?

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.

like image 662
filinep Avatar asked Jan 23 '23 03:01

filinep


2 Answers

Boost.Spirit is a pretty big one.

like image 60
Dean Harding Avatar answered Jan 26 '23 00:01

Dean Harding


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.

like image 25
Lance Diduck Avatar answered Jan 26 '23 00:01

Lance Diduck