Boost logo

Boost Users :

Subject: Re: [Boost-users] [karma] using attributes out of order
From: Hartmut Kaiser (hartmut.kaiser_at_[hidden])
Date: 2010-01-15 08:46:01


> > I was fairly familiar with Spirit 1, but I'm now making my first
> foray
> > into Spirit 2. I'm trying to write something with karma's attribute
> > system. Say I've got a fusion adapted struct:
> >
> > struct s { int foo; double bar; std::string baz; };
> >
> > The Spirit docs give plenty of examples like:
> >
> > int_ << double_ << string
> >
> > But I can't find any docs showing how to output the members in a
> > different order, like:
> >
> > double_ << string << int_
> >
> > Is there a recommended way to achieve this? Thanks!
>
> It is generally better to ask these questions on the Spirit mailing
> list.
>
> However, three immediate answers to that from what I can see right off
> the bat.
>
> First:
> When you specify you struct as the BOOST_FUSION_ADAPT_STRUCT or
> whatever it is called, you can specify your order in that.

That's the preferred method, indeed.

> Second:
> You can use Fusion to operate over the struct to give things a
> different order.

Look for fusion::nview, which allows to create a fusion view of another fusion sequence/view while defining the order of the elements.

> Third:
> You can use actors, unsure if this code is correct, but it is like
> this:
> double_[_2] << string[_3] << int_[_1]

This syntax is actually:

    using boost::phoenix::at_c;

        double_[_1 = at_c<1>(_val)]
    << string[_1 = at_c<2>(_val)]
    << int_[_1 = at_c<0>(_val)]

Which is much more than you ever want to write :-P (and additionally, it's much more inefficient than the 2 solutions above).

There is a forth method involving Spirit's customization points, but that's merely academic...

Regards Hartmut

---------------
Meet me at BoostCon
www.boostcon.com


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