integral_constant.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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/concept/constant.hpp>
  5. #include <boost/hana/concept/integral_constant.hpp>
  6. #include <boost/hana/core/when.hpp>
  7. #include <boost/hana/fwd/core/to.hpp>
  8. #include <boost/hana/value.hpp>
  9. namespace hana = boost::hana;
  10. // Define a simple model of IntegralConstant
  11. struct constant_tag { using value_type = int; };
  12. template <int i>
  13. struct constant {
  14. static constexpr int value = i;
  15. using hana_tag = constant_tag;
  16. };
  17. namespace boost { namespace hana {
  18. template <>
  19. struct IntegralConstant<constant_tag> {
  20. static constexpr bool value = true;
  21. };
  22. template <typename From>
  23. struct to_impl<constant_tag, From, when<IntegralConstant<From>::value>> {
  24. template <typename N>
  25. static constexpr auto apply(N const&)
  26. { return constant<N::value>{}; }
  27. };
  28. }}
  29. // Make sure we really satisfy IntegralConstant<>.
  30. static_assert(hana::IntegralConstant<constant<0>>::value, "");
  31. static_assert(hana::IntegralConstant<constant<1>>::value, "");
  32. static_assert(hana::IntegralConstant<constant<2>>::value, "");
  33. // Make sure we're also a model of Constant automatically.
  34. static_assert(hana::Constant<constant<0>>::value, "");
  35. static_assert(hana::Constant<constant<1>>::value, "");
  36. static_assert(hana::Constant<constant<2>>::value, "");
  37. // Make sure we have the hana::value<> function defined automatically.
  38. static_assert(hana::value<constant<0>>() == 0, "");
  39. static_assert(hana::value<constant<1>>() == 1, "");
  40. static_assert(hana::value<constant<2>>() == 2, "");
  41. static_assert(hana::value<constant<3>>() == 3, "");
  42. int main() { }