[qi and lambda] Get wrong reaction of the qi::rule if I use lambda for the action
Hello, if I execute the following example, I get the following output "The bad one The good one a" but I only expected "The good one a". Why does it happens? Boost 1.47.0 was used. #include <boost/config/warning_disable.hpp> #include <boost/spirit/include/qi.hpp> #include <boost/lambda/lambda.hpp> #include <iostream> int main() { using boost::spirit::qi::char_; using boost::spirit::qi::parse; char const *first = "{a}", *last = first + std::strlen(first); parse(first, last, '{' >> char_("a")[std::cout << "The good one " << boost::lambda::_1 << '\n'] | char_("b")[std::cout << "The bad one " << boost::lambda::_1 << '\n'] >> '}'); return 0; }
2011/8/15 <boost_mailinglist@gmx.de>
Hello,
if I execute the following example, I get the following output "The bad one The good one a" but I only expected "The good one a".
Why does it happens? Boost 1.47.0 was used.
#include <boost/config/warning_disable.**hpp> #include <boost/spirit/include/qi.hpp> #include <boost/lambda/lambda.hpp>
#include <iostream>
int main() { using boost::spirit::qi::char_; using boost::spirit::qi::parse;
char const *first = "{a}", *last = first + std::strlen(first);
parse(first, last, '{' >> char_("a")[std::cout << "The good one " << boost::lambda::_1 << '\n'] | char_("b")[std::cout << "The bad one " << boost::lambda::_1 << '\n']
'}');
return 0; }
Because (std::cout << "The good one ") & (std::cout << "The bad one ") are not lambda expressions so they are executed immediately, and it seems Boost.Lambda just happened to work here. The corrected version would be: #include <boost/config/warning_disable.hpp> #include <boost/spirit/include/qi.hpp> #include <boost/spirit/include/phoenix.hpp> #include <iostream> int main() { using boost::spirit::qi::char_; using boost::spirit::qi::parse; using boost::spirit::qi::_1; using boost::phoenix::val; char const *first = "{a}", *last = first + std::strlen(first); parse(first, last, '{' >> char_("a")[std::cout << phx::val("The good one ") << _1 << '\n'] | char_("b")[std::cout << phx::val("The bad one ") << _1 << '\n'] >> '}'); return 0; } HTH
2011/8/15 TONGARI <tongari95@gmail.com>
2011/8/15 <boost_mailinglist@gmx.de>
Hello,
if I execute the following example, I get the following output "The bad one The good one a" but I only expected "The good one a".
Why does it happens? Boost 1.47.0 was used.
#include <boost/config/warning_disable.**hpp> #include <boost/spirit/include/qi.hpp> #include <boost/lambda/lambda.hpp>
#include <iostream>
int main() { using boost::spirit::qi::char_; using boost::spirit::qi::parse;
char const *first = "{a}", *last = first + std::strlen(first);
parse(first, last, '{' >> char_("a")[std::cout << "The good one " << boost::lambda::_1 << '\n'] | char_("b")[std::cout << "The bad one " << boost::lambda::_1 << '\n'] >> '}');
return 0; }
Because (std::cout << "The good one ") & (std::cout << "The bad one ") are not lambda expressions so they are executed immediately, and it seems Boost.Lambda just happened to work here.
The corrected version would be:
#include <boost/config/warning_disable.hpp> #include <boost/spirit/include/qi.hpp> #include <boost/spirit/include/phoenix.hpp>
#include <iostream>
int main() { using boost::spirit::qi::char_; using boost::spirit::qi::parse; using boost::spirit::qi::_1; using boost::phoenix::val;
char const *first = "{a}", *last = first + std::strlen(first);
parse(first, last, '{' >> char_("a")[std::cout << phx::val("The good one ") << _1 << '\n'] | char_("b")[std::cout << phx::val("The bad one ") << _1 << '\n'] >> '}');
return 0; }
Please omit phx:: before val.
Am 15.08.2011 16:21, schrieb TONGARI:
2011/8/15 TONGARI<tongari95@gmail.com>
2011/8/15<boost_mailinglist@gmx.de>
Hello,
if I execute the following example, I get the following output "The bad one The good one a" but I only expected "The good one a".
Why does it happens? Boost 1.47.0 was used.
#include<boost/config/warning_disable.**hpp> #include<boost/spirit/include/qi.hpp> #include<boost/lambda/lambda.hpp>
#include<iostream>
int main() { using boost::spirit::qi::char_; using boost::spirit::qi::parse;
char const *first = "{a}", *last = first + std::strlen(first);
parse(first, last, '{'>> char_("a")[std::cout<< "The good one "<< boost::lambda::_1<< '\n'] | char_("b")[std::cout<< "The bad one "<< boost::lambda::_1<< '\n']>> '}');
return 0; }
Because (std::cout<< "The good one ")& (std::cout<< "The bad one ") are not lambda expressions so they are executed immediately, and it seems Boost.Lambda just happened to work here.
The corrected version would be:
#include<boost/config/warning_disable.hpp> #include<boost/spirit/include/qi.hpp> #include<boost/spirit/include/phoenix.hpp>
#include<iostream>
int main() { using boost::spirit::qi::char_; using boost::spirit::qi::parse; using boost::spirit::qi::_1; using boost::phoenix::val;
char const *first = "{a}", *last = first + std::strlen(first);
parse(first, last, '{'>> char_("a")[std::cout<< phx::val("The good one ")<< _1<< '\n'] | char_("b")[std::cout<< phx::val("The bad one ")<< _1<< '\n']>> '}');
return 0; }
Please omit phx:: before val.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Thank you, it is working now.
participants (2)
-
boost_mailinglist@gmx.de
-
TONGARI