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