| 1 | // Copyright (C) 2015-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-do run { target c++14 } } |
| 19 | |
| 20 | #include <experimental/any> |
| 21 | #include <set> |
| 22 | #include <testsuite_hooks.h> |
| 23 | |
| 24 | std::set<const void*> live_objects; |
| 25 | |
| 26 | struct A { |
| 27 | A() { live_objects.insert(this); } |
| 28 | ~A() { live_objects.erase(this); } |
| 29 | A(const A& a) { VERIFY(live_objects.count(&a)); live_objects.insert(this); } |
| 30 | }; |
| 31 | |
| 32 | void |
| 33 | test01() |
| 34 | { |
| 35 | using std::experimental::any; |
| 36 | |
| 37 | any a; |
| 38 | a = a; |
| 39 | VERIFY( a.empty() ); |
| 40 | |
| 41 | a = A{}; |
| 42 | a = a; |
| 43 | VERIFY( !a.empty() ); |
| 44 | |
| 45 | a.clear(); |
| 46 | VERIFY( live_objects.empty() ); |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | test02() |
| 51 | { |
| 52 | using std::experimental::any; |
| 53 | |
| 54 | struct X { |
| 55 | any a; |
| 56 | }; |
| 57 | |
| 58 | X x; |
| 59 | std::swap(x, x); // results in "self-move-assignment" of X::a |
| 60 | VERIFY( x.a.empty() ); |
| 61 | |
| 62 | x.a = A{}; |
| 63 | std::swap(x, x); // results in "self-move-assignment" of X::a |
| 64 | VERIFY( !x.a.empty() ); |
| 65 | |
| 66 | x.a.clear(); |
| 67 | VERIFY( live_objects.empty() ); |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | test03() |
| 72 | { |
| 73 | using std::experimental::any; |
| 74 | |
| 75 | any a; |
| 76 | a.swap(a); |
| 77 | VERIFY( a.empty() ); |
| 78 | |
| 79 | a = A{}; |
| 80 | a.swap(a); |
| 81 | VERIFY( !a.empty() ); |
| 82 | |
| 83 | a.clear(); |
| 84 | VERIFY( live_objects.empty() ); |
| 85 | } |
| 86 | |
| 87 | int |
| 88 | main() |
| 89 | { |
| 90 | test01(); |
| 91 | test02(); |
| 92 | test03(); |
| 93 | } |