Is metaprogramming useful?

Don Clugston dac at nospam.com.au
Mon Nov 27 07:14:34 PST 2006


Frank Benoit (keinfarbton) wrote:
> In generic programming we can do wonderful things like container classes
> and template functions.
> 
> Using more advanced feature will result in metaprogramming, like the
> compile time regex.
> 
> I wonder if compile time regex is really a thing of practical use.
> 
> If I a lot of compile time regex pattern, it is very nice to have
> compiler error for invalid patterns.
> 
> But disadvantages are:
> - compile time is increasing fast
> - code size is increasing with every new pattern (is it?).

Not necessarily. With the compile-time regex I'm working in, all it does 
is convert one human-readable string literal (eg, "ab+") into a 
byte-code string literal. There's no code generation. Executable code 
size should decrease very slightly, because the regex compilation code 
isn't included, and there's a bit less memory manipulation.
However, the size of the obj file does increase. If 'early discard' was 
implemented for templates*, compilation speed would be extremely fast, 
and there'd be no effect on obj file size.

* 'early discard' = if a template contains only const values, evaluate 
it, then immediately delete it from the symbol table. Apply this 
recursively.

> With a parser like Spirit in C++ it would be the same. In the Spirit
> FAQ, a 78 rule parser was reported with 2 hours compile time.
> 
> Well, D might be faster, but it shows that the compile time can increase
> very fast.

AFAIK, D is fast because import system is so much faster than the 
#include system, and the name lookup rules are simpler. It's like the 
difference between quicksort and bubblesort; as the complexity 
increases, the advantage of D becomes greater and greater.

> In both cases an external regex or parser generator would make more sense.
> 
> Now my questions:
> 1.) Is metaprogramming really useful or only kind of hype?
> 2.) Are there examples where a metaprogramming solution is the best way
> to solve it?

I hope to provide some <g>.



More information about the Digitalmars-d mailing list