• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <functional>
11 
12 // template <class T>
13 // struct hash
14 //     : public unary_function<T, size_t>
15 // {
16 //     size_t operator()(T val) const;
17 // };
18 
19 // Not very portable
20 
21 #include <functional>
22 #include <cassert>
23 #include <type_traits>
24 #include <limits>
25 
26 #include "test_macros.h"
27 
28 template <class T>
29 void
test()30 test()
31 {
32     typedef std::hash<T> H;
33     static_assert((std::is_same<typename H::argument_type, T>::value), "" );
34     static_assert((std::is_same<typename H::result_type, std::size_t>::value), "" );
35     ASSERT_NOEXCEPT(H()(T()));
36     H h;
37 
38     typedef typename std::remove_pointer<T>::type type;
39     type i;
40     type j;
41     assert(h(&i) != h(&j));
42 }
43 
44 // can't hash nullptr_t until C++17
test_nullptr()45 void test_nullptr()
46 {
47 #if TEST_STD_VER > 14
48     typedef std::nullptr_t T;
49     typedef std::hash<T> H;
50     static_assert((std::is_same<typename H::argument_type, T>::value), "" );
51     static_assert((std::is_same<typename H::result_type, std::size_t>::value), "" );
52     ASSERT_NOEXCEPT(H()(T()));
53 #endif
54 }
55 
main()56 int main()
57 {
58     test<int*>();
59     test_nullptr();
60 }
61