if_.qbk 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. [#if_]
  2. [section if_]
  3. [h1 Synopsis]
  4. template <class P, class T, class F>
  5. struct if_;
  6. This is a [link parser_combinator parser combinator].
  7. [table Arguments
  8. [[Name] [Type]]
  9. [[`P`] [[link parser parser]]]
  10. [[`T`] [[link metaprogramming_value template metaprogramming value]]]
  11. [[`F`] [[link metaprogramming_value template metaprogramming value]]]
  12. ]
  13. [h1 Description]
  14. `if_` always accepts the input string. The result of parsing is `T`, when `P`
  15. accepts the input and `F` otherwise.
  16. [h1 Header]
  17. #include <boost/metaparse/if_.hpp>
  18. [h1 Expression semantics]
  19. For any `p` parser, `t` and `f` classes the following are equivalent:
  20. if_<p, t, f>
  21. one_of<last_of<p, return_<t>>, return_<f>>
  22. [h1 Example]
  23. #include <boost/metaparse/if_.hpp>
  24. #include <boost/metaparse/int_.hpp>
  25. #include <boost/metaparse/string.hpp>
  26. #include <boost/metaparse/start.hpp>
  27. #include <boost/metaparse/get_result.hpp>
  28. #include <type_traits>
  29. using namespace boost::metaparse;
  30. using int11 = std::integral_constant<int, 11>;
  31. using int13 = std::integral_constant<int, 13>;
  32. static_assert(
  33. get_result<
  34. if_<int_, int11, int13>::apply<BOOST_METAPARSE_STRING("1234"), start>
  35. >::type::value == 11,
  36. "When the int_ parser succeeds, the result of parsing should be 11"
  37. );
  38. static_assert(
  39. get_result<
  40. if_<int_, int11, int13>::apply<BOOST_METAPARSE_STRING("foo"), start>
  41. >::type::value == 13,
  42. "When the int_ parser fails, the result of parsing should be 13"
  43. );
  44. [endsect]