| 1 | // Copyright (C) 2016-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 compile { target c++11 } } |
| 19 | // { dg-require-effective-target hosted } |
| 20 | |
| 21 | #include <memory> |
| 22 | |
| 23 | template<typename T, typename Args, typename = void> |
| 24 | struct resettable |
| 25 | : std::false_type |
| 26 | { }; |
| 27 | |
| 28 | template<typename... T> struct type_list { }; |
| 29 | |
| 30 | template<typename T, typename... Args> |
| 31 | using reset_result |
| 32 | = decltype(std::shared_ptr<T>{}.reset(std::declval<Args>()...)); |
| 33 | |
| 34 | template<typename T, typename... Args> |
| 35 | struct resettable<T, type_list<Args...>, reset_result<T, Args...>> |
| 36 | : std::true_type |
| 37 | { }; |
| 38 | |
| 39 | template<typename T, typename... Args> |
| 40 | constexpr bool can_reset() |
| 41 | { return resettable<T, type_list<Args...>>::value; } |
| 42 | |
| 43 | template<typename T> |
| 44 | struct Deleter { |
| 45 | void operator()(T*) const; |
| 46 | }; |
| 47 | |
| 48 | template<typename T> |
| 49 | using Alloc = std::allocator<T>; |
| 50 | |
| 51 | struct Base { }; |
| 52 | struct Derived : Base { }; |
| 53 | |
| 54 | // Positive cases: |
| 55 | |
| 56 | static_assert( can_reset<const void, void*>(), |
| 57 | "void* convertible to const void*"); |
| 58 | static_assert( can_reset<const int, int*>(), |
| 59 | "int* convertible to const int*"); |
| 60 | static_assert( can_reset<Base, Derived*>(), |
| 61 | "Derived* convertible to Base*"); |
| 62 | static_assert( can_reset<const Base, Derived*>(), |
| 63 | "Derived* convertible to const Base*"); |
| 64 | |
| 65 | // Negative cases: |
| 66 | |
| 67 | static_assert( !can_reset<int, void*>(), |
| 68 | "void* not convertible to int*"); |
| 69 | static_assert( !can_reset<int, void*, Deleter<int>>(), |
| 70 | "void* not convertible to int*"); |
| 71 | static_assert( !can_reset<int, void*, Deleter<int>, Alloc<int>>(), |
| 72 | "void* not convertible to int*"); |
| 73 | |
| 74 | static_assert( !can_reset<int, const int*>(), |
| 75 | "const int* not convertible to int*"); |
| 76 | static_assert( !can_reset<int, const int*, Deleter<int>>(), |
| 77 | "const int* not convertible to int*"); |
| 78 | static_assert( !can_reset<int, const int*, Deleter<int>, Alloc<int>>(), |
| 79 | "const int* not convertible to int*"); |
| 80 | |
| 81 | static_assert( !can_reset<int, long*>(), |
| 82 | "long* not convertible to int*"); |
| 83 | static_assert( !can_reset<int, long*, Deleter<int>>(), |
| 84 | "long* not convertible to int*"); |
| 85 | static_assert( !can_reset<int, long*, Deleter<int>, Alloc<int>>(), |
| 86 | "long* not convertible to int*"); |
| 87 | |
| 88 | static_assert( !can_reset<Derived, Base*>(), |
| 89 | "Base* not convertible to Derived*"); |
| 90 | static_assert( !can_reset<Derived, Base*, Deleter<int>>(), |
| 91 | "Base* not convertible to Derived*"); |
| 92 | static_assert( !can_reset<Derived, Base*, Deleter<int>, Alloc<int>>(), |
| 93 | "Base* not convertible to Derived*"); |