| 1 | // { dg-do run { target c++11 } } |
| 2 | // { dg-require-effective-target hosted } |
| 3 | |
| 4 | // Copyright (C) 2008-2025 Free Software Foundation, Inc. |
| 5 | // |
| 6 | // This file is part of the GNU ISO C++ Library. This library is free |
| 7 | // software; you can redistribute it and/or modify it under the |
| 8 | // terms of the GNU General Public License as published by the |
| 9 | // Free Software Foundation; either version 3, or (at your option) |
| 10 | // any later version. |
| 11 | |
| 12 | // This library is distributed in the hope that it will be useful, |
| 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | // GNU General Public License for more details. |
| 16 | |
| 17 | // You should have received a copy of the GNU General Public License along |
| 18 | // with this library; see the file COPYING3. If not see |
| 19 | // <http://www.gnu.org/licenses/>. |
| 20 | |
| 21 | // 20.8.13.2 Template class shared_ptr [util.smartptr.shared] |
| 22 | |
| 23 | #include <memory> |
| 24 | #include <testsuite_hooks.h> |
| 25 | |
| 26 | struct A { }; |
| 27 | |
| 28 | namespace std |
| 29 | { |
| 30 | template<> |
| 31 | struct less<A*> |
| 32 | { |
| 33 | static int count; |
| 34 | bool operator()(A* l, A* r) { ++count; return l < r; } |
| 35 | }; |
| 36 | int less<A*>::count = 0; |
| 37 | } |
| 38 | |
| 39 | // 20.8.13.2.7 shared_ptr comparison [util.smartptr.shared.cmp] |
| 40 | |
| 41 | |
| 42 | int |
| 43 | test01() |
| 44 | { |
| 45 | std::less<std::shared_ptr<A>> less; |
| 46 | // test empty shared_ptrs compare equivalent |
| 47 | std::shared_ptr<A> p1; |
| 48 | std::shared_ptr<A> p2; |
| 49 | VERIFY( !less(p1, p2) && !less(p2, p1) ); |
| 50 | #ifndef __cpp_lib_three_way_comparison |
| 51 | // In C++20 std::less<std::shared_ptr<A>> uses the operator< synthesized |
| 52 | // from operator<=>, which uses std::compare_three_way not std::less<A*>. |
| 53 | VERIFY( std::less<A*>::count == 2 ); |
| 54 | #endif |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | // Construction from pointer |
| 60 | int |
| 61 | test02() |
| 62 | { |
| 63 | std::less<std::shared_ptr<A>> less; |
| 64 | |
| 65 | std::shared_ptr<A> empty; |
| 66 | std::shared_ptr<A> p1(new A); |
| 67 | std::shared_ptr<A> p2(new A); |
| 68 | |
| 69 | VERIFY( less(p1, p2) || less(p2, p1) ); |
| 70 | VERIFY( !(less(p1, p2) && less(p2, p1)) ); |
| 71 | |
| 72 | p1.reset(); |
| 73 | VERIFY( !less(p1, empty) && !less(empty, p1) ); |
| 74 | |
| 75 | p2.reset(); |
| 76 | VERIFY( !less(p1, p2) && !less(p2, p1) ); |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | // Aliasing |
| 82 | int |
| 83 | test03() |
| 84 | { |
| 85 | std::less<std::shared_ptr<A>> less; |
| 86 | |
| 87 | A a; |
| 88 | std::shared_ptr<A> p1(new A); |
| 89 | std::shared_ptr<A> p2(p1, &a); |
| 90 | VERIFY( less(p1, p2) || less(p2, p1) ); |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | int |
| 95 | main() |
| 96 | { |
| 97 | test01(); |
| 98 | test02(); |
| 99 | test03(); |
| 100 | return 0; |
| 101 | } |