monad.types.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright Louis Dionne 2013-2017
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  4. #include <boost/hana/assert.hpp>
  5. #include <boost/hana/chain.hpp>
  6. #include <boost/hana/equal.hpp>
  7. #include <boost/hana/for_each.hpp>
  8. #include <boost/hana/traits.hpp>
  9. #include <boost/hana/tuple.hpp>
  10. #include <boost/hana/type.hpp>
  11. #include <type_traits>
  12. #include <utility>
  13. namespace hana = boost::hana;
  14. // Using the `tuple` Monad, we generate all the possible combinations of
  15. // cv-qualifiers and reference qualifiers. Then, we use the `optional`
  16. // Monad to make sure that our generic function can be called with
  17. // arguments of any of those types.
  18. // cv_qualifiers : type -> tuple(type)
  19. auto cv_qualifiers = [](auto t) {
  20. return hana::make_tuple(
  21. t,
  22. hana::traits::add_const(t),
  23. hana::traits::add_volatile(t),
  24. hana::traits::add_volatile(hana::traits::add_const(t))
  25. );
  26. };
  27. // ref_qualifiers : type -> tuple(type)
  28. auto ref_qualifiers = [](auto t) {
  29. return hana::make_tuple(
  30. hana::traits::add_lvalue_reference(t),
  31. hana::traits::add_rvalue_reference(t)
  32. );
  33. };
  34. auto possible_args = cv_qualifiers(hana::type_c<int>) | ref_qualifiers;
  35. BOOST_HANA_CONSTANT_CHECK(
  36. possible_args == hana::make_tuple(
  37. hana::type_c<int&>,
  38. hana::type_c<int&&>,
  39. hana::type_c<int const&>,
  40. hana::type_c<int const&&>,
  41. hana::type_c<int volatile&>,
  42. hana::type_c<int volatile&&>,
  43. hana::type_c<int const volatile&>,
  44. hana::type_c<int const volatile&&>
  45. )
  46. );
  47. struct some_function {
  48. template <typename T>
  49. void operator()(T&&) const { }
  50. };
  51. int main() {
  52. hana::for_each(possible_args, [](auto t) {
  53. using T = typename decltype(t)::type;
  54. static_assert(decltype(hana::is_valid(some_function{})(std::declval<T>())){},
  55. "some_function should be callable with any type of argument");
  56. });
  57. }