return_.qbk 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. [#return_]
  2. [section return_]
  3. [h1 Synopsis]
  4. template <class C>
  5. struct return_;
  6. This is a [link parser parser].
  7. [table Arguments
  8. [[Name] [Type]]
  9. [[`C`] [[link metaprogramming_value template metaprogramming value]]]
  10. ]
  11. [h1 Description]
  12. `return_` accepts every input. The result of parsing is `C`, the remaining
  13. string is the input string.
  14. [h1 Header]
  15. #include <boost/metaparse/return_.hpp>
  16. [h1 Expression semantics]
  17. For any `c` class, `s` compile-time string and `pos` source position the
  18. following are equivalent
  19. get_result<return_<c>::apply<s, pos>>::type
  20. c
  21. get_remaining<return_<c>::apply<s, pos>>::type
  22. s
  23. get_position<return_<c>::apply<s, pos>>::type
  24. pos
  25. [h1 Example]
  26. #include <boost/metaparse/return_.hpp>
  27. #include <boost/metaparse/int_.hpp>
  28. #include <boost/metaparse/one_of.hpp>
  29. #include <boost/metaparse/start.hpp>
  30. #include <boost/metaparse/string.hpp>
  31. #include <boost/metaparse/get_result.hpp>
  32. #include <type_traits>
  33. using namespace boost::metaparse;
  34. using default_value = std::integral_constant<int, 13>;
  35. using optional_number = one_of<int_, return_<default_value>>;
  36. static_assert(
  37. get_result<
  38. optional_number::apply<BOOST_METAPARSE_STRING("11"), start>
  39. >::type::value == 11,
  40. "when a number is provided, it is the result of parsing"
  41. );
  42. static_assert(
  43. get_result<
  44. optional_number::apply<BOOST_METAPARSE_STRING(""), start>
  45. >::type::value == 13,
  46. "when no number is provided, the default value is the result of parsing"
  47. );
  48. [endsect]