metafunction_class.cpp 2.6 KB

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