I want to use qi::int_parser<int64_t> to parse an integer value (it's really convenient how it automatically checks for overflows, handles the INT_MIN case, and so on). But, I also want to get the substring that the int_parser matched, because I want to print a warning message if it has extraneous characters (i.e., a plus sign, leading zeroes, or the case of -0).
I saw in another answer the suggestion to use qi::as_string, but it doesn't seem to work in this case. Here is some code that illustrates the issue:
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/operator.hpp>
#include <boost/spirit/include/qi.hpp>
#include <cstdint>
#include <iostream>
#include <string>
int main() {
namespace phx = boost::phoenix;
namespace qi = boost::spirit::qi;
using namespace std;
std::string value_str;
int64_t value;
std::string test_str = "<+123>";
const auto success = qi::parse(test_str.begin(), test_str.end(),
qi::char_('<') >>
qi::as_string[
qi::int_parser<int64_t>{}[phx::ref(value) = qi::_1]
][phx::ref(value_str) = qi::_1] >>
qi::char_('>')
);
std::cout << "success: " << success << '\n';
std::cout << "value: " << value << '\n';
std::cout << "matched substring: " << value_str << '\n';
}
The output I want is
success: 1
value: 123
matched substring: +123
The output I get is
success: 1
value: 123
matched substring: {
(or some other garbage). Parsing the int value works just fine, but I can't figure out how to get the substring.