blob: 19e20e0db6cbc1b482d02377a70f37267c1f8eb3 [file] [log] [blame]
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001// -*- C++ -*-
2//===-------------------------- optional ----------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Eric Fiselierd4ec6352016-10-12 07:46:20 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_OPTIONAL
11#define _LIBCPP_OPTIONAL
12
13/*
14 optional synopsis
15
16// C++1z
17
18namespace std {
Casey Carter84b8a402017-04-17 20:15:16 +000019 // 23.6.3, optional for object types
Eric Fiselierd4ec6352016-10-12 07:46:20 +000020 template <class T> class optional;
21
Casey Carter84b8a402017-04-17 20:15:16 +000022 // 23.6.4, no-value state indicator
Eric Fiselierd4ec6352016-10-12 07:46:20 +000023 struct nullopt_t{see below };
Marshall Clowf1bf62f2018-01-02 17:17:01 +000024 inline constexpr nullopt_t nullopt(unspecified );
Eric Fiselierd4ec6352016-10-12 07:46:20 +000025
Casey Carter84b8a402017-04-17 20:15:16 +000026 // 23.6.5, class bad_optional_access
Eric Fiselierd4ec6352016-10-12 07:46:20 +000027 class bad_optional_access;
28
Casey Carter84b8a402017-04-17 20:15:16 +000029 // 23.6.6, relational operators
30 template <class T, class U>
31 constexpr bool operator==(const optional<T>&, const optional<U>&);
32 template <class T, class U>
33 constexpr bool operator!=(const optional<T>&, const optional<U>&);
34 template <class T, class U>
35 constexpr bool operator<(const optional<T>&, const optional<U>&);
36 template <class T, class U>
37 constexpr bool operator>(const optional<T>&, const optional<U>&);
38 template <class T, class U>
39 constexpr bool operator<=(const optional<T>&, const optional<U>&);
40 template <class T, class U>
41 constexpr bool operator>=(const optional<T>&, const optional<U>&);
42
43 // 23.6.7 comparison with nullopt
Eric Fiselierd4ec6352016-10-12 07:46:20 +000044 template <class T> constexpr bool operator==(const optional<T>&, nullopt_t) noexcept;
45 template <class T> constexpr bool operator==(nullopt_t, const optional<T>&) noexcept;
46 template <class T> constexpr bool operator!=(const optional<T>&, nullopt_t) noexcept;
47 template <class T> constexpr bool operator!=(nullopt_t, const optional<T>&) noexcept;
48 template <class T> constexpr bool operator<(const optional<T>&, nullopt_t) noexcept;
49 template <class T> constexpr bool operator<(nullopt_t, const optional<T>&) noexcept;
50 template <class T> constexpr bool operator<=(const optional<T>&, nullopt_t) noexcept;
51 template <class T> constexpr bool operator<=(nullopt_t, const optional<T>&) noexcept;
52 template <class T> constexpr bool operator>(const optional<T>&, nullopt_t) noexcept;
53 template <class T> constexpr bool operator>(nullopt_t, const optional<T>&) noexcept;
54 template <class T> constexpr bool operator>=(const optional<T>&, nullopt_t) noexcept;
55 template <class T> constexpr bool operator>=(nullopt_t, const optional<T>&) noexcept;
56
Casey Carter84b8a402017-04-17 20:15:16 +000057 // 23.6.8, comparison with T
58 template <class T, class U> constexpr bool operator==(const optional<T>&, const U&);
Marshall Clow29dd7e42017-11-01 01:27:25 +000059 template <class T, class U> constexpr bool operator==(const T&, const optional<U>&);
Casey Carter84b8a402017-04-17 20:15:16 +000060 template <class T, class U> constexpr bool operator!=(const optional<T>&, const U&);
Marshall Clow29dd7e42017-11-01 01:27:25 +000061 template <class T, class U> constexpr bool operator!=(const T&, const optional<U>&);
Casey Carter84b8a402017-04-17 20:15:16 +000062 template <class T, class U> constexpr bool operator<(const optional<T>&, const U&);
Marshall Clow29dd7e42017-11-01 01:27:25 +000063 template <class T, class U> constexpr bool operator<(const T&, const optional<U>&);
Casey Carter84b8a402017-04-17 20:15:16 +000064 template <class T, class U> constexpr bool operator<=(const optional<T>&, const U&);
Marshall Clow29dd7e42017-11-01 01:27:25 +000065 template <class T, class U> constexpr bool operator<=(const T&, const optional<U>&);
Casey Carter84b8a402017-04-17 20:15:16 +000066 template <class T, class U> constexpr bool operator>(const optional<T>&, const U&);
Marshall Clow29dd7e42017-11-01 01:27:25 +000067 template <class T, class U> constexpr bool operator>(const T&, const optional<U>&);
Casey Carter84b8a402017-04-17 20:15:16 +000068 template <class T, class U> constexpr bool operator>=(const optional<T>&, const U&);
Marshall Clow29dd7e42017-11-01 01:27:25 +000069 template <class T, class U> constexpr bool operator>=(const T&, const optional<U>&);
Eric Fiselierd4ec6352016-10-12 07:46:20 +000070
Casey Carter84b8a402017-04-17 20:15:16 +000071 // 23.6.9, specialized algorithms
Eric Fiselierd4ec6352016-10-12 07:46:20 +000072 template <class T> void swap(optional<T>&, optional<T>&) noexcept(see below );
73 template <class T> constexpr optional<see below > make_optional(T&&);
74 template <class T, class... Args>
75 constexpr optional<T> make_optional(Args&&... args);
76 template <class T, class U, class... Args>
77 constexpr optional<T> make_optional(initializer_list<U> il, Args&&... args);
78
Casey Carter84b8a402017-04-17 20:15:16 +000079 // 23.6.10, hash support
Eric Fiselierd4ec6352016-10-12 07:46:20 +000080 template <class T> struct hash;
81 template <class T> struct hash<optional<T>>;
82
83 template <class T> class optional {
84 public:
85 using value_type = T;
86
Casey Carter84b8a402017-04-17 20:15:16 +000087 // 23.6.3.1, constructors
Eric Fiselierd4ec6352016-10-12 07:46:20 +000088 constexpr optional() noexcept;
89 constexpr optional(nullopt_t) noexcept;
90 optional(const optional &);
Marshall Clow45ff9822017-04-12 22:51:27 +000091 optional(optional &&) noexcept(see below);
Eric Fiselierd4ec6352016-10-12 07:46:20 +000092 template <class... Args> constexpr explicit optional(in_place_t, Args &&...);
93 template <class U, class... Args>
94 constexpr explicit optional(in_place_t, initializer_list<U>, Args &&...);
95 template <class U = T>
96 constexpr EXPLICIT optional(U &&);
97 template <class U>
98 constexpr EXPLICIT optional(const optional<U> &);
99 template <class U>
100 constexpr EXPLICIT optional(optional<U> &&);
101
Casey Carter84b8a402017-04-17 20:15:16 +0000102 // 23.6.3.2, destructor
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000103 ~optional();
104
Casey Carter84b8a402017-04-17 20:15:16 +0000105 // 23.6.3.3, assignment
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000106 optional &operator=(nullopt_t) noexcept;
Louis Dionnecee59012019-01-10 20:06:11 +0000107 optional &operator=(const optional &); // constexpr in C++20
108 optional &operator=(optional &&) noexcept(see below); // constexpr in C++20
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000109 template <class U = T> optional &operator=(U &&);
110 template <class U> optional &operator=(const optional<U> &);
111 template <class U> optional &operator=(optional<U> &&);
Marshall Clow45ff9822017-04-12 22:51:27 +0000112 template <class... Args> T& emplace(Args &&...);
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000113 template <class U, class... Args>
Marshall Clow45ff9822017-04-12 22:51:27 +0000114 T& emplace(initializer_list<U>, Args &&...);
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000115
Casey Carter84b8a402017-04-17 20:15:16 +0000116 // 23.6.3.4, swap
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000117 void swap(optional &) noexcept(see below );
118
Casey Carter84b8a402017-04-17 20:15:16 +0000119 // 23.6.3.5, observers
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000120 constexpr T const *operator->() const;
121 constexpr T *operator->();
122 constexpr T const &operator*() const &;
123 constexpr T &operator*() &;
124 constexpr T &&operator*() &&;
125 constexpr const T &&operator*() const &&;
126 constexpr explicit operator bool() const noexcept;
127 constexpr bool has_value() const noexcept;
128 constexpr T const &value() const &;
129 constexpr T &value() &;
130 constexpr T &&value() &&;
131 constexpr const T &&value() const &&;
132 template <class U> constexpr T value_or(U &&) const &;
133 template <class U> constexpr T value_or(U &&) &&;
134
Casey Carter84b8a402017-04-17 20:15:16 +0000135 // 23.6.3.6, modifiers
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000136 void reset() noexcept;
137
138 private:
139 T *val; // exposition only
140 };
Marshall Clowe3c71ae2018-05-25 02:08:49 +0000141
142template<class T>
143 optional(T) -> optional<T>;
144
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000145} // namespace std
146
147*/
148
149#include <__config>
Louis Dionne73912b22020-11-04 15:01:25 -0500150#include <__availability>
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000151#include <__debug>
152#include <__functional_base>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400153#include <compare>
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000154#include <functional>
155#include <initializer_list>
156#include <new>
157#include <stdexcept>
158#include <type_traits>
159#include <utility>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000160#include <version>
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000161
162#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
163#pragma GCC system_header
164#endif
165
Eric Fiselierf4433a32017-05-31 22:07:49 +0000166_LIBCPP_PUSH_MACROS
167#include <__undef_macros>
168
169
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000170namespace std // purposefully not using versioning namespace
171{
172
Louis Dionnecef92e62018-11-19 15:37:04 +0000173class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS bad_optional_access
Marshall Clowa627d4a2017-02-05 20:06:38 +0000174 : public exception
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000175{
176public:
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000177 // Get the key function ~bad_optional_access() into the dylib
Marshall Clow53fc0372017-02-05 20:52:32 +0000178 virtual ~bad_optional_access() _NOEXCEPT;
Marshall Clowa627d4a2017-02-05 20:06:38 +0000179 virtual const char* what() const _NOEXCEPT;
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000180};
181
182} // std
183
184#if _LIBCPP_STD_VER > 14
185
186_LIBCPP_BEGIN_NAMESPACE_STD
187
188_LIBCPP_NORETURN
189inline _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +0000190_LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000191void __throw_bad_optional_access() {
192#ifndef _LIBCPP_NO_EXCEPTIONS
193 throw bad_optional_access();
194#else
195 _VSTD::abort();
196#endif
197}
198
199struct nullopt_t
200{
201 struct __secret_tag { _LIBCPP_INLINE_VISIBILITY explicit __secret_tag() = default; };
202 _LIBCPP_INLINE_VISIBILITY constexpr explicit nullopt_t(__secret_tag, __secret_tag) noexcept {}
203};
204
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000205_LIBCPP_INLINE_VAR constexpr nullopt_t nullopt{nullopt_t::__secret_tag{}, nullopt_t::__secret_tag{}};
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000206
207template <class _Tp, bool = is_trivially_destructible<_Tp>::value>
208struct __optional_destruct_base;
209
210template <class _Tp>
211struct __optional_destruct_base<_Tp, false>
212{
213 typedef _Tp value_type;
214 static_assert(is_object_v<value_type>,
215 "instantiation of optional with a non-object type is undefined behavior");
216 union
217 {
218 char __null_state_;
219 value_type __val_;
220 };
221 bool __engaged_;
222
223 _LIBCPP_INLINE_VISIBILITY
224 ~__optional_destruct_base()
225 {
226 if (__engaged_)
227 __val_.~value_type();
228 }
229
230 _LIBCPP_INLINE_VISIBILITY
231 constexpr __optional_destruct_base() noexcept
232 : __null_state_(),
233 __engaged_(false) {}
234
235 template <class... _Args>
236 _LIBCPP_INLINE_VISIBILITY
237 constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args)
238 : __val_(_VSTD::forward<_Args>(__args)...),
239 __engaged_(true) {}
240
241 _LIBCPP_INLINE_VISIBILITY
242 void reset() noexcept
243 {
244 if (__engaged_)
245 {
246 __val_.~value_type();
247 __engaged_ = false;
248 }
249 }
250};
251
252template <class _Tp>
253struct __optional_destruct_base<_Tp, true>
254{
255 typedef _Tp value_type;
256 static_assert(is_object_v<value_type>,
257 "instantiation of optional with a non-object type is undefined behavior");
258 union
259 {
260 char __null_state_;
261 value_type __val_;
262 };
263 bool __engaged_;
264
265 _LIBCPP_INLINE_VISIBILITY
266 constexpr __optional_destruct_base() noexcept
267 : __null_state_(),
268 __engaged_(false) {}
269
270 template <class... _Args>
271 _LIBCPP_INLINE_VISIBILITY
272 constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args)
273 : __val_(_VSTD::forward<_Args>(__args)...),
274 __engaged_(true) {}
275
276 _LIBCPP_INLINE_VISIBILITY
277 void reset() noexcept
278 {
279 if (__engaged_)
280 {
281 __engaged_ = false;
282 }
283 }
284};
285
286template <class _Tp, bool = is_reference<_Tp>::value>
287struct __optional_storage_base : __optional_destruct_base<_Tp>
288{
289 using __base = __optional_destruct_base<_Tp>;
290 using value_type = _Tp;
291 using __base::__base;
292
293 _LIBCPP_INLINE_VISIBILITY
294 constexpr bool has_value() const noexcept
295 {
296 return this->__engaged_;
297 }
298
299 _LIBCPP_INLINE_VISIBILITY
300 constexpr value_type& __get() & noexcept
301 {
302 return this->__val_;
303 }
304 _LIBCPP_INLINE_VISIBILITY
305 constexpr const value_type& __get() const& noexcept
306 {
307 return this->__val_;
308 }
309 _LIBCPP_INLINE_VISIBILITY
310 constexpr value_type&& __get() && noexcept
311 {
312 return _VSTD::move(this->__val_);
313 }
314 _LIBCPP_INLINE_VISIBILITY
315 constexpr const value_type&& __get() const&& noexcept
316 {
317 return _VSTD::move(this->__val_);
318 }
319
320 template <class... _Args>
321 _LIBCPP_INLINE_VISIBILITY
322 void __construct(_Args&&... __args)
323 {
324 _LIBCPP_ASSERT(!has_value(), "__construct called for engaged __optional_storage");
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -0500325 ::new ((void*)_VSTD::addressof(this->__val_)) value_type(_VSTD::forward<_Args>(__args)...);
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000326 this->__engaged_ = true;
327 }
328
329 template <class _That>
330 _LIBCPP_INLINE_VISIBILITY
331 void __construct_from(_That&& __opt)
332 {
333 if (__opt.has_value())
334 __construct(_VSTD::forward<_That>(__opt).__get());
335 }
336
337 template <class _That>
338 _LIBCPP_INLINE_VISIBILITY
339 void __assign_from(_That&& __opt)
340 {
341 if (this->__engaged_ == __opt.has_value())
342 {
343 if (this->__engaged_)
344 this->__val_ = _VSTD::forward<_That>(__opt).__get();
345 }
346 else
347 {
348 if (this->__engaged_)
349 this->reset();
350 else
351 __construct(_VSTD::forward<_That>(__opt).__get());
352 }
353 }
354};
355
356// optional<T&> is currently required ill-formed, however it may to be in the
357// future. For this reason it has already been implemented to ensure we can
358// make the change in an ABI compatible manner.
359template <class _Tp>
360struct __optional_storage_base<_Tp, true>
361{
362 using value_type = _Tp;
363 using __raw_type = remove_reference_t<_Tp>;
364 __raw_type* __value_;
365
366 template <class _Up>
367 static constexpr bool __can_bind_reference() {
368 using _RawUp = typename remove_reference<_Up>::type;
369 using _UpPtr = _RawUp*;
370 using _RawTp = typename remove_reference<_Tp>::type;
371 using _TpPtr = _RawTp*;
372 using _CheckLValueArg = integral_constant<bool,
373 (is_lvalue_reference<_Up>::value && is_convertible<_UpPtr, _TpPtr>::value)
374 || is_same<_RawUp, reference_wrapper<_RawTp>>::value
375 || is_same<_RawUp, reference_wrapper<typename remove_const<_RawTp>::type>>::value
376 >;
377 return (is_lvalue_reference<_Tp>::value && _CheckLValueArg::value)
378 || (is_rvalue_reference<_Tp>::value && !is_lvalue_reference<_Up>::value &&
379 is_convertible<_UpPtr, _TpPtr>::value);
380 }
381
382 _LIBCPP_INLINE_VISIBILITY
383 constexpr __optional_storage_base() noexcept
384 : __value_(nullptr) {}
385
386 template <class _UArg>
387 _LIBCPP_INLINE_VISIBILITY
388 constexpr explicit __optional_storage_base(in_place_t, _UArg&& __uarg)
389 : __value_(_VSTD::addressof(__uarg))
390 {
391 static_assert(__can_bind_reference<_UArg>(),
392 "Attempted to construct a reference element in tuple from a "
393 "possible temporary");
394 }
395
396 _LIBCPP_INLINE_VISIBILITY
397 void reset() noexcept { __value_ = nullptr; }
398
399 _LIBCPP_INLINE_VISIBILITY
400 constexpr bool has_value() const noexcept
401 { return __value_ != nullptr; }
402
403 _LIBCPP_INLINE_VISIBILITY
404 constexpr value_type& __get() const& noexcept
405 { return *__value_; }
406
407 _LIBCPP_INLINE_VISIBILITY
408 constexpr value_type&& __get() const&& noexcept
409 { return _VSTD::forward<value_type>(*__value_); }
410
411 template <class _UArg>
412 _LIBCPP_INLINE_VISIBILITY
413 void __construct(_UArg&& __val)
414 {
415 _LIBCPP_ASSERT(!has_value(), "__construct called for engaged __optional_storage");
416 static_assert(__can_bind_reference<_UArg>(),
417 "Attempted to construct a reference element in tuple from a "
418 "possible temporary");
419 __value_ = _VSTD::addressof(__val);
420 }
421
422 template <class _That>
423 _LIBCPP_INLINE_VISIBILITY
424 void __construct_from(_That&& __opt)
425 {
426 if (__opt.has_value())
427 __construct(_VSTD::forward<_That>(__opt).__get());
428 }
429
430 template <class _That>
431 _LIBCPP_INLINE_VISIBILITY
432 void __assign_from(_That&& __opt)
433 {
434 if (has_value() == __opt.has_value())
435 {
436 if (has_value())
437 *__value_ = _VSTD::forward<_That>(__opt).__get();
438 }
439 else
440 {
441 if (has_value())
442 reset();
443 else
444 __construct(_VSTD::forward<_That>(__opt).__get());
445 }
446 }
447};
448
Casey Carterc189f7f2017-07-09 17:15:49 +0000449template <class _Tp, bool = is_trivially_copy_constructible<_Tp>::value>
450struct __optional_copy_base : __optional_storage_base<_Tp>
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000451{
452 using __optional_storage_base<_Tp>::__optional_storage_base;
453};
454
455template <class _Tp>
Casey Carterc189f7f2017-07-09 17:15:49 +0000456struct __optional_copy_base<_Tp, false> : __optional_storage_base<_Tp>
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000457{
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000458 using __optional_storage_base<_Tp>::__optional_storage_base;
459
460 _LIBCPP_INLINE_VISIBILITY
Casey Carterc189f7f2017-07-09 17:15:49 +0000461 __optional_copy_base() = default;
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000462
463 _LIBCPP_INLINE_VISIBILITY
Casey Carterc189f7f2017-07-09 17:15:49 +0000464 __optional_copy_base(const __optional_copy_base& __opt)
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000465 {
466 this->__construct_from(__opt);
467 }
468
469 _LIBCPP_INLINE_VISIBILITY
Casey Carterc189f7f2017-07-09 17:15:49 +0000470 __optional_copy_base(__optional_copy_base&&) = default;
471 _LIBCPP_INLINE_VISIBILITY
472 __optional_copy_base& operator=(const __optional_copy_base&) = default;
473 _LIBCPP_INLINE_VISIBILITY
474 __optional_copy_base& operator=(__optional_copy_base&&) = default;
475};
476
477template <class _Tp, bool = is_trivially_move_constructible<_Tp>::value>
478struct __optional_move_base : __optional_copy_base<_Tp>
479{
480 using __optional_copy_base<_Tp>::__optional_copy_base;
481};
482
483template <class _Tp>
484struct __optional_move_base<_Tp, false> : __optional_copy_base<_Tp>
485{
486 using value_type = _Tp;
487 using __optional_copy_base<_Tp>::__optional_copy_base;
488
489 _LIBCPP_INLINE_VISIBILITY
490 __optional_move_base() = default;
491 _LIBCPP_INLINE_VISIBILITY
492 __optional_move_base(const __optional_move_base&) = default;
493
494 _LIBCPP_INLINE_VISIBILITY
495 __optional_move_base(__optional_move_base&& __opt)
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000496 noexcept(is_nothrow_move_constructible_v<value_type>)
497 {
498 this->__construct_from(_VSTD::move(__opt));
499 }
500
501 _LIBCPP_INLINE_VISIBILITY
Casey Carterc189f7f2017-07-09 17:15:49 +0000502 __optional_move_base& operator=(const __optional_move_base&) = default;
503 _LIBCPP_INLINE_VISIBILITY
504 __optional_move_base& operator=(__optional_move_base&&) = default;
505};
506
507template <class _Tp, bool =
508 is_trivially_destructible<_Tp>::value &&
509 is_trivially_copy_constructible<_Tp>::value &&
510 is_trivially_copy_assignable<_Tp>::value>
511struct __optional_copy_assign_base : __optional_move_base<_Tp>
512{
513 using __optional_move_base<_Tp>::__optional_move_base;
514};
515
516template <class _Tp>
517struct __optional_copy_assign_base<_Tp, false> : __optional_move_base<_Tp>
518{
519 using __optional_move_base<_Tp>::__optional_move_base;
520
521 _LIBCPP_INLINE_VISIBILITY
522 __optional_copy_assign_base() = default;
523 _LIBCPP_INLINE_VISIBILITY
524 __optional_copy_assign_base(const __optional_copy_assign_base&) = default;
525 _LIBCPP_INLINE_VISIBILITY
526 __optional_copy_assign_base(__optional_copy_assign_base&&) = default;
527
528 _LIBCPP_INLINE_VISIBILITY
529 __optional_copy_assign_base& operator=(const __optional_copy_assign_base& __opt)
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000530 {
531 this->__assign_from(__opt);
532 return *this;
533 }
534
535 _LIBCPP_INLINE_VISIBILITY
Casey Carterc189f7f2017-07-09 17:15:49 +0000536 __optional_copy_assign_base& operator=(__optional_copy_assign_base&&) = default;
537};
538
539template <class _Tp, bool =
540 is_trivially_destructible<_Tp>::value &&
541 is_trivially_move_constructible<_Tp>::value &&
542 is_trivially_move_assignable<_Tp>::value>
543struct __optional_move_assign_base : __optional_copy_assign_base<_Tp>
544{
545 using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base;
546};
547
548template <class _Tp>
549struct __optional_move_assign_base<_Tp, false> : __optional_copy_assign_base<_Tp>
550{
551 using value_type = _Tp;
552 using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base;
553
554 _LIBCPP_INLINE_VISIBILITY
555 __optional_move_assign_base() = default;
556 _LIBCPP_INLINE_VISIBILITY
557 __optional_move_assign_base(const __optional_move_assign_base& __opt) = default;
558 _LIBCPP_INLINE_VISIBILITY
559 __optional_move_assign_base(__optional_move_assign_base&&) = default;
560 _LIBCPP_INLINE_VISIBILITY
561 __optional_move_assign_base& operator=(const __optional_move_assign_base&) = default;
562
563 _LIBCPP_INLINE_VISIBILITY
564 __optional_move_assign_base& operator=(__optional_move_assign_base&& __opt)
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000565 noexcept(is_nothrow_move_assignable_v<value_type> &&
566 is_nothrow_move_constructible_v<value_type>)
567 {
568 this->__assign_from(_VSTD::move(__opt));
569 return *this;
570 }
571};
572
573template <class _Tp>
574using __optional_sfinae_ctor_base_t = __sfinae_ctor_base<
575 is_copy_constructible<_Tp>::value,
576 is_move_constructible<_Tp>::value
577>;
578
579template <class _Tp>
580using __optional_sfinae_assign_base_t = __sfinae_assign_base<
581 (is_copy_constructible<_Tp>::value && is_copy_assignable<_Tp>::value),
582 (is_move_constructible<_Tp>::value && is_move_assignable<_Tp>::value)
583>;
584
585template <class _Tp>
586class optional
Casey Carterc189f7f2017-07-09 17:15:49 +0000587 : private __optional_move_assign_base<_Tp>
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000588 , private __optional_sfinae_ctor_base_t<_Tp>
589 , private __optional_sfinae_assign_base_t<_Tp>
590{
Casey Carterc189f7f2017-07-09 17:15:49 +0000591 using __base = __optional_move_assign_base<_Tp>;
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000592public:
593 using value_type = _Tp;
594
595private:
596 // Disable the reference extension using this static assert.
Marshall Clowfa6a3342019-03-25 16:35:59 +0000597 static_assert(!is_same_v<__uncvref_t<value_type>, in_place_t>,
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000598 "instantiation of optional with in_place_t is ill-formed");
599 static_assert(!is_same_v<__uncvref_t<value_type>, nullopt_t>,
600 "instantiation of optional with nullopt_t is ill-formed");
601 static_assert(!is_reference_v<value_type>,
602 "instantiation of optional with a reference type is ill-formed");
603 static_assert(is_destructible_v<value_type>,
604 "instantiation of optional with a non-destructible type is ill-formed");
Marshall Clowfa6a3342019-03-25 16:35:59 +0000605 static_assert(!is_array_v<value_type>,
606 "instantiation of optional with an array type is ill-formed");
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000607
608 // LWG2756: conditionally explicit conversion from _Up
609 struct _CheckOptionalArgsConstructor {
610 template <class _Up>
611 static constexpr bool __enable_implicit() {
612 return is_constructible_v<_Tp, _Up&&> &&
613 is_convertible_v<_Up&&, _Tp>;
614 }
615
616 template <class _Up>
617 static constexpr bool __enable_explicit() {
618 return is_constructible_v<_Tp, _Up&&> &&
619 !is_convertible_v<_Up&&, _Tp>;
620 }
621 };
622 template <class _Up>
Eric Fiselier3906a132019-06-23 20:28:29 +0000623 using _CheckOptionalArgsCtor = _If<
624 _IsNotSame<__uncvref_t<_Up>, in_place_t>::value &&
625 _IsNotSame<__uncvref_t<_Up>, optional>::value,
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000626 _CheckOptionalArgsConstructor,
627 __check_tuple_constructor_fail
628 >;
629 template <class _QualUp>
630 struct _CheckOptionalLikeConstructor {
631 template <class _Up, class _Opt = optional<_Up>>
Eric Fiselier3906a132019-06-23 20:28:29 +0000632 using __check_constructible_from_opt = _Or<
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000633 is_constructible<_Tp, _Opt&>,
634 is_constructible<_Tp, _Opt const&>,
635 is_constructible<_Tp, _Opt&&>,
636 is_constructible<_Tp, _Opt const&&>,
637 is_convertible<_Opt&, _Tp>,
638 is_convertible<_Opt const&, _Tp>,
639 is_convertible<_Opt&&, _Tp>,
640 is_convertible<_Opt const&&, _Tp>
641 >;
642 template <class _Up, class _Opt = optional<_Up>>
Eric Fiselier3906a132019-06-23 20:28:29 +0000643 using __check_assignable_from_opt = _Or<
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000644 is_assignable<_Tp&, _Opt&>,
645 is_assignable<_Tp&, _Opt const&>,
646 is_assignable<_Tp&, _Opt&&>,
647 is_assignable<_Tp&, _Opt const&&>
648 >;
649 template <class _Up, class _QUp = _QualUp>
650 static constexpr bool __enable_implicit() {
651 return is_convertible<_QUp, _Tp>::value &&
652 !__check_constructible_from_opt<_Up>::value;
653 }
654 template <class _Up, class _QUp = _QualUp>
655 static constexpr bool __enable_explicit() {
656 return !is_convertible<_QUp, _Tp>::value &&
657 !__check_constructible_from_opt<_Up>::value;
658 }
659 template <class _Up, class _QUp = _QualUp>
660 static constexpr bool __enable_assign() {
Arthur O'Dwyerdf5df562020-12-12 11:57:32 -0500661 // Construction and assignability of _QUp to _Tp has already been
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000662 // checked.
663 return !__check_constructible_from_opt<_Up>::value &&
664 !__check_assignable_from_opt<_Up>::value;
665 }
666 };
667
668 template <class _Up, class _QualUp>
Eric Fiselier3906a132019-06-23 20:28:29 +0000669 using _CheckOptionalLikeCtor = _If<
670 _And<
671 _IsNotSame<_Up, _Tp>,
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000672 is_constructible<_Tp, _QualUp>
673 >::value,
674 _CheckOptionalLikeConstructor<_QualUp>,
675 __check_tuple_constructor_fail
676 >;
677 template <class _Up, class _QualUp>
Eric Fiselier3906a132019-06-23 20:28:29 +0000678 using _CheckOptionalLikeAssign = _If<
679 _And<
680 _IsNotSame<_Up, _Tp>,
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000681 is_constructible<_Tp, _QualUp>,
682 is_assignable<_Tp&, _QualUp>
683 >::value,
684 _CheckOptionalLikeConstructor<_QualUp>,
685 __check_tuple_constructor_fail
686 >;
687public:
688
689 _LIBCPP_INLINE_VISIBILITY constexpr optional() noexcept {}
Marshall Clowd44e2e32017-05-17 15:30:01 +0000690 _LIBCPP_INLINE_VISIBILITY constexpr optional(const optional&) = default;
691 _LIBCPP_INLINE_VISIBILITY constexpr optional(optional&&) = default;
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000692 _LIBCPP_INLINE_VISIBILITY constexpr optional(nullopt_t) noexcept {}
693
Eric Fiselier3906a132019-06-23 20:28:29 +0000694 template <class _InPlaceT, class... _Args, class = _EnableIf<
695 _And<
696 _IsSame<_InPlaceT, in_place_t>,
697 is_constructible<value_type, _Args...>
Eric Fiselier82103932019-03-11 22:55:21 +0000698 >::value
699 >
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000700 >
701 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier82103932019-03-11 22:55:21 +0000702 constexpr explicit optional(_InPlaceT, _Args&&... __args)
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000703 : __base(in_place, _VSTD::forward<_Args>(__args)...) {}
704
Eric Fiselier3906a132019-06-23 20:28:29 +0000705 template <class _Up, class... _Args, class = _EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000706 is_constructible_v<value_type, initializer_list<_Up>&, _Args...>>
707 >
708 _LIBCPP_INLINE_VISIBILITY
709 constexpr explicit optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
710 : __base(in_place, __il, _VSTD::forward<_Args>(__args)...) {}
711
Eric Fiselier3906a132019-06-23 20:28:29 +0000712 template <class _Up = value_type, _EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000713 _CheckOptionalArgsCtor<_Up>::template __enable_implicit<_Up>()
714 , int> = 0>
715 _LIBCPP_INLINE_VISIBILITY
716 constexpr optional(_Up&& __v)
717 : __base(in_place, _VSTD::forward<_Up>(__v)) {}
718
Eric Fiselier3906a132019-06-23 20:28:29 +0000719 template <class _Up, _EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000720 _CheckOptionalArgsCtor<_Up>::template __enable_explicit<_Up>()
721 , int> = 0>
722 _LIBCPP_INLINE_VISIBILITY
723 constexpr explicit optional(_Up&& __v)
724 : __base(in_place, _VSTD::forward<_Up>(__v)) {}
725
726 // LWG2756: conditionally explicit conversion from const optional<_Up>&
Eric Fiselier3906a132019-06-23 20:28:29 +0000727 template <class _Up, _EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000728 _CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_implicit<_Up>()
729 , int> = 0>
730 _LIBCPP_INLINE_VISIBILITY
731 optional(const optional<_Up>& __v)
732 {
733 this->__construct_from(__v);
734 }
Eric Fiselier3906a132019-06-23 20:28:29 +0000735 template <class _Up, _EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000736 _CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_explicit<_Up>()
737 , int> = 0>
738 _LIBCPP_INLINE_VISIBILITY
739 explicit optional(const optional<_Up>& __v)
740 {
741 this->__construct_from(__v);
742 }
743
744 // LWG2756: conditionally explicit conversion from optional<_Up>&&
Eric Fiselier3906a132019-06-23 20:28:29 +0000745 template <class _Up, _EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000746 _CheckOptionalLikeCtor<_Up, _Up &&>::template __enable_implicit<_Up>()
747 , int> = 0>
748 _LIBCPP_INLINE_VISIBILITY
749 optional(optional<_Up>&& __v)
750 {
751 this->__construct_from(_VSTD::move(__v));
752 }
Eric Fiselier3906a132019-06-23 20:28:29 +0000753 template <class _Up, _EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000754 _CheckOptionalLikeCtor<_Up, _Up &&>::template __enable_explicit<_Up>()
755 , int> = 0>
756 _LIBCPP_INLINE_VISIBILITY
757 explicit optional(optional<_Up>&& __v)
758 {
759 this->__construct_from(_VSTD::move(__v));
760 }
761
762 _LIBCPP_INLINE_VISIBILITY
763 optional& operator=(nullopt_t) noexcept
764 {
765 reset();
766 return *this;
767 }
768
769 _LIBCPP_INLINE_VISIBILITY optional& operator=(const optional&) = default;
770 _LIBCPP_INLINE_VISIBILITY optional& operator=(optional&&) = default;
771
772 // LWG2756
773 template <class _Up = value_type,
Eric Fiselier3906a132019-06-23 20:28:29 +0000774 class = _EnableIf<
775 _And<
776 _IsNotSame<__uncvref_t<_Up>, optional>,
777 _Or<
Marshall Clow99315112019-06-27 18:40:55 +0000778 _IsNotSame<__uncvref_t<_Up>, value_type>,
Eric Fiselier3906a132019-06-23 20:28:29 +0000779 _Not<is_scalar<value_type>>
Eric Fiselierb9014262016-10-16 03:21:35 +0000780 >,
781 is_constructible<value_type, _Up>,
782 is_assignable<value_type&, _Up>
783 >::value>
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000784 >
785 _LIBCPP_INLINE_VISIBILITY
786 optional&
787 operator=(_Up&& __v)
788 {
789 if (this->has_value())
790 this->__get() = _VSTD::forward<_Up>(__v);
791 else
792 this->__construct(_VSTD::forward<_Up>(__v));
793 return *this;
794 }
795
796 // LWG2756
Eric Fiselier3906a132019-06-23 20:28:29 +0000797 template <class _Up, _EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000798 _CheckOptionalLikeAssign<_Up, _Up const&>::template __enable_assign<_Up>()
799 , int> = 0>
800 _LIBCPP_INLINE_VISIBILITY
801 optional&
802 operator=(const optional<_Up>& __v)
803 {
804 this->__assign_from(__v);
805 return *this;
806 }
807
808 // LWG2756
Eric Fiselier3906a132019-06-23 20:28:29 +0000809 template <class _Up, _EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000810 _CheckOptionalLikeCtor<_Up, _Up &&>::template __enable_assign<_Up>()
811 , int> = 0>
812 _LIBCPP_INLINE_VISIBILITY
813 optional&
814 operator=(optional<_Up>&& __v)
815 {
816 this->__assign_from(_VSTD::move(__v));
817 return *this;
818 }
819
820 template <class... _Args,
Eric Fiselier3906a132019-06-23 20:28:29 +0000821 class = _EnableIf
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000822 <
823 is_constructible_v<value_type, _Args...>
824 >
825 >
826 _LIBCPP_INLINE_VISIBILITY
Marshall Clow45ff9822017-04-12 22:51:27 +0000827 _Tp &
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000828 emplace(_Args&&... __args)
829 {
830 reset();
831 this->__construct(_VSTD::forward<_Args>(__args)...);
Marshall Clow45ff9822017-04-12 22:51:27 +0000832 return this->__get();
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000833 }
834
835 template <class _Up, class... _Args,
Eric Fiselier3906a132019-06-23 20:28:29 +0000836 class = _EnableIf
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000837 <
838 is_constructible_v<value_type, initializer_list<_Up>&, _Args...>
839 >
840 >
841 _LIBCPP_INLINE_VISIBILITY
Marshall Clow45ff9822017-04-12 22:51:27 +0000842 _Tp &
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000843 emplace(initializer_list<_Up> __il, _Args&&... __args)
844 {
845 reset();
846 this->__construct(__il, _VSTD::forward<_Args>(__args)...);
Marshall Clow45ff9822017-04-12 22:51:27 +0000847 return this->__get();
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000848 }
849
850 _LIBCPP_INLINE_VISIBILITY
851 void swap(optional& __opt)
852 noexcept(is_nothrow_move_constructible_v<value_type> &&
853 is_nothrow_swappable_v<value_type>)
854 {
855 if (this->has_value() == __opt.has_value())
856 {
857 using _VSTD::swap;
858 if (this->has_value())
859 swap(this->__get(), __opt.__get());
860 }
861 else
862 {
863 if (this->has_value())
864 {
865 __opt.__construct(_VSTD::move(this->__get()));
866 reset();
867 }
868 else
869 {
870 this->__construct(_VSTD::move(__opt.__get()));
871 __opt.reset();
872 }
873 }
874 }
875
876 _LIBCPP_INLINE_VISIBILITY
877 constexpr
878 add_pointer_t<value_type const>
879 operator->() const
880 {
881 _LIBCPP_ASSERT(this->has_value(), "optional operator-> called for disengaged value");
882#ifndef _LIBCPP_HAS_NO_BUILTIN_ADDRESSOF
883 return _VSTD::addressof(this->__get());
884#else
885 return __operator_arrow(__has_operator_addressof<value_type>{}, this->__get());
886#endif
887 }
888
889 _LIBCPP_INLINE_VISIBILITY
890 constexpr
891 add_pointer_t<value_type>
892 operator->()
893 {
894 _LIBCPP_ASSERT(this->has_value(), "optional operator-> called for disengaged value");
895#ifndef _LIBCPP_HAS_NO_BUILTIN_ADDRESSOF
896 return _VSTD::addressof(this->__get());
897#else
898 return __operator_arrow(__has_operator_addressof<value_type>{}, this->__get());
899#endif
900 }
901
902 _LIBCPP_INLINE_VISIBILITY
903 constexpr
904 const value_type&
905 operator*() const&
906 {
907 _LIBCPP_ASSERT(this->has_value(), "optional operator* called for disengaged value");
908 return this->__get();
909 }
910
911 _LIBCPP_INLINE_VISIBILITY
912 constexpr
913 value_type&
914 operator*() &
915 {
916 _LIBCPP_ASSERT(this->has_value(), "optional operator* called for disengaged value");
917 return this->__get();
918 }
919
920 _LIBCPP_INLINE_VISIBILITY
921 constexpr
922 value_type&&
923 operator*() &&
924 {
925 _LIBCPP_ASSERT(this->has_value(), "optional operator* called for disengaged value");
926 return _VSTD::move(this->__get());
927 }
928
929 _LIBCPP_INLINE_VISIBILITY
930 constexpr
931 const value_type&&
932 operator*() const&&
933 {
934 _LIBCPP_ASSERT(this->has_value(), "optional operator* called for disengaged value");
935 return _VSTD::move(this->__get());
936 }
937
938 _LIBCPP_INLINE_VISIBILITY
939 constexpr explicit operator bool() const noexcept { return has_value(); }
940
941 using __base::has_value;
942 using __base::__get;
943
944 _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +0000945 _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000946 constexpr value_type const& value() const&
947 {
948 if (!this->has_value())
949 __throw_bad_optional_access();
950 return this->__get();
951 }
952
953 _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +0000954 _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000955 constexpr value_type& value() &
956 {
957 if (!this->has_value())
958 __throw_bad_optional_access();
959 return this->__get();
960 }
961
962 _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +0000963 _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000964 constexpr value_type&& value() &&
965 {
966 if (!this->has_value())
967 __throw_bad_optional_access();
968 return _VSTD::move(this->__get());
969 }
970
971 _LIBCPP_INLINE_VISIBILITY
Louis Dionnecef92e62018-11-19 15:37:04 +0000972 _LIBCPP_AVAILABILITY_THROW_BAD_OPTIONAL_ACCESS
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000973 constexpr value_type const&& value() const&&
974 {
975 if (!this->has_value())
976 __throw_bad_optional_access();
977 return _VSTD::move(this->__get());
978 }
979
980 template <class _Up>
981 _LIBCPP_INLINE_VISIBILITY
982 constexpr value_type value_or(_Up&& __v) const&
983 {
984 static_assert(is_copy_constructible_v<value_type>,
985 "optional<T>::value_or: T must be copy constructible");
986 static_assert(is_convertible_v<_Up, value_type>,
987 "optional<T>::value_or: U must be convertible to T");
988 return this->has_value() ? this->__get() :
989 static_cast<value_type>(_VSTD::forward<_Up>(__v));
990 }
991
992 template <class _Up>
993 _LIBCPP_INLINE_VISIBILITY
Casey Cartercb05fae2017-06-06 18:47:26 +0000994 constexpr value_type value_or(_Up&& __v) &&
Eric Fiselierd4ec6352016-10-12 07:46:20 +0000995 {
996 static_assert(is_move_constructible_v<value_type>,
997 "optional<T>::value_or: T must be move constructible");
998 static_assert(is_convertible_v<_Up, value_type>,
999 "optional<T>::value_or: U must be convertible to T");
1000 return this->has_value() ? _VSTD::move(this->__get()) :
1001 static_cast<value_type>(_VSTD::forward<_Up>(__v));
1002 }
1003
1004 using __base::reset;
1005
1006private:
1007 template <class _Up>
1008 _LIBCPP_INLINE_VISIBILITY
1009 static _Up*
1010 __operator_arrow(true_type, _Up& __x)
1011 {
1012 return _VSTD::addressof(__x);
1013 }
1014
1015 template <class _Up>
1016 _LIBCPP_INLINE_VISIBILITY
1017 static constexpr _Up*
1018 __operator_arrow(false_type, _Up& __x)
1019 {
1020 return &__x;
1021 }
1022};
1023
Marshall Clowe3c71ae2018-05-25 02:08:49 +00001024#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
1025template<class T>
1026 optional(T) -> optional<T>;
1027#endif
1028
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001029// Comparisons between optionals
Eric Fiselier8af90142017-03-30 20:06:52 +00001030template <class _Tp, class _Up>
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001031_LIBCPP_INLINE_VISIBILITY constexpr
Eric Fiselier3906a132019-06-23 20:28:29 +00001032_EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001033 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() ==
Eric Fiselier8af90142017-03-30 20:06:52 +00001034 _VSTD::declval<const _Up&>()), bool>,
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001035 bool
1036>
Eric Fiselier8af90142017-03-30 20:06:52 +00001037operator==(const optional<_Tp>& __x, const optional<_Up>& __y)
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001038{
1039 if (static_cast<bool>(__x) != static_cast<bool>(__y))
1040 return false;
1041 if (!static_cast<bool>(__x))
1042 return true;
1043 return *__x == *__y;
1044}
1045
Eric Fiselier8af90142017-03-30 20:06:52 +00001046template <class _Tp, class _Up>
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001047_LIBCPP_INLINE_VISIBILITY constexpr
Eric Fiselier3906a132019-06-23 20:28:29 +00001048_EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001049 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() !=
Eric Fiselier8af90142017-03-30 20:06:52 +00001050 _VSTD::declval<const _Up&>()), bool>,
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001051 bool
1052>
Eric Fiselier8af90142017-03-30 20:06:52 +00001053operator!=(const optional<_Tp>& __x, const optional<_Up>& __y)
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001054{
1055 if (static_cast<bool>(__x) != static_cast<bool>(__y))
1056 return true;
1057 if (!static_cast<bool>(__x))
1058 return false;
1059 return *__x != *__y;
1060}
1061
Eric Fiselier8af90142017-03-30 20:06:52 +00001062template <class _Tp, class _Up>
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001063_LIBCPP_INLINE_VISIBILITY constexpr
Eric Fiselier3906a132019-06-23 20:28:29 +00001064_EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001065 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() <
Eric Fiselier8af90142017-03-30 20:06:52 +00001066 _VSTD::declval<const _Up&>()), bool>,
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001067 bool
1068>
Eric Fiselier8af90142017-03-30 20:06:52 +00001069operator<(const optional<_Tp>& __x, const optional<_Up>& __y)
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001070{
1071 if (!static_cast<bool>(__y))
1072 return false;
1073 if (!static_cast<bool>(__x))
1074 return true;
1075 return *__x < *__y;
1076}
1077
Eric Fiselier8af90142017-03-30 20:06:52 +00001078template <class _Tp, class _Up>
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001079_LIBCPP_INLINE_VISIBILITY constexpr
Eric Fiselier3906a132019-06-23 20:28:29 +00001080_EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001081 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() >
Eric Fiselier8af90142017-03-30 20:06:52 +00001082 _VSTD::declval<const _Up&>()), bool>,
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001083 bool
1084>
Eric Fiselier8af90142017-03-30 20:06:52 +00001085operator>(const optional<_Tp>& __x, const optional<_Up>& __y)
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001086{
1087 if (!static_cast<bool>(__x))
1088 return false;
1089 if (!static_cast<bool>(__y))
1090 return true;
1091 return *__x > *__y;
1092}
1093
Eric Fiselier8af90142017-03-30 20:06:52 +00001094template <class _Tp, class _Up>
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001095_LIBCPP_INLINE_VISIBILITY constexpr
Eric Fiselier3906a132019-06-23 20:28:29 +00001096_EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001097 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() <=
Eric Fiselier8af90142017-03-30 20:06:52 +00001098 _VSTD::declval<const _Up&>()), bool>,
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001099 bool
1100>
Eric Fiselier8af90142017-03-30 20:06:52 +00001101operator<=(const optional<_Tp>& __x, const optional<_Up>& __y)
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001102{
1103 if (!static_cast<bool>(__x))
1104 return true;
1105 if (!static_cast<bool>(__y))
1106 return false;
1107 return *__x <= *__y;
1108}
1109
Eric Fiselier8af90142017-03-30 20:06:52 +00001110template <class _Tp, class _Up>
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001111_LIBCPP_INLINE_VISIBILITY constexpr
Eric Fiselier3906a132019-06-23 20:28:29 +00001112_EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001113 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() >=
Eric Fiselier8af90142017-03-30 20:06:52 +00001114 _VSTD::declval<const _Up&>()), bool>,
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001115 bool
1116>
Eric Fiselier8af90142017-03-30 20:06:52 +00001117operator>=(const optional<_Tp>& __x, const optional<_Up>& __y)
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001118{
1119 if (!static_cast<bool>(__y))
1120 return true;
1121 if (!static_cast<bool>(__x))
1122 return false;
1123 return *__x >= *__y;
1124}
1125
1126// Comparisons with nullopt
1127template <class _Tp>
1128_LIBCPP_INLINE_VISIBILITY constexpr
1129bool
1130operator==(const optional<_Tp>& __x, nullopt_t) noexcept
1131{
1132 return !static_cast<bool>(__x);
1133}
1134
1135template <class _Tp>
1136_LIBCPP_INLINE_VISIBILITY constexpr
1137bool
1138operator==(nullopt_t, const optional<_Tp>& __x) noexcept
1139{
1140 return !static_cast<bool>(__x);
1141}
1142
1143template <class _Tp>
1144_LIBCPP_INLINE_VISIBILITY constexpr
1145bool
1146operator!=(const optional<_Tp>& __x, nullopt_t) noexcept
1147{
1148 return static_cast<bool>(__x);
1149}
1150
1151template <class _Tp>
1152_LIBCPP_INLINE_VISIBILITY constexpr
1153bool
1154operator!=(nullopt_t, const optional<_Tp>& __x) noexcept
1155{
1156 return static_cast<bool>(__x);
1157}
1158
1159template <class _Tp>
1160_LIBCPP_INLINE_VISIBILITY constexpr
1161bool
1162operator<(const optional<_Tp>&, nullopt_t) noexcept
1163{
1164 return false;
1165}
1166
1167template <class _Tp>
1168_LIBCPP_INLINE_VISIBILITY constexpr
1169bool
1170operator<(nullopt_t, const optional<_Tp>& __x) noexcept
1171{
1172 return static_cast<bool>(__x);
1173}
1174
1175template <class _Tp>
1176_LIBCPP_INLINE_VISIBILITY constexpr
1177bool
1178operator<=(const optional<_Tp>& __x, nullopt_t) noexcept
1179{
1180 return !static_cast<bool>(__x);
1181}
1182
1183template <class _Tp>
1184_LIBCPP_INLINE_VISIBILITY constexpr
1185bool
Eric Fiselier6003c772016-12-23 23:37:52 +00001186operator<=(nullopt_t, const optional<_Tp>&) noexcept
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001187{
1188 return true;
1189}
1190
1191template <class _Tp>
1192_LIBCPP_INLINE_VISIBILITY constexpr
1193bool
1194operator>(const optional<_Tp>& __x, nullopt_t) noexcept
1195{
1196 return static_cast<bool>(__x);
1197}
1198
1199template <class _Tp>
1200_LIBCPP_INLINE_VISIBILITY constexpr
1201bool
Eric Fiselier6003c772016-12-23 23:37:52 +00001202operator>(nullopt_t, const optional<_Tp>&) noexcept
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001203{
1204 return false;
1205}
1206
1207template <class _Tp>
1208_LIBCPP_INLINE_VISIBILITY constexpr
1209bool
1210operator>=(const optional<_Tp>&, nullopt_t) noexcept
1211{
1212 return true;
1213}
1214
1215template <class _Tp>
1216_LIBCPP_INLINE_VISIBILITY constexpr
1217bool
1218operator>=(nullopt_t, const optional<_Tp>& __x) noexcept
1219{
1220 return !static_cast<bool>(__x);
1221}
1222
1223// Comparisons with T
Eric Fiselier8af90142017-03-30 20:06:52 +00001224template <class _Tp, class _Up>
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001225_LIBCPP_INLINE_VISIBILITY constexpr
Eric Fiselier3906a132019-06-23 20:28:29 +00001226_EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001227 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() ==
Eric Fiselier8af90142017-03-30 20:06:52 +00001228 _VSTD::declval<const _Up&>()), bool>,
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001229 bool
1230>
Eric Fiselier8af90142017-03-30 20:06:52 +00001231operator==(const optional<_Tp>& __x, const _Up& __v)
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001232{
1233 return static_cast<bool>(__x) ? *__x == __v : false;
1234}
1235
Eric Fiselier8af90142017-03-30 20:06:52 +00001236template <class _Tp, class _Up>
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001237_LIBCPP_INLINE_VISIBILITY constexpr
Eric Fiselier3906a132019-06-23 20:28:29 +00001238_EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001239 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() ==
Eric Fiselier8af90142017-03-30 20:06:52 +00001240 _VSTD::declval<const _Up&>()), bool>,
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001241 bool
1242>
Eric Fiselier8af90142017-03-30 20:06:52 +00001243operator==(const _Tp& __v, const optional<_Up>& __x)
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001244{
1245 return static_cast<bool>(__x) ? __v == *__x : false;
1246}
1247
Eric Fiselier8af90142017-03-30 20:06:52 +00001248template <class _Tp, class _Up>
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001249_LIBCPP_INLINE_VISIBILITY constexpr
Eric Fiselier3906a132019-06-23 20:28:29 +00001250_EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001251 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() !=
Eric Fiselier8af90142017-03-30 20:06:52 +00001252 _VSTD::declval<const _Up&>()), bool>,
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001253 bool
1254>
Eric Fiselier8af90142017-03-30 20:06:52 +00001255operator!=(const optional<_Tp>& __x, const _Up& __v)
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001256{
1257 return static_cast<bool>(__x) ? *__x != __v : true;
1258}
1259
Eric Fiselier8af90142017-03-30 20:06:52 +00001260template <class _Tp, class _Up>
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001261_LIBCPP_INLINE_VISIBILITY constexpr
Eric Fiselier3906a132019-06-23 20:28:29 +00001262_EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001263 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() !=
Eric Fiselier8af90142017-03-30 20:06:52 +00001264 _VSTD::declval<const _Up&>()), bool>,
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001265 bool
1266>
Eric Fiselier8af90142017-03-30 20:06:52 +00001267operator!=(const _Tp& __v, const optional<_Up>& __x)
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001268{
1269 return static_cast<bool>(__x) ? __v != *__x : true;
1270}
1271
Eric Fiselier8af90142017-03-30 20:06:52 +00001272template <class _Tp, class _Up>
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001273_LIBCPP_INLINE_VISIBILITY constexpr
Eric Fiselier3906a132019-06-23 20:28:29 +00001274_EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001275 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() <
Eric Fiselier8af90142017-03-30 20:06:52 +00001276 _VSTD::declval<const _Up&>()), bool>,
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001277 bool
1278>
Eric Fiselier8af90142017-03-30 20:06:52 +00001279operator<(const optional<_Tp>& __x, const _Up& __v)
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001280{
1281 return static_cast<bool>(__x) ? *__x < __v : true;
1282}
1283
Eric Fiselier8af90142017-03-30 20:06:52 +00001284template <class _Tp, class _Up>
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001285_LIBCPP_INLINE_VISIBILITY constexpr
Eric Fiselier3906a132019-06-23 20:28:29 +00001286_EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001287 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() <
Eric Fiselier8af90142017-03-30 20:06:52 +00001288 _VSTD::declval<const _Up&>()), bool>,
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001289 bool
1290>
Eric Fiselier8af90142017-03-30 20:06:52 +00001291operator<(const _Tp& __v, const optional<_Up>& __x)
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001292{
1293 return static_cast<bool>(__x) ? __v < *__x : false;
1294}
1295
Eric Fiselier8af90142017-03-30 20:06:52 +00001296template <class _Tp, class _Up>
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001297_LIBCPP_INLINE_VISIBILITY constexpr
Eric Fiselier3906a132019-06-23 20:28:29 +00001298_EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001299 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() <=
Eric Fiselier8af90142017-03-30 20:06:52 +00001300 _VSTD::declval<const _Up&>()), bool>,
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001301 bool
1302>
Eric Fiselier8af90142017-03-30 20:06:52 +00001303operator<=(const optional<_Tp>& __x, const _Up& __v)
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001304{
1305 return static_cast<bool>(__x) ? *__x <= __v : true;
1306}
1307
Eric Fiselier8af90142017-03-30 20:06:52 +00001308template <class _Tp, class _Up>
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001309_LIBCPP_INLINE_VISIBILITY constexpr
Eric Fiselier3906a132019-06-23 20:28:29 +00001310_EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001311 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() <=
Eric Fiselier8af90142017-03-30 20:06:52 +00001312 _VSTD::declval<const _Up&>()), bool>,
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001313 bool
1314>
Eric Fiselier8af90142017-03-30 20:06:52 +00001315operator<=(const _Tp& __v, const optional<_Up>& __x)
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001316{
1317 return static_cast<bool>(__x) ? __v <= *__x : false;
1318}
1319
Eric Fiselier8af90142017-03-30 20:06:52 +00001320template <class _Tp, class _Up>
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001321_LIBCPP_INLINE_VISIBILITY constexpr
Eric Fiselier3906a132019-06-23 20:28:29 +00001322_EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001323 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() >
Eric Fiselier8af90142017-03-30 20:06:52 +00001324 _VSTD::declval<const _Up&>()), bool>,
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001325 bool
1326>
Eric Fiselier8af90142017-03-30 20:06:52 +00001327operator>(const optional<_Tp>& __x, const _Up& __v)
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001328{
1329 return static_cast<bool>(__x) ? *__x > __v : false;
1330}
1331
Eric Fiselier8af90142017-03-30 20:06:52 +00001332template <class _Tp, class _Up>
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001333_LIBCPP_INLINE_VISIBILITY constexpr
Eric Fiselier3906a132019-06-23 20:28:29 +00001334_EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001335 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() >
Eric Fiselier8af90142017-03-30 20:06:52 +00001336 _VSTD::declval<const _Up&>()), bool>,
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001337 bool
1338>
Eric Fiselier8af90142017-03-30 20:06:52 +00001339operator>(const _Tp& __v, const optional<_Up>& __x)
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001340{
1341 return static_cast<bool>(__x) ? __v > *__x : true;
1342}
1343
Eric Fiselier8af90142017-03-30 20:06:52 +00001344template <class _Tp, class _Up>
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001345_LIBCPP_INLINE_VISIBILITY constexpr
Eric Fiselier3906a132019-06-23 20:28:29 +00001346_EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001347 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() >=
Eric Fiselier8af90142017-03-30 20:06:52 +00001348 _VSTD::declval<const _Up&>()), bool>,
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001349 bool
1350>
Eric Fiselier8af90142017-03-30 20:06:52 +00001351operator>=(const optional<_Tp>& __x, const _Up& __v)
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001352{
1353 return static_cast<bool>(__x) ? *__x >= __v : false;
1354}
1355
Eric Fiselier8af90142017-03-30 20:06:52 +00001356template <class _Tp, class _Up>
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001357_LIBCPP_INLINE_VISIBILITY constexpr
Eric Fiselier3906a132019-06-23 20:28:29 +00001358_EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001359 is_convertible_v<decltype(_VSTD::declval<const _Tp&>() >=
Eric Fiselier8af90142017-03-30 20:06:52 +00001360 _VSTD::declval<const _Up&>()), bool>,
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001361 bool
1362>
Eric Fiselier8af90142017-03-30 20:06:52 +00001363operator>=(const _Tp& __v, const optional<_Up>& __x)
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001364{
1365 return static_cast<bool>(__x) ? __v >= *__x : true;
1366}
1367
1368
1369template <class _Tp>
1370inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier3906a132019-06-23 20:28:29 +00001371_EnableIf<
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001372 is_move_constructible_v<_Tp> && is_swappable_v<_Tp>,
1373 void
1374>
1375swap(optional<_Tp>& __x, optional<_Tp>& __y) noexcept(noexcept(__x.swap(__y)))
1376{
1377 __x.swap(__y);
1378}
1379
1380template <class _Tp>
1381_LIBCPP_INLINE_VISIBILITY constexpr
1382optional<decay_t<_Tp>> make_optional(_Tp&& __v)
1383{
1384 return optional<decay_t<_Tp>>(_VSTD::forward<_Tp>(__v));
1385}
1386
1387template <class _Tp, class... _Args>
1388_LIBCPP_INLINE_VISIBILITY constexpr
1389optional<_Tp> make_optional(_Args&&... __args)
1390{
1391 return optional<_Tp>(in_place, _VSTD::forward<_Args>(__args)...);
1392}
1393
1394template <class _Tp, class _Up, class... _Args>
1395_LIBCPP_INLINE_VISIBILITY constexpr
1396optional<_Tp> make_optional(initializer_list<_Up> __il, _Args&&... __args)
1397{
1398 return optional<_Tp>(in_place, __il, _VSTD::forward<_Args>(__args)...);
1399}
1400
1401template <class _Tp>
Eric Fiselier698a97b2017-01-21 00:02:12 +00001402struct _LIBCPP_TEMPLATE_VIS hash<
1403 __enable_hash_helper<optional<_Tp>, remove_const_t<_Tp>>
1404>
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001405{
1406 typedef optional<_Tp> argument_type;
1407 typedef size_t result_type;
1408
1409 _LIBCPP_INLINE_VISIBILITY
Marshall Clow49036892017-03-23 02:40:28 +00001410 result_type operator()(const argument_type& __opt) const
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001411 {
Eric Fiselier698a97b2017-01-21 00:02:12 +00001412 return static_cast<bool>(__opt) ? hash<remove_const_t<_Tp>>()(*__opt) : 0;
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001413 }
1414};
1415
1416_LIBCPP_END_NAMESPACE_STD
1417
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001418#endif // _LIBCPP_STD_VER > 14
Eric Fiselierd4ec6352016-10-12 07:46:20 +00001419
Eric Fiselierf4433a32017-05-31 22:07:49 +00001420_LIBCPP_POP_MACROS
1421
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001422#endif // _LIBCPP_OPTIONAL