Boost logo

Boost Users :

From: Matthias Vallentin (vallentin_at_[hidden])
Date: 2008-07-21 04:45:47


On Wed, Jul 16, 2008 at 11:30:46AM -0400, Christian Henning wrote:
> Hi there, can I use push_back_a with std::vector<std::string>? I want
> something like this:
>
> std::vector< std::string > lines;
> rule<> l_entry = blanks_p >> *print_p[push_back_a( lines )] >>
> blanks_p >> eol_p;

Since your rule probably returns an iterator pair, you need to create
the std::string first. To this end, you can leverage phoenix to create a
lazy functor [1], e.g.

    struct push_back_impl
    {
        template <typename Container, typename Item>
        struct result
        {
            typedef void type;
        };

        template <typename Container, typename Item>
        void operator()(Container& c, Item const& item) const
        {
            c.push_back(item);
        }
    };

    function<push_back_impl> const push_back = push_back_impl();

Thereafter, you can use the functor as follows:

  *print_p[push_back(var(lines), construct_<std::string>(arg1, arg2))]

Another option would be to create a rule that returns a std::string and
instead of a char_t iterator pair. In this case, you have to change the
scanner context. Unfortunately, I have not yet experimented with this
option, but I assume it's a more convenient solution than the one above.

   Matthias

[1] http://www.boost.org/doc/libs/1_35_0/libs/spirit/doc/phoenix.html

-- 
Matthias Vallentin                vallentin_at_[hidden]
http://matthias.vallentin.cc                  pgp: 0x37F34C16

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