提振精神,提升和任何引用字符串 - 编译时错误 [英] boost spirit, boost any and quoted string - compile-time error

查看:223
本文介绍了提振精神,提升和任何引用字符串 - 编译时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的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_ - '"') >> '"']

我不能在这里和pastie.org因为字符数量限制的发布错误。

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?

推荐答案

再次为什么将要使用的事项使其缓慢复杂的boost ::任何

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

不管怎么说, +字符_ 公开的std ::矢量&lt;字符&GT; ,显然属性兼容性规则不要决定什么该变换。

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

请其明确的与 as_string 住在Coliru

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";
        }
    }
}

这篇关于提振精神,提升和任何引用字符串 - 编译时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆