integral.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/concept/metafunction.hpp>
  6. #include <boost/hana/equal.hpp>
  7. #include <boost/hana/not.hpp>
  8. #include <boost/hana/type.hpp>
  9. #include <type_traits>
  10. namespace hana = boost::hana;
  11. struct x1; struct x2; struct x3;
  12. struct y1 { }; struct y2 { }; struct y3 { };
  13. template <typename ...> struct mf { struct type { }; };
  14. struct mfc { template <typename ...> struct apply { struct type { }; }; };
  15. template <typename ...> struct tpl { };
  16. // make sure `integral(f)(...)` returns the right type
  17. static_assert(std::is_same<
  18. decltype(hana::integral(hana::metafunction<mf>)()),
  19. mf<>::type
  20. >{}, "");
  21. static_assert(std::is_same<
  22. decltype(hana::integral(hana::metafunction<mf>)(hana::type_c<x1>)),
  23. mf<x1>::type
  24. >{}, "");
  25. static_assert(std::is_same<
  26. decltype(hana::integral(hana::metafunction<mf>)(hana::type_c<x1>, hana::type_c<x2>)),
  27. mf<x1, x2>::type
  28. >{}, "");
  29. static_assert(std::is_same<
  30. decltype(hana::integral(hana::template_<tpl>)()),
  31. tpl<>
  32. >{}, "");
  33. static_assert(std::is_same<
  34. decltype(hana::integral(hana::template_<tpl>)(hana::type_c<x1>)),
  35. tpl<x1>
  36. >{}, "");
  37. static_assert(std::is_same<
  38. decltype(hana::integral(hana::template_<tpl>)(hana::type_c<x1>, hana::type_c<x2>)),
  39. tpl<x1, x2>
  40. >{}, "");
  41. static_assert(std::is_same<
  42. decltype(hana::integral(hana::metafunction_class<mfc>)()),
  43. mfc::apply<>::type
  44. >{}, "");
  45. static_assert(std::is_same<
  46. decltype(hana::integral(hana::metafunction_class<mfc>)(hana::type_c<x1>)),
  47. mfc::apply<x1>::type
  48. >{}, "");
  49. static_assert(std::is_same<
  50. decltype(hana::integral(hana::metafunction_class<mfc>)(hana::type_c<x1>, hana::type_c<x2>)),
  51. mfc::apply<x1, x2>::type
  52. >{}, "");
  53. // Make sure integral is SFINAE-friendly
  54. struct invalid_hana_metafunction {
  55. template <typename ...> struct apply { /* missing type alias */ };
  56. };
  57. auto invalid_integral = hana::integral(invalid_hana_metafunction{});
  58. BOOST_HANA_CONSTANT_CHECK(hana::not_(
  59. hana::is_valid(invalid_integral)(hana::type_c<void>, hana::type_c<void>)
  60. ));
  61. int main() {
  62. // Make sure we can perform the call; we already made sure the return type was correct
  63. constexpr auto a = hana::integral(hana::metafunction<mf>)(); (void)a;
  64. constexpr auto b = hana::integral(hana::metafunction<mf>)(hana::type_c<x1>); (void)b;
  65. constexpr auto c = hana::integral(hana::metafunction<mf>)(hana::type_c<x1>, hana::type_c<x2>); (void)c;
  66. constexpr auto d = hana::integral(hana::metafunction<mf>)(hana::type_c<x1>, hana::type_c<x2>, hana::type_c<x3>); (void)d;
  67. // Make sure we don't read from a non-constexpr variable
  68. auto t = hana::type_c<x1>;
  69. constexpr auto r = hana::integral(hana::metafunction<mf>)(t);
  70. (void)r;
  71. }