]>
Commit | Line | Data |
---|---|---|
6964bb3e | 1 | // { dg-options "-std=gnu++17" } |
6458742a | 2 | // { dg-do run { target c++17 } } |
6964bb3e | 3 | |
99dee823 | 4 | // Copyright (C) 2016-2021 Free Software Foundation, Inc. |
6964bb3e VV |
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 | ||
c7cbb4da | 17 | // You should have received a copy of the GNU General Public License along |
6964bb3e VV |
18 | // with this library; see the file COPYING3. If not see |
19 | // <http://www.gnu.org/licenses/>. | |
20 | ||
21 | #include <optional> | |
22 | #include <testsuite_hooks.h> | |
23 | ||
24 | class S{}; // No hash specialization | |
25 | ||
26 | template<class T> | |
27 | auto f(int) -> decltype(std::hash<std::optional<T>>(), std::true_type()); | |
28 | ||
29 | template<class T> | |
30 | auto f(...) -> decltype(std::false_type()); | |
31 | ||
32 | static_assert(!decltype(f<S>(0))::value, ""); | |
f6b05c44 JW |
33 | |
34 | template<typename T> | |
35 | constexpr bool hashable() | |
36 | { return std::is_invocable_v<std::hash<T>&, const T&>; } | |
37 | ||
38 | static_assert(!hashable<std::optional<S>>()); | |
39 | static_assert(!hashable<std::optional<const S>>()); | |
40 | static_assert(hashable<std::optional<int>>()); | |
41 | static_assert(hashable<std::optional<const int>>()); | |
6964bb3e VV |
42 | |
43 | int main() | |
44 | { | |
45 | int x = 42; | |
46 | std::optional<int> x2 = 42; | |
47 | VERIFY(std::hash<int>()(x) == std::hash<std::optional<int>>()(x2)); | |
f6b05c44 JW |
48 | |
49 | // PR libstdc++/82262 | |
50 | std::optional<const int> x3 = x2; | |
51 | VERIFY(std::hash<int>()(x) == std::hash<std::optional<const int>>()(x3)); | |
6964bb3e | 52 | } |