pointer.pass.cpp 1.4 KB

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