integral_constant.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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/config.hpp>
  6. #include <boost/hana/equal.hpp>
  7. #include <boost/hana/for_each.hpp>
  8. #include <boost/hana/integral_constant.hpp>
  9. #include <boost/hana/mult.hpp>
  10. #include <boost/hana/negate.hpp>
  11. #include <boost/hana/plus.hpp>
  12. #include <boost/hana/tuple.hpp>
  13. #include <string>
  14. #include <vector>
  15. namespace hana = boost::hana;
  16. int main() {
  17. {
  18. //! [operators]
  19. BOOST_HANA_CONSTANT_CHECK(hana::int_c<1> + hana::int_c<3> == hana::int_c<4>);
  20. // Mixed-type operations are supported, but only when it involves a
  21. // promotion, and not a conversion that could be lossy.
  22. BOOST_HANA_CONSTANT_CHECK(hana::size_c<3> * hana::ushort_c<5> == hana::size_c<15>);
  23. BOOST_HANA_CONSTANT_CHECK(hana::llong_c<15> == hana::int_c<15>);
  24. //! [operators]
  25. }{
  26. //! [times_loop_unrolling]
  27. std::string s;
  28. for (char c = 'x'; c <= 'z'; ++c)
  29. hana::int_<5>::times([&] { s += c; });
  30. BOOST_HANA_RUNTIME_CHECK(s == "xxxxxyyyyyzzzzz");
  31. //! [times_loop_unrolling]
  32. }{
  33. //! [times_higher_order]
  34. std::string s;
  35. auto functions = hana::make_tuple(
  36. [&] { s += "x"; },
  37. [&] { s += "y"; },
  38. [&] { s += "z"; }
  39. );
  40. hana::for_each(functions, hana::int_<5>::times);
  41. BOOST_HANA_RUNTIME_CHECK(s == "xxxxxyyyyyzzzzz");
  42. //! [times_higher_order]
  43. }{
  44. //! [from_object]
  45. std::string s;
  46. for (char c = 'x'; c <= 'z'; ++c)
  47. hana::int_c<5>.times([&] { s += c; });
  48. BOOST_HANA_RUNTIME_CHECK(s == "xxxxxyyyyyzzzzz");
  49. //! [from_object]
  50. }{
  51. //! [times_with_index_runtime]
  52. std::vector<int> v;
  53. hana::int_<5>::times.with_index([&](auto index) { v.push_back(index); });
  54. BOOST_HANA_RUNTIME_CHECK(v == std::vector<int>{0, 1, 2, 3, 4});
  55. //! [times_with_index_runtime]
  56. //! [times_with_index_compile_time]
  57. constexpr auto xs = hana::tuple_c<int, 0, 1, 2>;
  58. hana::int_<3>::times.with_index([xs](auto index) {
  59. BOOST_HANA_CONSTANT_CHECK(xs[index] == index);
  60. });
  61. //! [times_with_index_compile_time]
  62. }{
  63. //! [literals]
  64. using namespace hana::literals; // contains the _c suffix
  65. BOOST_HANA_CONSTANT_CHECK(1234_c == hana::llong_c<1234>);
  66. BOOST_HANA_CONSTANT_CHECK(-1234_c == hana::llong_c<-1234>);
  67. BOOST_HANA_CONSTANT_CHECK(1_c + (3_c * 4_c) == hana::llong_c<1 + (3 * 4)>);
  68. //! [literals]
  69. }{
  70. //! [integral_c]
  71. BOOST_HANA_CONSTANT_CHECK(hana::integral_c<int, 2> == hana::int_c<2>);
  72. static_assert(decltype(hana::integral_c<int, 2>)::value == 2, "");
  73. //! [integral_c]
  74. }
  75. }