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 // <functional> 10 11 // template <class T> 12 // struct hash 13 // : public unary_function<T, size_t> 14 // { 15 // size_t operator()(T val) const; 16 // }; 17 18 // Not very portable 19 20 #include <functional> 21 #include <cassert> 22 #include <type_traits> 23 #include <limits> 24 25 #include "test_macros.h" 26 27 template <class T> 28 void 29 test() 30 { 31 typedef std::hash<T> H; 32 static_assert((std::is_same<typename H::argument_type, T>::value), "" ); 33 static_assert((std::is_same<typename H::result_type, std::size_t>::value), "" ); 34 ASSERT_NOEXCEPT(H()(T())); 35 H h; 36 37 typedef typename std::remove_pointer<T>::type type; 38 type i; 39 type j; 40 assert(h(&i) != h(&j)); 41 } 42 43 // can't hash nullptr_t until C++17 44 void test_nullptr() 45 { 46 #if TEST_STD_VER > 14 47 typedef std::nullptr_t T; 48 typedef std::hash<T> H; 49 static_assert((std::is_same<typename H::argument_type, T>::value), "" ); 50 static_assert((std::is_same<typename H::result_type, std::size_t>::value), "" ); 51 ASSERT_NOEXCEPT(H()(T())); 52 #endif 53 } 54 55 int main(int, char**) 56 { 57 test<int*>(); 58 test_nullptr(); 59 60 return 0; 61 } 62