Re: C++ Frequently Questioned Answers
Jerry Coffin wrote:
In article <87prygcmc6.fsf@grogan.peloton>, dave@boost-consulting.com
says...
[ ... ]
As far as I can tell, you just violently agreed with at least part of
what Jerry said. All of those applications use calculations that
"involve the essence of the program" in exactly the way he referred
to. After all, metaprogramming (template or otherwise) is just
programmatic manipulation of program elements.
While I agree, I would say that Andrei's post about logarithm tables is
more or less a counterexample. It's "less" only because I this case I'm
pretty sure that generating the tables at runtime is more efficient than
reading them from disk.
Nonetheless, if the tables involved were sufficiently small and
sufficiently complex to compute that it was faster to read them from
disk than re-generate them, it would make sense to generate them at
compile time.
It seems to me that the first place to look for uses for metaprogramming
would areas in which we've classically used other sorts of code
generators. The single most obvious case of that would undoubtedly be
lex/yacc and similar tools -- but you know at least as well as I do that
work is already well under way to meet that challenge (i.e.
Spirit/Spirit2).
Another point that might be interesting would be simple compile-time
code generation -- i.e. write a program that manipulates strings to
produce source code that would compiled as part of the program. This
would be limited compared to (many) interpreted languages that allow
arbitrary strings to be interpreted as source code, but considerably
more flexible than what's currently possible with macros and such.
There's plenty of things that can be done, and lex/yacc are only the
beginning:
* Perl's black hole and white hole classes
(http://cpan.uwinnipeg.ca/htdocs/Class-WhiteHole/Class/WhiteHole.html
http://cpan.uwinnipeg.ca/htdocs/Class-BlackHole/Class/BlackHole.html).
* Marshaling/unmarshaling
* Serialization
* Finite state automata (beyond lex)
* Records, e.g. Struct<float, "x", float, "y"> names a struct with two
float members x and y.
* Compound types (see http://citeseer.ist.psu.edu/487470.html) and
statically-checked structural casts.
* Automated memoization of functions (a la Higher-Order Perl
http://hop.perl.plover.com), i.e. write a (meta)function that, given a
pure function name F, implements the same functionality by memoizing F's
outputs.
* Aspects (e.g. take a class C and define a class C' that wraps C while
logging all calls to public methods).
* Custom, statically-checked formats (a la printf) and string
interpolation (a la Perl).
I am sure there might be many others.
Andrei
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]