1 | //===----------------------------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS |
10 | |
11 | // <functional> |
12 | |
13 | // template <class T> |
14 | // struct hash |
15 | // : public unary_function<T, size_t> |
16 | // { |
17 | // size_t operator()(T val) const; |
18 | // }; |
19 | |
20 | // Not very portable |
21 | |
22 | #include <functional> |
23 | #include <cassert> |
24 | #include <type_traits> |
25 | #include <limits> |
26 | |
27 | #include "test_macros.h" |
28 | |
29 | template <class T> |
30 | void |
31 | test() |
32 | { |
33 | typedef std::hash<T> H; |
34 | #if TEST_STD_VER <= 17 |
35 | static_assert((std::is_same<typename H::argument_type, T>::value), "" ); |
36 | static_assert((std::is_same<typename H::result_type, std::size_t>::value), "" ); |
37 | #endif |
38 | ASSERT_NOEXCEPT(H()(T())); |
39 | H h; |
40 | |
41 | typedef typename std::remove_pointer<T>::type type; |
42 | type i; |
43 | type j; |
44 | assert(h(&i) != h(&j)); |
45 | } |
46 | |
47 | // can't hash nullptr_t until C++17 |
48 | void test_nullptr() |
49 | { |
50 | #if TEST_STD_VER > 14 |
51 | typedef std::nullptr_t T; |
52 | typedef std::hash<T> H; |
53 | #if TEST_STD_VER <= 17 |
54 | static_assert((std::is_same<typename H::argument_type, T>::value), "" ); |
55 | static_assert((std::is_same<typename H::result_type, std::size_t>::value), "" ); |
56 | #endif |
57 | ASSERT_NOEXCEPT(H()(T())); |
58 | #endif |
59 | } |
60 | |
61 | int main(int, char**) |
62 | { |
63 | test<int*>(); |
64 | test_nullptr(); |
65 | |
66 | return 0; |
67 | } |
68 | |