at.non_const.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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/tuple.hpp>
  6. #include <string>
  7. namespace hana = boost::hana;
  8. struct Empty {};
  9. struct S {
  10. constexpr S()
  11. : a{1, Empty{}},
  12. k(hana::at_c<0>(a)),
  13. e(hana::at_c<1>(a))
  14. { }
  15. hana::tuple<int, Empty> a;
  16. int k;
  17. Empty e;
  18. };
  19. constexpr hana::tuple<int, int> getP () { return { 3, 4 }; }
  20. int main() {
  21. {
  22. using T = hana::tuple<int>;
  23. T t(3);
  24. BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == 3);
  25. hana::at_c<0>(t) = 2;
  26. BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == 2);
  27. }
  28. {
  29. using T = hana::tuple<std::string, int>;
  30. T t("high", 5);
  31. BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == "high");
  32. BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(t) == 5);
  33. hana::at_c<0>(t) = "four";
  34. hana::at_c<1>(t) = 4;
  35. BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == "four");
  36. BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(t) == 4);
  37. }
  38. {
  39. using T = hana::tuple<double&, std::string, int>;
  40. double d = 1.5;
  41. T t(d, "high", 5);
  42. BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == 1.5);
  43. BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(t) == "high");
  44. BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(t) == 5);
  45. hana::at_c<0>(t) = 2.5;
  46. hana::at_c<1>(t) = "four";
  47. hana::at_c<2>(t) = 4;
  48. BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == 2.5);
  49. BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(t) == "four");
  50. BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(t) == 4);
  51. BOOST_HANA_RUNTIME_CHECK(d == 2.5);
  52. }
  53. // get on a rvalue tuple
  54. {
  55. static_assert(hana::at_c<0>(hana::tuple<float, int, double, long>{0.0f, 1, 2.0, 3L}) == 0, "" );
  56. static_assert(hana::at_c<1>(hana::tuple<float, int, double, long>{0.0f, 1, 2.0, 3L}) == 1, "" );
  57. static_assert(hana::at_c<2>(hana::tuple<float, int, double, long>{0.0f, 1, 2.0, 3L}) == 2, "" );
  58. static_assert(hana::at_c<3>(hana::tuple<float, int, double, long>{0.0f, 1, 2.0, 3L}) == 3, "" );
  59. static_assert(S().k == 1, "");
  60. static_assert(hana::at_c<1>(getP()) == 4, "");
  61. }
  62. {
  63. // make sure get<> returns the right types
  64. struct T { };
  65. struct U { };
  66. struct V { };
  67. hana::tuple<T, U, V> xs{};
  68. (void)static_cast<T>(hana::at_c<0>(xs));
  69. (void)static_cast<U>(hana::at_c<1>(xs));
  70. (void)static_cast<V>(hana::at_c<2>(xs));
  71. }
  72. }