blob: ea89dc3bd9278a50cc4b601feece8d14e11c1037 [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
25template <>
26struct std::hash<B> {
27 size_t operator()(B const&) { return 0; }
28};
Eric Fiseliera9e65962016-10-12 07:46:20 +000029
30int 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 }
Eric Fiselierf9127592017-01-21 00:02:12 +000057 {
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 }
Eric Fiseliera9e65962016-10-12 07:46:20 +000069}