'How do I get which() to work correctly in boost spirit x3 expectation_failure?

Calling which() in expectation_failure returns a strange std::string.
How can I fix it?

#include <boost/spirit/home/x3.hpp>
#include <boost/spirit/home/x3/support/utility/error_reporting.hpp>
#include <iostream>

namespace x3 = boost::spirit::x3;

struct my_error_handler {
  template <typename Iterator, typename Context>
  auto on_error(Iterator&,
                Iterator const&,
                const x3::expectation_failure<Iterator>& x,
                const Context&)
  {
    std::cerr << x.which() << '\n';
    return x3::error_handler_result::fail;
  }
};

struct test_class : my_error_handler {
};

const x3::rule<test_class, int> test{"test"};

const auto test_def = x3::expect[x3::int_];

BOOST_SPIRIT_DEFINE(test)

auto parse(std::string&& input)
{
  auto       first = std::cbegin(input);
  const auto last  = std::cend(input);

  x3::error_handler<decltype(first)> error_handler{first, last, std::cerr};

  const auto parser
    = x3::with<x3::error_handler_tag>(std::ref(error_handler))[test];

  x3::phrase_parse(std::cbegin(input), std::cend(input), parser, x3::space);
}

int main()
{
  parse("a123");
}

Live on wandbox

The execution result is shown below.

N5boost6spirit2x310int_parserIiLj10ELj1ELin1EEE



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source