| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // UNSUPPORTED: c++03, c++11 |
| 10 | |
| 11 | #include <tuple> |
| 12 | #include <utility> |
| 13 | #include <memory> |
| 14 | #include <string> |
| 15 | #include <complex> |
| 16 | #include <type_traits> |
| 17 | |
| 18 | #include <cassert> |
| 19 | |
| 20 | #include "test_macros.h" |
| 21 | |
| 22 | int main(int, char**) |
| 23 | { |
| 24 | typedef std::complex<float> cf; |
| 25 | { |
| 26 | auto t1 = std::tuple<int, std::string, cf> { 42, "Hi" , { 1,2 }}; |
| 27 | assert ( std::get<int>(t1) == 42 ); // find at the beginning |
| 28 | assert ( std::get<std::string>(t1) == "Hi" ); // find in the middle |
| 29 | assert ( std::get<cf>(t1).real() == 1 ); // find at the end |
| 30 | assert ( std::get<cf>(t1).imag() == 2 ); |
| 31 | } |
| 32 | |
| 33 | { |
| 34 | auto t2 = std::tuple<int, std::string, int, cf> { 42, "Hi" , 23, { 1,2 }}; |
| 35 | // get<int> would fail! |
| 36 | assert ( std::get<std::string>(t2) == "Hi" ); |
| 37 | assert (( std::get<cf>(t2) == cf{ 1,2 } )); |
| 38 | } |
| 39 | |
| 40 | { |
| 41 | constexpr std::tuple<int, const int, double, double> p5 { 1, 2, 3.4, 5.6 }; |
| 42 | static_assert ( std::get<int>(p5) == 1, "" ); |
| 43 | static_assert ( std::get<const int>(p5) == 2, "" ); |
| 44 | } |
| 45 | |
| 46 | { |
| 47 | const std::tuple<int, const int, double, double> p5 { 1, 2, 3.4, 5.6 }; |
| 48 | const int &i1 = std::get<int>(p5); |
| 49 | const int &i2 = std::get<const int>(p5); |
| 50 | assert ( i1 == 1 ); |
| 51 | assert ( i2 == 2 ); |
| 52 | } |
| 53 | |
| 54 | { |
| 55 | typedef std::unique_ptr<int> upint; |
| 56 | std::tuple<upint> t(upint(new int(4))); |
| 57 | upint p = std::get<upint>(std::move(t)); // get rvalue |
| 58 | assert(*p == 4); |
| 59 | assert(std::get<upint>(t) == nullptr); // has been moved from |
| 60 | } |
| 61 | |
| 62 | { |
| 63 | typedef std::unique_ptr<int> upint; |
| 64 | const std::tuple<upint> t(upint(new int(4))); |
| 65 | const upint&& p = std::get<upint>(std::move(t)); // get const rvalue |
| 66 | assert(*p == 4); |
| 67 | assert(std::get<upint>(t) != nullptr); |
| 68 | } |
| 69 | |
| 70 | { |
| 71 | int x = 42; |
| 72 | int y = 43; |
| 73 | std::tuple<int&, int const&> const t(x, y); |
| 74 | static_assert(std::is_same<int&, decltype(std::get<int&>(std::move(t)))>::value, "" ); |
| 75 | static_assert(noexcept(std::get<int&>(std::move(t))), "" ); |
| 76 | static_assert(std::is_same<int const&, decltype(std::get<int const&>(std::move(t)))>::value, "" ); |
| 77 | static_assert(noexcept(std::get<int const&>(std::move(t))), "" ); |
| 78 | } |
| 79 | |
| 80 | { |
| 81 | int x = 42; |
| 82 | int y = 43; |
| 83 | std::tuple<int&&, int const&&> const t(std::move(x), std::move(y)); |
| 84 | static_assert(std::is_same<int&&, decltype(std::get<int&&>(std::move(t)))>::value, "" ); |
| 85 | static_assert(noexcept(std::get<int&&>(std::move(t))), "" ); |
| 86 | static_assert(std::is_same<int const&&, decltype(std::get<int const&&>(std::move(t)))>::value, "" ); |
| 87 | static_assert(noexcept(std::get<int const&&>(std::move(t))), "" ); |
| 88 | } |
| 89 | |
| 90 | { |
| 91 | constexpr const std::tuple<int, const int, double, double> t { 1, 2, 3.4, 5.6 }; |
| 92 | static_assert(std::get<int>(std::move(t)) == 1, "" ); |
| 93 | static_assert(std::get<const int>(std::move(t)) == 2, "" ); |
| 94 | } |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |