| 1 | // { dg-do run { target c++11 } } |
| 2 | |
| 3 | // Copyright (C) 2007-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 | |
| 21 | // NOTE: This makes use of the fact that we know how moveable |
| 22 | // is implemented on tuple. If the implementation changed |
| 23 | // this test may begin to fail. |
| 24 | |
| 25 | #include <tuple> |
| 26 | #include <utility> |
| 27 | #include <testsuite_hooks.h> |
| 28 | |
| 29 | struct MoveOnly |
| 30 | { |
| 31 | explicit MoveOnly (int j) : i(j) { } |
| 32 | |
| 33 | MoveOnly (MoveOnly&& m) : i(m.i) { } |
| 34 | |
| 35 | MoveOnly& operator=(MoveOnly&& m) |
| 36 | { i = m.i; return *this; } |
| 37 | |
| 38 | MoveOnly(MoveOnly const&) = delete; |
| 39 | MoveOnly& operator=(MoveOnly const&) = delete; |
| 40 | |
| 41 | bool operator==(MoveOnly const& m) const |
| 42 | { return i == m.i; } |
| 43 | |
| 44 | void swap(MoveOnly& m) |
| 45 | { std::swap(m.i, i); } |
| 46 | |
| 47 | int i; |
| 48 | }; |
| 49 | |
| 50 | void swap(MoveOnly& m1, MoveOnly& m2) |
| 51 | { m1.swap(m2); } |
| 52 | |
| 53 | MoveOnly |
| 54 | make_move_only (int i) |
| 55 | { return MoveOnly(i); } |
| 56 | |
| 57 | void test01() |
| 58 | { |
| 59 | std::tuple<> t1, t2; |
| 60 | std::swap(t1, t2); |
| 61 | |
| 62 | VERIFY( t1 == t2 ); |
| 63 | } |
| 64 | |
| 65 | void test02() |
| 66 | { |
| 67 | std::tuple<int> t1(1), t2(2); |
| 68 | std::swap(t1, t2); |
| 69 | |
| 70 | VERIFY( std::get<0>(t1) == 2 && std::get<0>(t2) == 1 ); |
| 71 | } |
| 72 | |
| 73 | void test03() |
| 74 | { |
| 75 | std::tuple<int, float> t1(1, 1.0f), t2(2, 2.0f); |
| 76 | std::swap(t1, t2); |
| 77 | |
| 78 | VERIFY( std::get<0>(t1) == 2 && std::get<0>(t2) == 1 ); |
| 79 | VERIFY( std::get<1>(t1) == 2.0f && std::get<1>(t2) == 1.0f ); |
| 80 | } |
| 81 | |
| 82 | void test04() |
| 83 | { |
| 84 | std::tuple<int, float, MoveOnly> |
| 85 | t1(1, 1.0f, make_move_only(1)), |
| 86 | t2(2, 2.0f, make_move_only(2)); |
| 87 | |
| 88 | std::swap(t1, t2); |
| 89 | |
| 90 | VERIFY( std::get<0>(t1) == 2 && std::get<0>(t2) == 1 ); |
| 91 | VERIFY( std::get<1>(t1) == 2.0f && std::get<1>(t2) == 1.0f ); |
| 92 | VERIFY( std::get<2>(t1) == make_move_only(2) |
| 93 | && std::get<2>(t2) == make_move_only(1) ); |
| 94 | } |
| 95 | |
| 96 | int main() |
| 97 | { |
| 98 | test01(); |
| 99 | test02(); |
| 100 | test03(); |
| 101 | test04(); |
| 102 | return 0; |
| 103 | } |