blob: 4a0bf6d037067ed38a5ef59642c1701e718b4e88 [file] [log] [blame]
Eric Fiseliera9e65962016-10-12 07:46:20 +00001//===----------------------------------------------------------------------===//
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
Eric Fiselierf9127592017-01-21 00:02:12 +000020#include "poisoned_hash_helper.hpp"
21
22struct A {};
23struct B {};
24
Michael Park41c4de42017-03-23 06:21:24 +000025namespace std {
26
Eric Fiselierf9127592017-01-21 00:02:12 +000027template <>
Michael Park41c4de42017-03-23 06:21:24 +000028struct hash<B> {
Billy Robert O'Neal III8ac1fa12017-04-06 23:50:33 +000029 size_t operator()(B const&) noexcept(false) { return 0; }
Eric Fiselierf9127592017-01-21 00:02:12 +000030};
Eric Fiseliera9e65962016-10-12 07:46:20 +000031
Michael Park41c4de42017-03-23 06:21:24 +000032}
33
Eric Fiseliera9e65962016-10-12 07:46:20 +000034int main()
35{
36 using std::optional;
37 const std::size_t nullopt_hash =
38 std::hash<optional<double>>{}(optional<double>{});
39
40 {
Billy Robert O'Neal IIIeaeeaaf2017-04-06 23:50:21 +000041 optional<B> opt;
42 ASSERT_NOT_NOEXCEPT(std::hash<optional<B>>()(opt));
43 ASSERT_NOT_NOEXCEPT(std::hash<optional<const B>>()(opt));
44 }
45
46 {
Eric Fiseliera9e65962016-10-12 07:46:20 +000047 typedef int T;
48 optional<T> opt;
49 assert(std::hash<optional<T>>{}(opt) == nullopt_hash);
50 opt = 2;
51 assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt));
52 }
53 {
54 typedef std::string T;
55 optional<T> opt;
56 assert(std::hash<optional<T>>{}(opt) == nullopt_hash);
57 opt = std::string("123");
58 assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt));
59 }
60 {
61 typedef std::unique_ptr<int> T;
62 optional<T> opt;
63 assert(std::hash<optional<T>>{}(opt) == nullopt_hash);
64 opt = std::unique_ptr<int>(new int(3));
65 assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt));
66 }
Eric Fiselierf9127592017-01-21 00:02:12 +000067 {
68 test_hash_enabled_for_type<std::optional<int> >();
69 test_hash_enabled_for_type<std::optional<int*> >();
70 test_hash_enabled_for_type<std::optional<const int> >();
71 test_hash_enabled_for_type<std::optional<int* const> >();
72
73 test_hash_disabled_for_type<std::optional<A>>();
74 test_hash_disabled_for_type<std::optional<const A>>();
75
76 test_hash_enabled_for_type<std::optional<B>>();
77 test_hash_enabled_for_type<std::optional<const B>>();
78 }
Eric Fiseliera9e65962016-10-12 07:46:20 +000079}