Eric Fiselier | a9e6596 | 2016-10-12 07:46:20 +0000 | [diff] [blame] | 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 | |
| 21 | int main() |
| 22 | { |
| 23 | using std::optional; |
| 24 | const std::size_t nullopt_hash = |
| 25 | std::hash<optional<double>>{}(optional<double>{}); |
| 26 | |
| 27 | { |
| 28 | typedef int T; |
| 29 | optional<T> opt; |
| 30 | assert(std::hash<optional<T>>{}(opt) == nullopt_hash); |
| 31 | opt = 2; |
| 32 | assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt)); |
| 33 | } |
| 34 | { |
| 35 | typedef std::string T; |
| 36 | optional<T> opt; |
| 37 | assert(std::hash<optional<T>>{}(opt) == nullopt_hash); |
| 38 | opt = std::string("123"); |
| 39 | assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt)); |
| 40 | } |
| 41 | { |
| 42 | typedef std::unique_ptr<int> T; |
| 43 | optional<T> opt; |
| 44 | assert(std::hash<optional<T>>{}(opt) == nullopt_hash); |
| 45 | opt = std::unique_ptr<int>(new int(3)); |
| 46 | assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt)); |
| 47 | } |
| 48 | } |