c++ - undefined behaviour somewhere in boost::spirit::qi::phrase_parse -
c++ - undefined behaviour somewhere in boost::spirit::qi::phrase_parse -
i learning utilize boost::spirit library. took illustration http://www.boost.org/doc/libs/1_56_0/libs/spirit/example/qi/num_list1.cpp , compiled on computer - works fine.
however if modify little - if initialize parser itself
auto parser = qi::double_ >> *(',' >> qi::double_);
somewhere global variable , pass phrase_parse, goes crazy. here finish modified code (only 1 line modified , 1 added) - http://pastebin.com/5rws3pmt
if run original code , pass "3.14, 3.15" stdin, says parsing succeeded, modified version fails. tried lot of modifications of same type - assigning parser global variable - in variants on compilers segfaults.
i don't understand why , how so. here another, simpler version prints true , segfaults on clang++ , segfaults on g++
#include <boost/spirit/include/qi.hpp> #include <iostream> #include <string> namespace qi = boost::spirit::qi; namespace ascii = boost::spirit::ascii; const auto doubles_parser_global = qi::double_ >> *(',' >> qi::double_); int main() { const auto doubles_parser_local = qi::double_ >> *(',' >> qi::double_); const std::string nums {"3.14, 3.15, 3.1415926"}; std::cout << std::boolalpha; std::cout << qi::phrase_parse( nums.cbegin(), nums.cend(), doubles_parser_local, ascii::space ) << std::endl; // works fine std::cout << qi::phrase_parse( nums.cbegin(), nums.cend(), doubles_parser_global, ascii::space ) // segfaults << std::endl; }
you cannot utilize auto
store parser expressions¹
either need evaluate temporary look directly, or need assign rule/grammar:
const qi::rule<std::string::const_iterator, qi::space_type> doubles_parser_local = qi::double_ >> *(',' >> qi::double_);
you can have cake , eat on recent boost versions (possibly dev branch) there should boost_spirit_auto macro
this becoming bit of faq item:
assigning parsers auto variables boost spirit v2 qi bug associated optimization level¹ believe limitation of underlying proto library. there's proto-0x lib version on github (by eric niebler) promises solve these issues beingness redesigned aware of references. think required c++11 features boost proto cannot use.
c++ boost auto boost-spirit
Comments
Post a Comment