• 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 // UNSUPPORTED: c++98, c++03, c++11, c++14
11 // <optional>
12 
13 // template <class T> struct hash<optional<T>>;
14 
15 #include <optional>
16 #include <string>
17 #include <memory>
18 #include <cassert>
19 
20 #include "poisoned_hash_helper.hpp"
21 
22 struct A {};
23 struct B {};
24 
25 template <>
26 struct std::hash<B> {
operator ()std::hash27   size_t operator()(B const&) { return 0; }
28 };
29 
main()30 int main()
31 {
32     using std::optional;
33     const std::size_t nullopt_hash =
34         std::hash<optional<double>>{}(optional<double>{});
35 
36     {
37         typedef int T;
38         optional<T> opt;
39         assert(std::hash<optional<T>>{}(opt) == nullopt_hash);
40         opt = 2;
41         assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt));
42     }
43     {
44         typedef std::string T;
45         optional<T> opt;
46         assert(std::hash<optional<T>>{}(opt) == nullopt_hash);
47         opt = std::string("123");
48         assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt));
49     }
50     {
51         typedef std::unique_ptr<int> T;
52         optional<T> opt;
53         assert(std::hash<optional<T>>{}(opt) == nullopt_hash);
54         opt = std::unique_ptr<int>(new int(3));
55         assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt));
56     }
57     {
58       test_hash_enabled_for_type<std::optional<int> >();
59       test_hash_enabled_for_type<std::optional<int*> >();
60       test_hash_enabled_for_type<std::optional<const int> >();
61       test_hash_enabled_for_type<std::optional<int* const> >();
62 
63       test_hash_disabled_for_type<std::optional<A>>();
64       test_hash_disabled_for_type<std::optional<const A>>();
65 
66       test_hash_enabled_for_type<std::optional<B>>();
67       test_hash_enabled_for_type<std::optional<const B>>();
68     }
69 }
70