Generative Programming in the large – applied meta-programming
The highlight of Day 3 of the ACCU Conference (for me at least) was Schalk Cronjé’s talk on Generative Programming. Schalk has run a sequence of sessions at past ACCU conferences and this session focussed on the real problems of applying Generative Programming. His focus on doing things with today’s tools and on practical problem solving was a welcome change. According to Schalk C++ Generative programming is a vastly untapped field.
Schalk began by noting that many engineers build components with little reuse potential. This leads to excessive time when adapting the software for another system. Sometimes when developing there may be artefacts that you can reuse or generate to save time later. You just need to think about this when starting out.
One question he is commonly asked is - Is it too difficult? – His answer is that this isn’t the issue; the problem is usually motivation. If the semantics are correct then the concepts are straightforward. An audience member noted that the organisation must value code reuse in order to implement these approaches.
As an example of creating generative library Schalk described the process of building a rule evaluator: Web and mail protocols are very different but there is an essential need to provide a custom, optimised evaluator for different protocols. I’m hoping that Schalk will write up his talk for the Code Generation Network so I’ll miss out the technical details here and focus on some of his general comments.
Schalk began by noting that Generative C++ can lead to a large number of configuration classes or meta-classes being generated – it is not uncommon to work with 50+ tag classes within a single type list. Effective integration of meta-programming, compile-time paradigms and run-time paradigms is required. Typical problems if done wrongly in C++ are code bloat or long symbolic names. In some cases you can’t always use a pure template-driven approach.
Schalk uses the Boost libraries for meta-programming as he feels this is the only practical C++ meta-programming environment and all his examples made use of Boost. In particular the Boost metaprogramming preprocessor has a number of macros to deal with the complexity of generated C++ (e.g. commas between typenames in templates). Use of Boost is also in line with Schalk’s “Principle of least surprise†– stick to what other people have been doing especially in a pretty new field.
After taking us through 5 idioms for C++ Template Metaprogramming Schalk closed his session with a number of observations:
- Generative Programming pushes C++ skills beyond the knowledge of existing practitioners. This explains the slow uptake of such techniques and also raises the question of how to recruit people with these skills?
- Compiler time reduction is a big issue – his current builds run at around 45 minutes – and what happens if you hit a compiler error after this amount of time? Some compilers also can’t deal with the complexity of some of the generated programs.
- Introducing a new technology can be political:
- Some people will have a mental block about the technology
- There is a distrust of tools
- Generative Programming teaches very good skills outside the C++ domain
Separation of generative stuff from non-generated stuff - Am I in the wrong language? (Schalk’s answer - Yes)
- The problem is that languages aren’t good enough to solve these complex problems.
- Some languages are easier to generate in than others. An audience member – not me – noted that “All languages will become Lisp eventually.â€
- Generative Programming in C++ shows requirements for future languages
- Powerful syntax
- Simplicity of expression
I also owe thanks to Schalk for mentioning Code Generation 2007 and the Code Generation Network at the start of his session.
