| 1 | // { dg-do compile { target c++17 } } |
| 2 | |
| 3 | // Copyright (C) 2017-2025 Free Software Foundation, Inc. |
| 4 | // |
| 5 | // This file is part of the GNU ISO C++ Library. This library is free |
| 6 | // software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the |
| 8 | // Free Software Foundation; either version 3, or (at your option) |
| 9 | // any later version. |
| 10 | |
| 11 | // This library is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | |
| 16 | // You should have received a copy of the GNU General Public License along |
| 17 | // with this library; see the file COPYING3. If not see |
| 18 | // <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | #include <tuple> |
| 21 | #include <memory> |
| 22 | |
| 23 | template<typename T, typename U> struct require_same; |
| 24 | template<typename T> struct require_same<T, T> { using type = void; }; |
| 25 | |
| 26 | template<typename T, typename U> |
| 27 | typename require_same<T, U>::type |
| 28 | check_type(U&) { } |
| 29 | |
| 30 | struct MoveOnly |
| 31 | { |
| 32 | MoveOnly() = default; |
| 33 | MoveOnly(MoveOnly&&); |
| 34 | MoveOnly& operator=(MoveOnly&&); |
| 35 | }; |
| 36 | |
| 37 | void |
| 38 | test00() |
| 39 | { |
| 40 | std::tuple x; |
| 41 | check_type<std::tuple<>>(x); |
| 42 | |
| 43 | std::tuple copy = x; |
| 44 | check_type<decltype(x)>(copy); |
| 45 | std::tuple move = std::move(x); |
| 46 | check_type<decltype(x)>(move); |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | test01() |
| 51 | { |
| 52 | std::tuple x = 5; |
| 53 | check_type<std::tuple<int>>(x); |
| 54 | int y = 42; |
| 55 | std::tuple x2 = y; |
| 56 | check_type<std::tuple<int>>(x2); |
| 57 | const int z = 666; |
| 58 | std::tuple x3 = z; |
| 59 | check_type<std::tuple<int>>(x3); |
| 60 | std::tuple mo = MoveOnly(); |
| 61 | check_type<std::tuple<MoveOnly>>(mo); |
| 62 | mo = MoveOnly(); |
| 63 | |
| 64 | std::tuple copy = x; |
| 65 | check_type<decltype(x)>(copy); |
| 66 | std::tuple move = std::move(mo); |
| 67 | check_type<decltype(mo)>(move); |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | test02() |
| 72 | { |
| 73 | std::tuple x{5, 6u}; |
| 74 | check_type<std::tuple<int, unsigned>>(x); |
| 75 | int y = 42; |
| 76 | std::tuple x2{y, 48u}; |
| 77 | check_type<std::tuple<int, unsigned>>(x2); |
| 78 | const int z = 666; |
| 79 | std::tuple x3{z, y}; |
| 80 | check_type<std::tuple<int, int>>(x3); |
| 81 | std::tuple x4{1, x}; |
| 82 | check_type<std::tuple<int, decltype(x)>>(x4); |
| 83 | std::tuple mo{MoveOnly(), 2l}; |
| 84 | check_type<std::tuple<MoveOnly, long>>(mo); |
| 85 | mo = {MoveOnly(), 3l}; |
| 86 | |
| 87 | std::tuple copy = x; |
| 88 | check_type<decltype(x)>(copy); |
| 89 | std::tuple copy2{x}; |
| 90 | check_type<decltype(x)>(copy2); |
| 91 | std::tuple move = std::move(mo); |
| 92 | check_type<decltype(mo)>(move); |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | test03() |
| 97 | { |
| 98 | std::tuple x{5, 6u, '7'}; |
| 99 | check_type<std::tuple<int, unsigned, char>>(x); |
| 100 | int y = 42; |
| 101 | std::tuple x2{y, 48u, 54l}; |
| 102 | check_type<std::tuple<int, unsigned, long>>(x2); |
| 103 | const int z = 666; |
| 104 | std::tuple x3{z, y, x}; |
| 105 | check_type<std::tuple<int, int, decltype(x)>>(x3); |
| 106 | std::tuple x4{1, x, x2}; |
| 107 | check_type<std::tuple<int, decltype(x), decltype(x2)>>(x4); |
| 108 | std::tuple mo{MoveOnly(), 2l}; |
| 109 | check_type<std::tuple<MoveOnly, long>>(mo); |
| 110 | mo = {MoveOnly(), 3l}; |
| 111 | |
| 112 | std::tuple copy = x; |
| 113 | check_type<decltype(x)>(copy); |
| 114 | std::tuple copy2{x}; |
| 115 | check_type<decltype(x)>(copy2); |
| 116 | std::tuple move = std::move(mo); |
| 117 | check_type<decltype(mo)>(move); |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | test04() |
| 122 | { |
| 123 | std::pair<int, unsigned> p; |
| 124 | std::tuple x = p; |
| 125 | check_type<std::tuple<int, unsigned>>(x); |
| 126 | std::tuple x2{p}; |
| 127 | check_type<std::tuple<int, unsigned>>(x2); |
| 128 | std::pair<const int, unsigned> p2; |
| 129 | std::tuple x3{p2}; |
| 130 | check_type<std::tuple<const int, unsigned>>(x3); |
| 131 | std::pair<int&, const unsigned&> p3{p.first, p.second}; |
| 132 | std::tuple x4{p3}; |
| 133 | check_type<std::tuple<int&, const unsigned&>>(x4); |
| 134 | std::tuple mo = std::pair<MoveOnly, MoveOnly>(); |
| 135 | check_type<std::tuple<MoveOnly, MoveOnly>>(mo); |
| 136 | |
| 137 | std::tuple copy = x4; |
| 138 | check_type<decltype(x4)>(copy); |
| 139 | std::tuple copy2{x4}; |
| 140 | check_type<decltype(x4)>(copy2); |
| 141 | std::tuple move = std::move(mo); |
| 142 | check_type<decltype(mo)>(move); |
| 143 | } |
| 144 | |
| 145 | void |
| 146 | test05() |
| 147 | { |
| 148 | #if __STDC_HOSTED__ |
| 149 | std::allocator<double> a; |
| 150 | std::tuple x{std::allocator_arg, a, 1}; |
| 151 | check_type<std::tuple<int>>(x); |
| 152 | std::tuple x2{std::allocator_arg, a, 1, '2'}; |
| 153 | check_type<std::tuple<int, char>>(x2); |
| 154 | |
| 155 | std::pair<float, const short> p{}; |
| 156 | std::tuple x3{std::allocator_arg, a, p}; |
| 157 | check_type<std::tuple<float, const short>>(x3); |
| 158 | std::tuple x4{std::allocator_arg, a, std::move(p)}; |
| 159 | check_type<std::tuple<float, const short>>(x4); |
| 160 | |
| 161 | std::tuple x5{std::allocator_arg, a, x}; |
| 162 | check_type<decltype(x)>(x5); |
| 163 | std::tuple x6{std::allocator_arg, a, std::move(x)}; |
| 164 | check_type<decltype(x)>(x6); |
| 165 | #endif |
| 166 | } |