hash.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include <cxxomfort/cxxomfort.hpp>
  2. #include <cxxomfort/functional.hpp> // hash
  3. #include <cxxomfort/string.hpp>
  4. #include <cxxomfort/library/functional.hpp>
  5. #include <cxxomfort/library/string.hpp>
  6. #include <cxxomfort/library/type_name.hpp>
  7. #include <functional>
  8. #include <iostream>
  9. template <typename T>
  10. void print_hash (T const& t) {
  11. using namespace std;
  12. hash<T> H;
  13. //cout<< "hash is: "<< cxxomfort::typeid_demangle(typeid(H))<< " "<< H.uses_std<< " \n";
  14. cout<< cxxomfort::library::type_name<T>()<< " -> "<< H(t)<< endl;
  15. }
  16. template <typename T>
  17. void print_xhash (T const& t) {
  18. using cxxomfort::fix::hash;
  19. using namespace std;
  20. hash<T> H;
  21. //cout<< "hash is: "<< cxxomfort::typeid_demangle(typeid(H))<< " "<< H.uses_std<< " \n";
  22. cout<< cxxomfort::library::type_name<T>()<< " -> "<< H(t)<< endl;
  23. }
  24. // of note, c++20 adds this:
  25. // https://en.cppreference.com/w/cpp/memory/unique_ptr/operator_ltlt
  26. template <typename Ch, typename T, typename D>
  27. std::basic_ostream<Ch>& operator<< (std::basic_ostream<Ch> &os, std::unique_ptr<T,D> const& p) CXXO_NOEXCEPTNOTHROW {
  28. void* const x = static_cast<void* const>(p.get());
  29. return os<< x;
  30. }
  31. enum enum_foo_t { eval1, eval2, eval3 };
  32. int main() {
  33. using namespace std;
  34. namespace CLF = cxxomfort::library::functional;
  35. cxxomfort::output_info(); cout<< "\n"<< endl;
  36. cout<< "**Demonstration of std::hash< T >\n";
  37. cout<< "hash< type > : type value -> hash value\n"<< endl;
  38. // c++03
  39. cout<< "std::hash for native types and integers: \n"<< endl;
  40. print_xhash<signed char>('c');
  41. print_xhash<int>(100);
  42. print_xhash<float>(-4.99);
  43. print_xhash<uintmax_t>(1234567890123456789);
  44. print_xhash<long*>(reinterpret_cast<long*>(0x33dd));
  45. print_xhash<double*>(reinterpret_cast<double*>(0x0));
  46. // c++11
  47. cout<< "std::hash spec-ions introduced in C++11: \n"<< endl;
  48. cout<< "char(n) types: \n";
  49. print_xhash<char16_t>('S');
  50. print_xhash<char32_t>('L');
  51. cout<< "string types: \n";
  52. print_xhash<string>("Hello world");
  53. print_xhash<wstring>(L"Hello wide world");
  54. print_xhash<u32string>(u32string(17, 0x2322));
  55. cout<< "others: \n";
  56. unique_ptr<long> u (new long(0x133));
  57. print_xhash< unique_ptr<long> >( u );
  58. // print_hash< error_code > (e);
  59. cout<< "\n" "std::hash spec-ions introduced in C++14:"<< endl;
  60. typedef underlying_type<enum_foo_t>::type enum_foo_t_ut;
  61. print_xhash<enum_foo_t>(eval2);
  62. print_xhash<enum_foo_t_ut>(0);
  63. cout<< endl;
  64. cout<< is_same<uint16_t,char16_t>::value<< endl;
  65. }