|
Boost : |
From: Eric Niebler (eric_at_[hidden])
Date: 2007-07-07 12:49:47
Larry Evans wrote:
> http://boost-sandbox.sourceforge.net/libs/proto/doc/html/boost_proto/users_guide/expression_extension/expression_generators.html
>
> contains:
>
> we want all expressions that involve the calculator placeholders to be
> calculators
>
> which means, IIUC, that calulator expressions are built bottom up.
> IOW, to build a calculator expressions, you must start with a
> proto terminal calculator expression. However, I would like
> to start from a non-terminal expression (e.g. any binary
> or unary expression) and convert all the nodes to calculator
> expressions. What's the simplest way to do this? I've tried
> understanding trans::construct and other trans templates; however,
> I can't figure it out so far :(
>
> TIA for any clues.
If you have an expression tree, and you want to wrap each node in some
wrapper, the construct transform is what you want. For instance:
#include <boost/xpressive/proto/proto.hpp>
#include <boost/xpressive/proto/transform/construct.hpp>
using namespace boost;
using namespace proto;
template<typename Expr>
struct wrap
{
explicit wrap(Expr const &expr)
{}
};
struct WrapItUp
: transform::construct<
or_<
terminal<_>
, nary_expr<_, vararg<WrapItUp> >
>
, wrap<_>(_)
>
{};
terminal<int>::type const _1 = {1};
terminal<int>::type const _2 = {2};
int main()
{
int i = 0;
WrapItUp::call( _1 + _2, i, i );
return 0;
}
This little example works, but if the wrap struct inherits from
proto::extends, it doesn't compile because of a bug in construct<> which
causes the eager instantiation of wrap<_>. You could write your own
as_wrap transform which doesn't have that problem and use that instead
of construct.
HTH,
-- 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