|
Boost : |
From: Douglas Gregor (gregod_at_[hidden])
Date: 2002-02-22 10:24:06
On Friday 22 February 2002 08:04 am, you wrote:
> I have to disagree with this. 'auto' is a nice feature that definitely gets
> publicity but it's _not_ needed for writing libraries (except for the
> return type autodeduction which is solved by typeof.)
I would put auto in the 'needed for writing libraries' category because it
allows libraries to be moved further into the metaprogramming domain. My best
example here is the Spirit parser. The only reason that Spirit parsers are a
bit pokey now is because we have to declare a type for a Spirit rule, as in
the following grammar:
rule<> integer;
rule<> group;
rule<> expr1;
rule<> expr2;
rule<> expr;
integer = lexeme[ !(ch_p('+') | '-') >> +digit ];
group = '(' >> expr >> ')';
expr1 = integer | group;
expr2 = expr1 >> *(('*' >> expr1) | ('/' >> expr1));
expr = expr2 >> *(('+' >> expr2) | ('-' >> expr2));
However, if we had 'auto', Spirit could put _everything_ in a giant
expression template, and therefore not require the expensive redirections
through virtual functions that it does now. I'm going to invent a little
syntax here to make this work, but essentially we could have:
struct Expr {};
auto integer = lexeme[ !(ch_p('+') | '-') >> +digit ];
auto group = '(' >> rule<Expr>() >> ')';
auto expr1 = integer | group;
auto expr2 = expr1 >> *(('*' >> expr1) | ('/' >> expr1));
auto expr = rulename<Expr>(expr2 >> *(('+' >> expr2) | ('-' >> expr2)));
The rule<Foo>() construct references a rule that has not yet been defined but
will have the static name 'Foo'. rulename<Foo>(my_rule) gives 'my_rule' the
static name 'Foo'. This allows recursion statically.
The overall result: this entire grammar can be stored as an expression
template, and with a little nifty metaprogramming Spirit can produce very
efficient parsers for static (parts of) languages.
Doug
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk