http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59709

            Bug ID: 59709
           Summary: break program behavior with optimization
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: faithandbrave at gmail dot com

Hi, I encountered difficult problem. If I use -O2 option, follow code behavior
be break. (in GCC 4.8.2)

#include <iostream>
#include <string>
#include <boost/spirit/include/qi.hpp>

namespace qi = boost::spirit::qi;

int main()
{
    const std::string input = "hello:";

    auto rule = *(qi::char_ - ':') >> ':';

    std::string::const_iterator it = input.begin();
    std::string result;
    if (qi::parse(it, input.end(), rule, result)) {
        std::cout << "parse successful" << std::endl;
    }
    else {
        std::cout << "parse failed" << std::endl;
    }
}

If I don't use optimization, the problem result is "parse successful". But if I
use optimization, the problem result is output "parse failed" or output
nothing.

If I don't use `auto` and I write type, the problem result is "parse
successful" correctly :

#include <iostream>
#include <string>
#include <boost/spirit/include/qi.hpp>

namespace qi = boost::spirit::qi;

int main()
{
    const std::string input = "hello:";

    qi::rule<std::string::const_iterator, std::string()> rule = *(qi::char_ -
':') >> ':';

    std::string::const_iterator it = input.begin();
    std::string result;
    if (qi::parse(it, input.end(), rule, result)) {
        std::cout << "parse successful" << std::endl;
    }
    else {
        std::cout << "parse failed" << std::endl;
    }
}

I couldn't write minimal code without Boost.Spirit. Sorry.

Reply via email to