Question

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::rule<Iterator, boost::any, boost::spirit::qi::ascii::space_type> start;
};

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

  boost::any var;
  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';
    }
  }
  else
  {
    std::cout << "Parsing failed \n";
  }
}

Output

Parsing succeeded
boost::bad_any_cast: failed conversion using boost::any_cast

I think that cast should work fine in such case. Am i wrong? How can I fix it?

Was it helpful?

Solution

Initialize your any!

boost::any var = 0;

Spirit binds the attribute value by reference, so it will cast to int& before invoking the qi::int_ parser.

On a side note, using boost::any with Spirit seems like misguided plan. I can't see why you'd diverge from statically known types and e.g. Variant. But I'll leave that up to you :)

P.S. Oh, and you forgot some more parentheses like here :). See it Live On Coliru now

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top