fork download
  1. #include <boost/spirit/include/qi.hpp>
  2. #include <boost/spirit/include/phoenix.hpp>
  3. namespace qi = boost::spirit::qi;
  4.  
  5. enum numeric_types
  6. {
  7. fmt_none,
  8. fmt_float,
  9. fmt_double,
  10. fmt_uint,
  11. fmt_int,
  12. // fmt_hex, etc.
  13. };
  14.  
  15. template <typename It>
  16. bool is_numeric(It& f, It l, numeric_types& detected)
  17. {
  18. return qi::phrase_parse(f,l,
  19. qi::uint_ [ qi::_val = fmt_uint ]
  20. | qi::int_ [ qi::_val = fmt_int ]
  21. | qi::float_ [ qi::_val = fmt_float ]
  22. | qi::double_ [ qi::_val = fmt_double ]
  23. ,qi::space, detected);
  24. }
  25.  
  26. template <typename It>
  27. bool is_numeric(It& f, It l)
  28. {
  29. numeric_types detected = fmt_none;
  30. return is_numeric(f, l, detected);
  31. }
  32.  
  33. int main()
  34. {
  35. const std::string input = "124, -25, 582";
  36. std::string::const_iterator it = input.begin();
  37.  
  38. bool ok = is_numeric(it, input.end());
  39.  
  40. if (ok)
  41. {
  42. std::cout << "parse success\n";
  43. if (it!=input.end())
  44. std::cerr << "trailing unparsed: '" << std::string(it,input.end()) << "'\n";
  45. }
  46. else
  47. std::cerr << "parse failed: '" << std::string(it,input.end()) << "'\n";
  48.  
  49. return ok? 0 : 255;
  50. }
  51.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘bool is_numeric(It&, It, numeric_types&)’:
prog.cpp:19: error: ‘uint_’ is not a member of ‘qi’
prog.cpp:19: error: ‘_val’ is not a member of ‘qi’
prog.cpp:20: error: ‘int_’ is not a member of ‘qi’
prog.cpp:20: error: ‘_val’ is not a member of ‘qi’
prog.cpp:21: error: ‘float_’ is not a member of ‘qi’
prog.cpp:21: error: ‘_val’ is not a member of ‘qi’
prog.cpp:22: error: ‘double_’ is not a member of ‘qi’
prog.cpp:22: error: ‘_val’ is not a member of ‘qi’
prog.cpp:23: error: ‘space’ is not a member of ‘qi’
stdout
Standard output is empty