template.cpp 2.0 KB

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