always_c.qbk 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. [#always_c]
  2. [section always_c]
  3. [h1 Synopsis]
  4. template <char C, class T>
  5. struct always_c;
  6. This is a [link parser_combinator parser combinator].
  7. [table Arguments
  8. [[Name] [Type]]
  9. [[`C`] [character]]
  10. [[`T`] [[link metaprogramming_value template metaprogramming value]]]
  11. ]
  12. [h1 Description]
  13. It accepts inputs beginning with the `C` character. It consumes that character
  14. and the result of parsing is `T`. Other inputs as rejected.
  15. [h1 Header]
  16. #include <boost/metaparse/always_c.hpp>
  17. [h1 Expression semantics]
  18. For any `c` character and `t` class the following are equivalent:
  19. always_c<c, t>
  20. always<lit_c<c>, t>
  21. [h1 Example]
  22. #include <boost/metaparse/always_c.hpp>
  23. #include <boost/metaparse/start.hpp>
  24. #include <boost/metaparse/string.hpp>
  25. #include <boost/metaparse/is_error.hpp>
  26. #include <boost/metaparse/get_result.hpp>
  27. #include <type_traits>
  28. using namespace boost::metaparse;
  29. using always13 = always_c<'x', std::integral_constant<int, 13>>;
  30. static_assert(
  31. !is_error<always13::apply<BOOST_METAPARSE_STRING("x"), start>>::type::value,
  32. "always13 should accept x"
  33. );
  34. static_assert(
  35. std::is_same<
  36. get_result<always13::apply<BOOST_METAPARSE_STRING("x"), start>>::type,
  37. std::integral_constant<int, 13>
  38. >::type::value,
  39. "the result of parsing should be the integral_constant type"
  40. );
  41. static_assert(
  42. is_error<always13::apply<BOOST_METAPARSE_STRING("y"), start>>::type::value,
  43. "always13 should reject characters other than x"
  44. );
  45. [endsect]