Is metaprogramming useful?

Sean Kelly sean at f4.ca
Mon Nov 27 06:07:49 PST 2006


Frank Benoit (keinfarbton) wrote:
>
> 1.) Is metaprogramming really useful or only kind of hype?

In-language code generation has an advantage over using a standalone 
preprocessor to perform the same task.  And generated code has the 
potential to contain fewer bugs than hand-written code, as well as 
reduce coding time for a project.  Finally, template code in general 
combined with common optimizer techniques can result in extremely fast 
code because it is essentially equivalent to hand-optimized code in many 
cases.  How fast?  Bjarne did a presentation at SDWest regarding some 
real-world applications where C++ was shown to outperform hand-tuned C 
and even FORTRAN by an order of magnitude for numeric calculations, and 
the reason was entirely attributed to the extensive use of template 
code.  Granted, metaprogramming is but a subset of template programming, 
but sometimes the line between the two is fairly blurry.

> 2.) Are there examples where a metaprogramming solution is the best way
> to solve it?

Blitz++ uses metaprogramming to gain a measurable speed increase in 
executable code.  But I think the real power of metaprogramming is 
contingent upon how well it integrates with the run-time language.  In 
C++, this integration is really fairly poor.  In D it's much better, but 
could still be improved.  The ideal situation would be if the syntax of 
a normal function call could be optimized using metaprogramming if some 
of the arguments were constants, rather than requiring a special syntax 
to do so.  Two fairly decent examples of metaprogramming in C++ are 
expression templates and lambda functions (Boost).


Sean



More information about the Digitalmars-d mailing list