metafunction.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 f { struct type; };
  14. template <typename F, typename ...T>
  15. constexpr auto valid_call(F f, T ...t) -> decltype(((void)f(t...)), true)
  16. { return true; }
  17. constexpr auto valid_call(...)
  18. { return false; }
  19. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  20. hana::metafunction<f>(),
  21. hana::type_c<f<>::type>
  22. ));
  23. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  24. hana::metafunction<f>(hana::type_c<x1>),
  25. hana::type_c<f<x1>::type>
  26. ));
  27. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  28. hana::metafunction<f>(hana::type_c<x1>, hana::type_c<x2>),
  29. hana::type_c<f<x1, x2>::type>
  30. ));
  31. BOOST_HANA_CONSTANT_CHECK(hana::equal(
  32. hana::metafunction<f>(hana::type_c<x1>, hana::type_c<x2>, hana::type_c<x3>),
  33. hana::type_c<f<x1, x2, x3>::type>
  34. ));
  35. using F = decltype(hana::metafunction<f>);
  36. static_assert(std::is_same<F::apply<>, f<>>::value, "");
  37. static_assert(std::is_same<F::apply<x1>, f<x1>>::value, "");
  38. static_assert(std::is_same<F::apply<x1, x2>, f<x1, x2>>::value, "");
  39. static_assert(std::is_same<F::apply<x1, x2, x3>, f<x1, x2, x3>>::value, "");
  40. // Make sure we're SFINAE-friendly
  41. template <typename ...T> struct no_type { };
  42. static_assert(!valid_call(hana::metafunction<no_type>), "");
  43. static_assert(!valid_call(hana::metafunction<no_type>, hana::type_c<x1>), "");
  44. // Make sure we model the Metafunction concept
  45. static_assert(hana::Metafunction<decltype(hana::metafunction<f>)>::value, "");
  46. static_assert(hana::Metafunction<decltype(hana::metafunction<f>)&>::value, "");
  47. // Make sure metafunction is SFINAE-friendly
  48. template <typename T> struct not_a_metafunction { };
  49. BOOST_HANA_CONSTANT_CHECK(hana::not_(
  50. hana::is_valid(hana::metafunction<not_a_metafunction>)(hana::type_c<void>)
  51. ));
  52. // Make sure we don't read from a non-constexpr variable
  53. int main() {
  54. auto t = hana::type_c<x1>;
  55. constexpr auto r = hana::metafunction<f>(t);
  56. (void)r;
  57. }