|
| 1 | +/* Copyright (C) 2019 Povilas Kanapickas <povilas@radix.lt> |
| 2 | + |
| 3 | + This file is part of cppreference-doc |
| 4 | + |
| 5 | + This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 |
| 6 | + Unported License. To view a copy of this license, visit |
| 7 | + http://creativecommons.org/licenses/by-sa/3.0/ or send a letter to Creative |
| 8 | + Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA. |
| 9 | + |
| 10 | + Permission is granted to copy, distribute and/or modify this document |
| 11 | + under the terms of the GNU Free Documentation License, Version 1.3 or |
| 12 | + any later version published by the Free Software Foundation; with no |
| 13 | + Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. |
| 14 | +*/ |
| 15 | + |
| 16 | +#ifndef CPPREFERENCE_OPTIONAL_H |
| 17 | +#define CPPREFERENCE_OPTIONAL_H |
| 18 | + |
| 19 | +#if CPPREFERENCE_STDVER >= 2017 |
| 20 | + |
| 21 | +#include <functional> // hash |
| 22 | +#include <utility> // in_place_t, ... |
| 23 | +#include <type_traits> // decay_t |
| 24 | +#include <exception> // exception |
| 25 | + |
| 26 | +namespace std { |
| 27 | + |
| 28 | +struct nullopt_t {}; |
| 29 | + |
| 30 | +inline constexpr nullopt_t nullopt; |
| 31 | + |
| 32 | +class bad_optional_access : public exception { |
| 33 | +public: |
| 34 | + ~bad_optional_access() override; |
| 35 | + const char * what() const override; |
| 36 | +}; |
| 37 | + |
| 38 | +template<class T> |
| 39 | +class optional { |
| 40 | +public: |
| 41 | + using value_type = T; |
| 42 | + |
| 43 | + constexpr optional(); |
| 44 | + constexpr optional(std::nullopt_t); |
| 45 | + constexpr optional(const optional& other); |
| 46 | + |
| 47 | + constexpr optional(optional&& other); |
| 48 | + |
| 49 | + template <class U> |
| 50 | + optional(const optional<U>& other); |
| 51 | + |
| 52 | + template<class U> |
| 53 | + optional(optional<U>&& other); |
| 54 | + |
| 55 | + template<class... Args> |
| 56 | + constexpr explicit optional(in_place_t, Args&&... args); |
| 57 | + |
| 58 | + template<class U, class... Args> |
| 59 | + constexpr explicit optional(in_place_t, |
| 60 | + initializer_list<U> ilist, |
| 61 | + Args&&... args); |
| 62 | + |
| 63 | + template<class U = value_type> |
| 64 | + constexpr optional(U&& value); |
| 65 | + |
| 66 | + ~optional(); |
| 67 | + |
| 68 | + optional& operator=(std::nullopt_t); |
| 69 | + |
| 70 | + constexpr optional& operator=(const optional& other); |
| 71 | + |
| 72 | + constexpr optional& operator=(optional&& other); |
| 73 | + |
| 74 | + template<class U = T> |
| 75 | + optional& operator=(U&& value); |
| 76 | + |
| 77 | + template<class U> |
| 78 | + optional& operator=(const optional<U>& other); |
| 79 | + |
| 80 | + template<class U> |
| 81 | + optional& operator=(optional<U>&& other); |
| 82 | + |
| 83 | + constexpr const T* operator->() const; |
| 84 | + |
| 85 | + constexpr T* operator->(); |
| 86 | + |
| 87 | + constexpr const T& operator*() const&; |
| 88 | + |
| 89 | + constexpr T& operator*() &; |
| 90 | + |
| 91 | + constexpr const T&& operator*() const&&; |
| 92 | + |
| 93 | + constexpr T&& operator*() &&; |
| 94 | + |
| 95 | + constexpr explicit operator bool() const; |
| 96 | + |
| 97 | + constexpr bool has_value() const; |
| 98 | + |
| 99 | + constexpr T& value() &; |
| 100 | + constexpr const T & value() const &; |
| 101 | + |
| 102 | + constexpr T&& value() &&; |
| 103 | + constexpr const T&& value() const &&; |
| 104 | + |
| 105 | + template< class U > |
| 106 | + constexpr T value_or(U&& default_value) const&; |
| 107 | + |
| 108 | + template< class U > |
| 109 | + constexpr T value_or(U&& default_value) &&; |
| 110 | + |
| 111 | + void swap(optional& other); |
| 112 | + |
| 113 | + void reset(); |
| 114 | + |
| 115 | + template<class... Args> |
| 116 | + T& emplace(Args&&... args); |
| 117 | + |
| 118 | + template<class U, class... Args> |
| 119 | + T& emplace(initializer_list<U> ilist, Args&&... args); |
| 120 | +}; |
| 121 | + |
| 122 | +template<class T, class U> |
| 123 | +constexpr bool operator==(const optional<T>& lhs, const optional<U>& rhs); |
| 124 | + |
| 125 | +template<class T, class U> |
| 126 | +constexpr bool operator!=(const optional<T>& lhs, const optional<U>& rhs); |
| 127 | + |
| 128 | +template<class T, class U> |
| 129 | +constexpr bool operator<(const optional<T>& lhs, const optional<U>& rhs); |
| 130 | + |
| 131 | +template<class T, class U> |
| 132 | +constexpr bool operator<=(const optional<T>& lhs, const optional<U>& rhs); |
| 133 | + |
| 134 | +template<class T, class U> |
| 135 | +constexpr bool operator>(const optional<T>& lhs, const optional<U>& rhs); |
| 136 | + |
| 137 | +template<class T, class U> |
| 138 | +constexpr bool operator>=(const optional<T>& lhs, const optional<U>& rhs); |
| 139 | + |
| 140 | +template<class T> |
| 141 | +constexpr bool operator==(const optional<T>& opt, nullopt_t); |
| 142 | + |
| 143 | +template<class T> |
| 144 | +constexpr bool operator==(nullopt_t, const optional<T>& opt); |
| 145 | + |
| 146 | +template<class T> |
| 147 | +constexpr bool operator!=(const optional<T>& opt, nullopt_t); |
| 148 | + |
| 149 | +template<class T> |
| 150 | +constexpr bool operator!=(nullopt_t, const optional<T>& opt); |
| 151 | + |
| 152 | +template<class T> |
| 153 | +constexpr bool operator<(const optional<T>& opt, nullopt_t); |
| 154 | + |
| 155 | +template<class T> |
| 156 | +constexpr bool operator<(nullopt_t, const optional<T>& opt); |
| 157 | + |
| 158 | +template<class T> |
| 159 | +constexpr bool operator<=(const optional<T>& opt, nullopt_t); |
| 160 | + |
| 161 | +template<class T> |
| 162 | +constexpr bool operator<=(nullopt_t, const optional<T>& opt); |
| 163 | + |
| 164 | +template<class T> |
| 165 | +constexpr bool operator>(const optional<T>& opt, nullopt_t); |
| 166 | + |
| 167 | +template<class T> |
| 168 | +constexpr bool operator>(nullopt_t, const optional<T>& opt); |
| 169 | + |
| 170 | +template<class T> |
| 171 | +constexpr bool operator>=(const optional<T>& opt, nullopt_t); |
| 172 | + |
| 173 | +template<class T> |
| 174 | +constexpr bool operator>=(nullopt_t, const optional<T>& opt); |
| 175 | + |
| 176 | + |
| 177 | +template<class T, class U> |
| 178 | +constexpr bool operator==(const optional<T>& opt, const U& value); |
| 179 | + |
| 180 | +template<class T, class U> |
| 181 | +constexpr bool operator==(const T& value, const optional<U>& opt); |
| 182 | + |
| 183 | +template<class T, class U> |
| 184 | +constexpr bool operator!=(const optional<T>& opt, const U& value); |
| 185 | + |
| 186 | +template<class T, class U> |
| 187 | +constexpr bool operator!=(const T& value, const optional<U>& opt); |
| 188 | + |
| 189 | +template<class T, class U> |
| 190 | +constexpr bool operator<(const optional<T>& opt, const U& value); |
| 191 | + |
| 192 | +template<class T, class U> |
| 193 | +constexpr bool operator<(const T& value, const optional<U>& opt); |
| 194 | + |
| 195 | +template<class T, class U> |
| 196 | +constexpr bool operator<=(const optional<T>& opt, const U& value); |
| 197 | + |
| 198 | +template<class T, class U> |
| 199 | +constexpr bool operator<=(const T& value, const optional<U>& opt); |
| 200 | + |
| 201 | +template<class T, class U> |
| 202 | +constexpr bool operator>(const optional<T>& opt, const U& value); |
| 203 | + |
| 204 | +template<class T, class U> |
| 205 | +constexpr bool operator>(const T& value, const optional<U>& opt); |
| 206 | + |
| 207 | +template<class T, class U> |
| 208 | +constexpr bool operator>=(const optional<T>& opt, const U& value); |
| 209 | + |
| 210 | +template<class T, class U> |
| 211 | +constexpr bool operator>=(const T& value, const optional<U>& opt); |
| 212 | + |
| 213 | +template<class T > |
| 214 | +constexpr optional<decay_t<T>> make_optional(T&& value); |
| 215 | + |
| 216 | +template<class T, class... Args > |
| 217 | +constexpr optional<T> make_optional(Args&&... args); |
| 218 | + |
| 219 | +template<class T, class U, class... Args > |
| 220 | +constexpr optional<T> make_optional(initializer_list<U> il, Args&&... args); |
| 221 | + |
| 222 | +template<class T> |
| 223 | +void swap(optional<T>& lhs, optional<T>& rhs); |
| 224 | + |
| 225 | +template<class T> |
| 226 | +struct hash<std::optional<T>> {}; |
| 227 | + |
| 228 | + |
| 229 | +} // namespace std |
| 230 | + |
| 231 | +#endif // CPPREFERENCE_STDVER >= 2017 |
| 232 | +#endif // CPPREFERENCE_OPTIONAL_H |
0 commit comments