| 1 | // Copyright (C) 2019-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++11 } } |
| 19 | |
| 20 | #include <memory> |
| 21 | |
| 22 | template<typename Func, typename Arg, typename = void> |
| 23 | struct is_callable |
| 24 | : std::false_type |
| 25 | { }; |
| 26 | |
| 27 | template<typename Func, typename Arg> |
| 28 | struct is_callable<Func, Arg, |
| 29 | decltype((void)(std::declval<Func&>()(std::declval<Arg>())))> |
| 30 | : std::true_type |
| 31 | { }; |
| 32 | |
| 33 | void |
| 34 | test01() |
| 35 | { |
| 36 | struct D { |
| 37 | struct pointer { }; |
| 38 | void operator()(pointer) const noexcept { } |
| 39 | }; |
| 40 | static_assert( !is_callable<std::hash<D::pointer>&, D::pointer>::value ); |
| 41 | |
| 42 | using UP = std::unique_ptr<int, D>; |
| 43 | // [unord.hash] |
| 44 | // Disabled specializations of hash are not function object types |
| 45 | static_assert( !is_callable<std::hash<UP>&, UP>::value ); |
| 46 | static_assert( !is_callable<std::hash<UP>&, UP&>::value ); |
| 47 | static_assert( !is_callable<std::hash<UP>&, const UP&>::value ); |
| 48 | } |
| 49 | |
| 50 | struct D { |
| 51 | struct pointer { }; |
| 52 | void operator()(pointer) const noexcept { } |
| 53 | }; |
| 54 | |
| 55 | bool operator==(D::pointer, std::nullptr_t) { return false; } |
| 56 | bool operator!=(D::pointer, std::nullptr_t) { return true; } |
| 57 | |
| 58 | namespace std { |
| 59 | template<> struct hash<D::pointer> { |
| 60 | size_t operator()(D::pointer) const { throw 1; } |
| 61 | }; |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | test02() |
| 66 | { |
| 67 | using UP = std::unique_ptr<int, D>; |
| 68 | UP p; |
| 69 | std::hash<UP> h; |
| 70 | try { |
| 71 | // [util.smartptr.hash] |
| 72 | // The member functions are not guaranteed to be noexcept. |
| 73 | h(p); |
| 74 | throw "should not reach here"; |
| 75 | } catch (int) { |
| 76 | // Should catch exception here, rather than terminating. |
| 77 | } |
| 78 | |
| 79 | // Should still be noexcept if the underlying hash object is: |
| 80 | using UP2 = std::unique_ptr<int>; |
| 81 | UP2 p2; |
| 82 | std::hash<UP2> h2; |
| 83 | static_assert( noexcept(h2(p2)), "operator() is noexcept" ); |
| 84 | } |
| 85 | |
| 86 | int |
| 87 | main() |
| 88 | { |
| 89 | test02(); |
| 90 | } |