Boost logo

Boost :

From: Eric Niebler (eric_at_[hidden])
Date: 2003-11-17 21:19:24


Joel de Guzman wrote:
> If yes, then how much? If none, why bother with ETs?

(Lengthy rationale follows...)

Even if statically-bound is no faster than dynamically-bound, there are
reasons to prefer ETs anyway. Regular expressions are not *really*
strings, and they're not *really* containers. They're little programs
written in a domain-specific language. And in C++, the best way to
represent domain-specific languages is to approximate their syntax in
native C++ using expression templates, as Spirit does. This brings a
number of advantages:

- Invalid regular expressions can be detected at compile time instead of
at runtime.

- ET's are executable C++ code, and can access other C++ code and data
directly. A good example are Spirit's semantic actions. Boost.Regex and
GRETA don't have anything quite like them -- the string-based
representation places a wall between your patterns and the rest of your
program.

- Ever try to write a regular expression to match balanced, nested tags?
Since ET's are native code, they can refer to other ET's (and
themselves!) in a natural way, so you can build recursive parsers out of
them. Doing the same thing with string-based regular expressions would
be clumsy and fragile.

In short, expression templates make regular expressions first-class
citizens of C++.

However, I'm not so naive as to think that people will never have
occasion to use a string-based regex representation. Hence the design
of xpressive, which allows for both.

-- 
Eric Niebler
Boost Consulting
www.boost-consulting.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk