unless_error.qbk 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. [#unless_error]
  2. [section unless_error]
  3. [h1 Synopsis]
  4. template <class T, class NotErrorCase>
  5. struct unless_error;
  6. This is a [link lazy_metafunction lazy template metafunction].
  7. [table Arguments
  8. [[Name] [Type]]
  9. [[`T`] [[link accept accept] or [link reject reject] value]]
  10. [[`NotErrorCase`] [[link metaprogramming_value template metaprogramming value]]]
  11. ]
  12. [h1 Description]
  13. Checks if `T` is a parsing error or not. When it is, the result is `T`. When it
  14. is not, the result is `NotErrorCase`.
  15. [h1 Header]
  16. #include <boost/metaparse/unless_error.hpp>
  17. [h1 Expression semantics]
  18. For any `t` and `c` classes the following are equivalent:
  19. unless_error<t, c>
  20. boost::mpl::if_<is_error<t::type>::type, t, c>
  21. [h1 Example]
  22. #include <boost/metaparse/unless_error.hpp>
  23. #include <boost/metaparse/accept.hpp>
  24. #include <boost/metaparse/reject.hpp>
  25. #include <boost/metaparse/start.hpp>
  26. #include <boost/metaparse/string.hpp>
  27. #include <boost/metaparse/define_error.hpp>
  28. #include <type_traits>
  29. using namespace boost::metaparse;
  30. BOOST_METAPARSE_DEFINE_ERROR(sample_error, "Sample error message");
  31. using accept1 =
  32. accept<std::integral_constant<int, 11>, BOOST_METAPARSE_STRING("foo"), start>;
  33. using accept2 =
  34. accept<std::integral_constant<int, 13>, BOOST_METAPARSE_STRING("bar"), start>;
  35. using reject1 = reject<sample_error, start>;
  36. struct returns_accept1 { using type = accept1; };
  37. struct returns_accept2 { using type = accept2; };
  38. static_assert(
  39. std::is_same<accept2, unless_error<accept1, accept2>::type>::type::value,
  40. "it returns the second argument when the first argument is an accept"
  41. );
  42. static_assert(
  43. std::is_same<reject1, unless_error<reject1, accept2>::type>::type::value,
  44. "it returns the first argument when that is a reject"
  45. );
  46. static_assert(
  47. std::is_same<
  48. accept2,
  49. unless_error<returns_accept1, returns_accept2>::type
  50. >::type::value,
  51. "it supports lazy evaluation"
  52. );
  53. [endsect]