Boost logo

Boost Users :

From: Thomas Wenisch (twenisch_at_[hidden])
Date: 2002-10-02 17:05:27


Hi Jeff, all,

It seems that part of Jeff's question is "What is meta-programming good
for?", which strikes me as a good question to ask (and I agree with all
the other responses suggesting Alexandrescu's book as a good starting
point).

But, I thought it still might be useful to suggest a list of problems that
I have solved, seen solved, or thought about solving with metaprogramming.

The "classic" example of meta-programming is using lex & yacc - programs
that themselves generate programs. The point of the MPL (and Loki) is to
be able to do this in C++ rather than depending on an external tool.

In general, what meta-programming lets you do is to do computation at
compile time instead of at runtime.

For example, you could precompute a table of values as compile time
constants instead of filling a table in at initialization time. This
might be useful in game programming, for precomputer trig functions.

Alexandrescu's book shows how you can automatically generate class
heirarchies. Or, have the compiler do the "dirty work" of implementing
double-dispatch (picking a function to call based on the runtime types of
2 parameters instead of just one. Virtual functions give you one,
metaprogramming gives you 2+ without a lot of code).

I use meta-programming to be able to switch between having parameters for
my simulation programs be hard-coded (ie compile time constants) or appear
as command line arguments by changing just one line of code.

MPL (and Loki) gives you the tools to do these kinds of compile-time
computations by giving you fundamental algorithms and data structures that
exist and can be manipulated in the compiler, but that don't actually
exist at run time.

Best Regards,
-Tom Wenisch


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net