80 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++1410// <optional>11 12// template <class T> struct hash<optional<T>>;13 14#include <optional>15#include <string>16#include <memory>17#include <cassert>18 19#include "poisoned_hash_helper.h"20 21#include "test_macros.h"22 23struct A {};24struct B {};25 26template <>27struct std::hash<B> {28 std::size_t operator()(B const&) noexcept(false) { return 0; }29};30 31int main(int, char**)32{33 using std::optional;34 const std::size_t nullopt_hash =35 std::hash<optional<double>>{}(optional<double>{});36 37 38 {39 optional<B> opt;40 ASSERT_NOT_NOEXCEPT(std::hash<optional<B>>()(opt));41 ASSERT_NOT_NOEXCEPT(std::hash<optional<const B>>()(opt));42 }43 44 {45 typedef int T;46 optional<T> opt;47 assert(std::hash<optional<T>>{}(opt) == nullopt_hash);48 opt = 2;49 assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt));50 }51 {52 typedef std::string T;53 optional<T> opt;54 assert(std::hash<optional<T>>{}(opt) == nullopt_hash);55 opt = std::string("123");56 assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt));57 }58 {59 typedef std::unique_ptr<int> T;60 optional<T> opt;61 assert(std::hash<optional<T>>{}(opt) == nullopt_hash);62 opt = std::unique_ptr<int>(new int(3));63 assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt));64 }65 {66 test_hash_enabled<std::optional<int> >();67 test_hash_enabled<std::optional<int*> >();68 test_hash_enabled<std::optional<const int> >();69 test_hash_enabled<std::optional<int* const> >();70 71 test_hash_disabled<std::optional<A>>();72 test_hash_disabled<std::optional<const A>>();73 74 test_hash_enabled<std::optional<B>>();75 test_hash_enabled<std::optional<const B>>();76 }77 78 return 0;79}80