boost spirit, boost any and quoted string - compile-time error

梦想与她 提交于 2019-12-13 18:10:01

问题


I have the following code:

#include <boost/any.hpp>
#include <boost/spirit/include/qi.hpp>

#include <iostream>
#include <string>

template <typename Iterator>
struct parser : boost::spirit::qi::grammar<Iterator, boost::any(), boost::spirit::qi::ascii::space_type>
{
  parser() : parser::base_type(start)
  {
    start %= boost::spirit::qi::int_ | boost::spirit::qi::lexeme['"' >> +(boost::spirit::qi::char_ - '"') >> '"']; // example: 0 or "str"
  }

  boost::spirit::qi::rule<Iterator, boost::any(), boost::spirit::qi::ascii::space_type> start;
};

int main()
{
  const std::string input_data("\"str\"");

  boost::any var = 0;
  auto itr = input_data.begin();
  auto end = input_data.end();
  parser<decltype(itr)> g;
  bool res = boost::spirit::qi::phrase_parse(itr, end, g, boost::spirit::ascii::space, var);
  if (res && itr == end)
  {
    std::cout << "Parsing succeeded \n";
    try
    {
      std::cout << boost::any_cast<std::string>(var) << '\n';
    }
    catch (const boost::bad_any_cast& ex)
    {
      std::cerr << ex.what() << '\n';
    }
  }
  else
  {
    std::cout << "Parsing failed \n";
  }
}

It compiles fine until I add

boost::spirit::qi::lexeme['"' >> +(boost::spirit::qi::char_ - '"') >> '"']

I can't post the errors here and on pastie.org because of the characters amount limitations.

What am I doing wrong? How can I fix it?


回答1:


Again, why would you want to complicate matters and make it slow by using boost::any?

Anyways, +char_ exposes std::vector<char> and apparently the attribute compatibility rules don't want to decide what to transform this to.

Make it explicit with as_string: Live On Coliru

#include <boost/any.hpp>
#include <boost/spirit/include/qi.hpp>

#include <iostream>
#include <string>

template <typename Iterator>
struct parser : boost::spirit::qi::grammar<Iterator, boost::any(), boost::spirit::qi::ascii::space_type>
{
    parser() : parser::base_type(start)
    {
        using namespace boost::spirit::qi;
        start %= int_ | as_string [ lexeme['"' >> +(char_ - '"') >> '"'] ]; // example: 0 or "str"
    }

    boost::spirit::qi::rule<Iterator, boost::any(), boost::spirit::qi::ascii::space_type> start;
};

int main()
{
//  const std::string input_data("\"str\"");
    const std::string input_data("123");
    for (std::string const& input_data : { "\"str\"", "123" })
    {
        boost::any var = boost::none;
        auto itr = input_data.begin();
        auto end = input_data.end();
        parser<decltype(itr)> g;
        bool res = boost::spirit::qi::phrase_parse(itr, end, g, boost::spirit::ascii::space, var);
        if (res && itr == end)
        {
            std::cout << "Parsing succeeded \n";
            try { std::cout << boost::any_cast<int>        (var) << '\n'; } catch (const boost::bad_any_cast& ex) { std::cerr << ex.what() << '\n'; }
            try { std::cout << boost::any_cast<std::string>(var) << '\n'; } catch (const boost::bad_any_cast& ex) { std::cerr << ex.what() << '\n'; }
        }
        else
        {
            std::cout << "Parsing failed \n";
        }
    }
}


来源:https://stackoverflow.com/questions/21647277/boost-spirit-boost-any-and-quoted-string-compile-time-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!