hash.pass.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. // UNSUPPORTED: c++98, c++03, c++11, c++14
  10. // <optional>
  11. // template <class T> struct hash<optional<T>>;
  12. #include <optional>
  13. #include <string>
  14. #include <memory>
  15. #include <cassert>
  16. int main()
  17. {
  18. using std::optional;
  19. const std::size_t nullopt_hash =
  20. std::hash<optional<double>>{}(optional<double>{});
  21. {
  22. typedef int T;
  23. optional<T> opt;
  24. assert(std::hash<optional<T>>{}(opt) == nullopt_hash);
  25. opt = 2;
  26. assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt));
  27. }
  28. {
  29. typedef std::string T;
  30. optional<T> opt;
  31. assert(std::hash<optional<T>>{}(opt) == nullopt_hash);
  32. opt = std::string("123");
  33. assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt));
  34. }
  35. {
  36. typedef std::unique_ptr<int> T;
  37. optional<T> opt;
  38. assert(std::hash<optional<T>>{}(opt) == nullopt_hash);
  39. opt = std::unique_ptr<int>(new int(3));
  40. assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt));
  41. }
  42. }