Consider the following code: (Boost.Spirit 2.5.1)
qi::parse(str.begin(), str.end(), (+qi::alpha)[[](const string& s){cout << s<< '\n';}]
                                    >> (*(qi::char_(',') | qi::char_('\'')))
                                    >> qi::uint_[[](int integer){cout << integer << '\n';}]);
The [[](int integer){cout << integer << '\n';}] works, but the analogous code for +qi::alpha doesn't.
How can I correct the code?
C++0x/C++11 lambdas aren't yet supported by Boost Spirit1
Edit Apparently support improved (I tested with an older boost version earlier today). Using Boost 1_48 and g++ 4.6.1 now, the following appears to just_work. Yay!
qi::as_string[]I think will want to know about qi::as_string to get the exposed attribute as 
std::string instead of the default std::vector<CharT>:
    qi::parse(str.begin(), str.end(),
        qi::as_string [ +qi::alpha ] 
        [
            [](const std::string&){std::cout << s << '\n';}
        ]
        >> (*(qi::char_(',') | qi::char_('\'')))
        >> qi::uint_
        [
            [](int integer){std::cout << integer << '\n';}
        ]);
But in my opinion the syntax isn't very friendly. Instead, I'd prefer phoenixV2:
    qi::parse(str.begin(), str.end(),
        qi::as_string [ +qi::alpha ] [ std::cout << _1 << '\n' ]
        >> (*(qi::char_(',') | qi::char_('\'')))
        >> qi::uint_ [ std::cout << _1 << '\n' ]);
That's already a lot shorter and less cryptic syntactically. Have a look at
A somewhat contrived example showing some of these in action, PLUS passing attribute references directly into the parse function:
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <algorithm>
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;
bool reverse(std::string& value)
{
    std::reverse(value.begin(), value.end());
    return true; // false makes the parsing fail
}
int main()
{
    std::string str("sometext,123");
    auto f(str.begin()), l(str.end());
    std::string s;
    unsigned int i;
    qi::parse(f,l,
            qi::as_string [ +qi::alpha ] [ reverse ]
            >> qi::omit [ *qi::char_(",\'") ]
            >> qi::uint_,
            s, i);
    std::cout << s << std::endl;
    std::cout << i << std::endl;
}
Outputs:
txetemos
123
1 PhoenixV3 might have some support for that in 1_48 (haven't checked); You'd want to
 #define BOOST_RESULT_OF_USE_DECLTYPE
 #define BOOST_SPIRIT_USE_PHOENIX_V3
See e.g. http://boost.2283326.n4.nabble.com/Boost-Spirit-Phoenix-V3-An-Overview-td3583792.html
Your code gives me the following compiler error, skipping some bits
/usr/include/boost/spirit/home/support/action_dispatch.hpp:142:13:
error: no matching function for call to
‘action_dispatch<>::do_call(
    const main()::<lambda(const string&)>&,
    action_dispatch<>::fwd_tag<std::vector<char>>,
...
which boils down to that the argument passed to your lambda is vector<char>, not string
So, replace string with vector<char>:
(+qi::alpha)[[](const std::vector<char>& s) {
      cout << std::string(s.begin(), s.end()) << '\n';
}]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With