libstdc++
optional
Go to the documentation of this file.
1 // <optional> -*- C++ -*-
2 
3 // Copyright (C) 2013-2021 Free Software Foundation, Inc.
4 // Copyright The GNU Toolchain Authors.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11 
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20 
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
25 
26 /** @file include/optional
27  * This is a Standard C++ Library header.
28  */
29 
30 #ifndef _GLIBCXX_OPTIONAL
31 #define _GLIBCXX_OPTIONAL 1
32 
33 #pragma GCC system_header
34 
35 #if __cplusplus >= 201703L
36 
37 #include <type_traits>
38 #include <exception>
39 #include <new>
40 #include <initializer_list>
42 #include <bits/exception_defines.h>
43 #include <bits/functional_hash.h>
44 #include <bits/stl_construct.h> // _Construct
45 #include <bits/utility.h> // in_place_t
46 #if __cplusplus > 201703L
47 # include <compare>
48 # include <bits/invoke.h> // std::__invoke
49 #endif
50 #if __cplusplus > 202002L
51 # include <concepts>
52 #endif
53 
54 namespace std _GLIBCXX_VISIBILITY(default)
55 {
56 _GLIBCXX_BEGIN_NAMESPACE_VERSION
57 
58  /**
59  * @addtogroup utilities
60  * @{
61  */
62 
63 #if __cplusplus == 201703L
64 # define __cpp_lib_optional 201606L
65 #else
66 # define __cpp_lib_optional 202106L
67 #endif
68 
69  template<typename _Tp>
70  class optional;
71 
72  /// Tag type to disengage optional objects.
73  struct nullopt_t
74  {
75  // Do not user-declare default constructor at all for
76  // optional_value = {} syntax to work.
77  // nullopt_t() = delete;
78 
79  // Used for constructing nullopt.
80  enum class _Construct { _Token };
81 
82  // Must be constexpr for nullopt_t to be literal.
83  explicit constexpr nullopt_t(_Construct) noexcept { }
84  };
85 
86  /// Tag to disengage optional objects.
87  inline constexpr nullopt_t nullopt { nullopt_t::_Construct::_Token };
88 
89  template<typename _Fn> struct _Optional_func { _Fn& _M_f; };
90 
91  /**
92  * @brief Exception class thrown when a disengaged optional object is
93  * dereferenced.
94  * @ingroup exceptions
95  */
97  {
98  public:
99  bad_optional_access() = default;
100  virtual ~bad_optional_access() = default;
101 
102  const char* what() const noexcept override
103  { return "bad optional access"; }
104  };
105 
106  // XXX Does not belong here.
107  [[__noreturn__]] inline void
108  __throw_bad_optional_access()
109  { _GLIBCXX_THROW_OR_ABORT(bad_optional_access()); }
110 
111  // This class template manages construction/destruction of
112  // the contained value for a std::optional.
113  template <typename _Tp>
114  struct _Optional_payload_base
115  {
116  using _Stored_type = remove_const_t<_Tp>;
117 
118  _Optional_payload_base() = default;
119  ~_Optional_payload_base() = default;
120 
121  template<typename... _Args>
122  constexpr
123  _Optional_payload_base(in_place_t __tag, _Args&&... __args)
124  : _M_payload(__tag, std::forward<_Args>(__args)...),
125  _M_engaged(true)
126  { }
127 
128  template<typename _Up, typename... _Args>
129  constexpr
130  _Optional_payload_base(std::initializer_list<_Up> __il,
131  _Args&&... __args)
132  : _M_payload(__il, std::forward<_Args>(__args)...),
133  _M_engaged(true)
134  { }
135 
136  // Constructor used by _Optional_base copy constructor when the
137  // contained value is not trivially copy constructible.
138  constexpr
139  _Optional_payload_base(bool __engaged,
140  const _Optional_payload_base& __other)
141  {
142  if (__other._M_engaged)
143  this->_M_construct(__other._M_get());
144  }
145 
146  // Constructor used by _Optional_base move constructor when the
147  // contained value is not trivially move constructible.
148  constexpr
149  _Optional_payload_base(bool __engaged,
150  _Optional_payload_base&& __other)
151  {
152  if (__other._M_engaged)
153  this->_M_construct(std::move(__other._M_get()));
154  }
155 
156  // Copy constructor is only used to when the contained value is
157  // trivially copy constructible.
158  _Optional_payload_base(const _Optional_payload_base&) = default;
159 
160  // Move constructor is only used to when the contained value is
161  // trivially copy constructible.
162  _Optional_payload_base(_Optional_payload_base&&) = default;
163 
164  _Optional_payload_base&
165  operator=(const _Optional_payload_base&) = default;
166 
167  _Optional_payload_base&
168  operator=(_Optional_payload_base&&) = default;
169 
170  // used to perform non-trivial copy assignment.
171  constexpr void
172  _M_copy_assign(const _Optional_payload_base& __other)
173  {
174  if (this->_M_engaged && __other._M_engaged)
175  this->_M_get() = __other._M_get();
176  else
177  {
178  if (__other._M_engaged)
179  this->_M_construct(__other._M_get());
180  else
181  this->_M_reset();
182  }
183  }
184 
185  // used to perform non-trivial move assignment.
186  constexpr void
187  _M_move_assign(_Optional_payload_base&& __other)
188  noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
189  is_nothrow_move_assignable<_Tp>>)
190  {
191  if (this->_M_engaged && __other._M_engaged)
192  this->_M_get() = std::move(__other._M_get());
193  else
194  {
195  if (__other._M_engaged)
196  this->_M_construct(std::move(__other._M_get()));
197  else
198  this->_M_reset();
199  }
200  }
201 
202  struct _Empty_byte { };
203 
204  template<typename _Up, bool = is_trivially_destructible_v<_Up>>
205  union _Storage
206  {
207  constexpr _Storage() noexcept : _M_empty() { }
208 
209  template<typename... _Args>
210  constexpr
211  _Storage(in_place_t, _Args&&... __args)
212  : _M_value(std::forward<_Args>(__args)...)
213  { }
214 
215  template<typename _Vp, typename... _Args>
216  constexpr
217  _Storage(std::initializer_list<_Vp> __il, _Args&&... __args)
218  : _M_value(__il, std::forward<_Args>(__args)...)
219  { }
220 
221 #if __cplusplus >= 202002L
222  template<typename _Fn, typename _Arg>
223  constexpr
224  _Storage(_Optional_func<_Fn> __f, _Arg&& __arg)
225  : _M_value(std::__invoke(std::forward<_Fn>(__f._M_f),
226  std::forward<_Arg>(__arg)))
227  { }
228 #endif
229 
230  _Empty_byte _M_empty;
231  _Up _M_value;
232  };
233 
234  template<typename _Up>
235  union _Storage<_Up, false>
236  {
237  constexpr _Storage() noexcept : _M_empty() { }
238 
239  template<typename... _Args>
240  constexpr
241  _Storage(in_place_t, _Args&&... __args)
242  : _M_value(std::forward<_Args>(__args)...)
243  { }
244 
245  template<typename _Vp, typename... _Args>
246  constexpr
247  _Storage(std::initializer_list<_Vp> __il, _Args&&... __args)
248  : _M_value(__il, std::forward<_Args>(__args)...)
249  { }
250 
251 #if __cplusplus >= 202002L
252  template<typename _Fn, typename _Arg>
253  constexpr
254  _Storage(_Optional_func<_Fn> __f, _Arg&& __arg)
255  : _M_value(std::__invoke(std::forward<_Fn>(__f._M_f),
256  std::forward<_Arg>(__arg)))
257  { }
258 #endif
259 
260  // User-provided destructor is needed when _Up has non-trivial dtor.
261  _GLIBCXX20_CONSTEXPR ~_Storage() { }
262 
263  _Empty_byte _M_empty;
264  _Up _M_value;
265  };
266 
267  _Storage<_Stored_type> _M_payload;
268 
269  bool _M_engaged = false;
270 
271  template<typename... _Args>
272  constexpr void
273  _M_construct(_Args&&... __args)
274  noexcept(is_nothrow_constructible_v<_Stored_type, _Args...>)
275  {
276  std::_Construct(std::__addressof(this->_M_payload._M_value),
277  std::forward<_Args>(__args)...);
278  this->_M_engaged = true;
279  }
280 
281  constexpr void
282  _M_destroy() noexcept
283  {
284  _M_engaged = false;
285  _M_payload._M_value.~_Stored_type();
286  }
287 
288 #if __cplusplus >= 202002L
289  template<typename _Fn, typename _Up>
290  constexpr void
291  _M_apply(_Optional_func<_Fn> __f, _Up&& __x)
292  {
293  std::construct_at(std::__addressof(this->_M_payload),
294  __f, std::forward<_Up>(__x));
295  _M_engaged = true;
296  }
297 #endif
298 
299  // The _M_get() operations have _M_engaged as a precondition.
300  // They exist to access the contained value with the appropriate
301  // const-qualification, because _M_payload has had the const removed.
302 
303  constexpr _Tp&
304  _M_get() noexcept
305  { return this->_M_payload._M_value; }
306 
307  constexpr const _Tp&
308  _M_get() const noexcept
309  { return this->_M_payload._M_value; }
310 
311  // _M_reset is a 'safe' operation with no precondition.
312  constexpr void
313  _M_reset() noexcept
314  {
315  if (this->_M_engaged)
316  _M_destroy();
317  }
318  };
319 
320  // Class template that manages the payload for optionals.
321  template <typename _Tp,
322  bool /*_HasTrivialDestructor*/ =
323  is_trivially_destructible_v<_Tp>,
324  bool /*_HasTrivialCopy */ =
325  is_trivially_copy_assignable_v<_Tp>
326  && is_trivially_copy_constructible_v<_Tp>,
327  bool /*_HasTrivialMove */ =
328  is_trivially_move_assignable_v<_Tp>
329  && is_trivially_move_constructible_v<_Tp>>
330  struct _Optional_payload;
331 
332  // Payload for potentially-constexpr optionals (trivial copy/move/destroy).
333  template <typename _Tp>
334  struct _Optional_payload<_Tp, true, true, true>
335  : _Optional_payload_base<_Tp>
336  {
337  using _Optional_payload_base<_Tp>::_Optional_payload_base;
338 
339  _Optional_payload() = default;
340  };
341 
342  // Payload for optionals with non-trivial copy construction/assignment.
343  template <typename _Tp>
344  struct _Optional_payload<_Tp, true, false, true>
345  : _Optional_payload_base<_Tp>
346  {
347  using _Optional_payload_base<_Tp>::_Optional_payload_base;
348 
349  _Optional_payload() = default;
350  ~_Optional_payload() = default;
351  _Optional_payload(const _Optional_payload&) = default;
352  _Optional_payload(_Optional_payload&&) = default;
353  _Optional_payload& operator=(_Optional_payload&&) = default;
354 
355  // Non-trivial copy assignment.
356  constexpr
357  _Optional_payload&
358  operator=(const _Optional_payload& __other)
359  {
360  this->_M_copy_assign(__other);
361  return *this;
362  }
363  };
364 
365  // Payload for optionals with non-trivial move construction/assignment.
366  template <typename _Tp>
367  struct _Optional_payload<_Tp, true, true, false>
368  : _Optional_payload_base<_Tp>
369  {
370  using _Optional_payload_base<_Tp>::_Optional_payload_base;
371 
372  _Optional_payload() = default;
373  ~_Optional_payload() = default;
374  _Optional_payload(const _Optional_payload&) = default;
375  _Optional_payload(_Optional_payload&&) = default;
376  _Optional_payload& operator=(const _Optional_payload&) = default;
377 
378  // Non-trivial move assignment.
379  constexpr
380  _Optional_payload&
381  operator=(_Optional_payload&& __other)
382  noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
383  is_nothrow_move_assignable<_Tp>>)
384  {
385  this->_M_move_assign(std::move(__other));
386  return *this;
387  }
388  };
389 
390  // Payload for optionals with non-trivial copy and move assignment.
391  template <typename _Tp>
392  struct _Optional_payload<_Tp, true, false, false>
393  : _Optional_payload_base<_Tp>
394  {
395  using _Optional_payload_base<_Tp>::_Optional_payload_base;
396 
397  _Optional_payload() = default;
398  ~_Optional_payload() = default;
399  _Optional_payload(const _Optional_payload&) = default;
400  _Optional_payload(_Optional_payload&&) = default;
401 
402  // Non-trivial copy assignment.
403  constexpr
404  _Optional_payload&
405  operator=(const _Optional_payload& __other)
406  {
407  this->_M_copy_assign(__other);
408  return *this;
409  }
410 
411  // Non-trivial move assignment.
412  constexpr
413  _Optional_payload&
414  operator=(_Optional_payload&& __other)
415  noexcept(__and_v<is_nothrow_move_constructible<_Tp>,
416  is_nothrow_move_assignable<_Tp>>)
417  {
418  this->_M_move_assign(std::move(__other));
419  return *this;
420  }
421  };
422 
423  // Payload for optionals with non-trivial destructors.
424  template <typename _Tp, bool _Copy, bool _Move>
425  struct _Optional_payload<_Tp, false, _Copy, _Move>
426  : _Optional_payload<_Tp, true, false, false>
427  {
428  // Base class implements all the constructors and assignment operators:
429  using _Optional_payload<_Tp, true, false, false>::_Optional_payload;
430  _Optional_payload() = default;
431  _Optional_payload(const _Optional_payload&) = default;
432  _Optional_payload(_Optional_payload&&) = default;
433  _Optional_payload& operator=(const _Optional_payload&) = default;
434  _Optional_payload& operator=(_Optional_payload&&) = default;
435 
436  // Destructor needs to destroy the contained value:
437  _GLIBCXX20_CONSTEXPR ~_Optional_payload() { this->_M_reset(); }
438  };
439 
440  // Common base class for _Optional_base<T> to avoid repeating these
441  // member functions in each specialization.
442  template<typename _Tp, typename _Dp>
443  class _Optional_base_impl
444  {
445  protected:
446  using _Stored_type = remove_const_t<_Tp>;
447 
448  // The _M_construct operation has !_M_engaged as a precondition
449  // while _M_destruct has _M_engaged as a precondition.
450  template<typename... _Args>
451  constexpr void
452  _M_construct(_Args&&... __args)
453  noexcept(is_nothrow_constructible_v<_Stored_type, _Args...>)
454  {
455  static_cast<_Dp*>(this)->_M_payload._M_construct(
456  std::forward<_Args>(__args)...);
457  }
458 
459  constexpr void
460  _M_destruct() noexcept
461  { static_cast<_Dp*>(this)->_M_payload._M_destroy(); }
462 
463  // _M_reset is a 'safe' operation with no precondition.
464  constexpr void
465  _M_reset() noexcept
466  { static_cast<_Dp*>(this)->_M_payload._M_reset(); }
467 
468  constexpr bool _M_is_engaged() const noexcept
469  { return static_cast<const _Dp*>(this)->_M_payload._M_engaged; }
470 
471  // The _M_get operations have _M_engaged as a precondition.
472  constexpr _Tp&
473  _M_get() noexcept
474  {
475  __glibcxx_assert(this->_M_is_engaged());
476  return static_cast<_Dp*>(this)->_M_payload._M_get();
477  }
478 
479  constexpr const _Tp&
480  _M_get() const noexcept
481  {
482  __glibcxx_assert(this->_M_is_engaged());
483  return static_cast<const _Dp*>(this)->_M_payload._M_get();
484  }
485  };
486 
487  /**
488  * @brief Class template that provides copy/move constructors of optional.
489  *
490  * Such a separate base class template is necessary in order to
491  * conditionally make copy/move constructors trivial.
492  *
493  * When the contained value is trivially copy/move constructible,
494  * the copy/move constructors of _Optional_base will invoke the
495  * trivial copy/move constructor of _Optional_payload. Otherwise,
496  * they will invoke _Optional_payload(bool, const _Optional_payload&)
497  * or _Optional_payload(bool, _Optional_payload&&) to initialize
498  * the contained value, if copying/moving an engaged optional.
499  *
500  * Whether the other special members are trivial is determined by the
501  * _Optional_payload<_Tp> specialization used for the _M_payload member.
502  *
503  * @see optional, _Enable_special_members
504  */
505  template<typename _Tp,
506  bool = is_trivially_copy_constructible_v<_Tp>,
507  bool = is_trivially_move_constructible_v<_Tp>>
509  : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
510  {
511  // Constructors for disengaged optionals.
512  constexpr _Optional_base() = default;
513 
514  // Constructors for engaged optionals.
515  template<typename... _Args,
516  enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
517  constexpr explicit
518  _Optional_base(in_place_t, _Args&&... __args)
519  : _M_payload(in_place, std::forward<_Args>(__args)...)
520  { }
521 
522  template<typename _Up, typename... _Args,
523  enable_if_t<is_constructible_v<_Tp,
525  _Args...>, bool> = false>
526  constexpr explicit
527  _Optional_base(in_place_t,
529  _Args&&... __args)
530  : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
531  { }
532 
533  // Copy and move constructors.
534  constexpr
535  _Optional_base(const _Optional_base& __other)
536  : _M_payload(__other._M_payload._M_engaged, __other._M_payload)
537  { }
538 
539  constexpr
540  _Optional_base(_Optional_base&& __other)
541  noexcept(is_nothrow_move_constructible_v<_Tp>)
542  : _M_payload(__other._M_payload._M_engaged,
543  std::move(__other._M_payload))
544  { }
545 
546  // Assignment operators.
547  _Optional_base& operator=(const _Optional_base&) = default;
548  _Optional_base& operator=(_Optional_base&&) = default;
549 
550  _Optional_payload<_Tp> _M_payload;
551  };
552 
553  template<typename _Tp>
555  : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
556  {
557  // Constructors for disengaged optionals.
558  constexpr _Optional_base() = default;
559 
560  // Constructors for engaged optionals.
561  template<typename... _Args,
562  enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
563  constexpr explicit
564  _Optional_base(in_place_t, _Args&&... __args)
565  : _M_payload(in_place, std::forward<_Args>(__args)...)
566  { }
567 
568  template<typename _Up, typename... _Args,
569  enable_if_t<is_constructible_v<_Tp,
570  initializer_list<_Up>&,
571  _Args...>, bool> = false>
572  constexpr explicit
573  _Optional_base(in_place_t,
574  initializer_list<_Up> __il,
575  _Args... __args)
576  : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
577  { }
578 
579  // Copy and move constructors.
580  constexpr _Optional_base(const _Optional_base& __other)
581  : _M_payload(__other._M_payload._M_engaged, __other._M_payload)
582  { }
583 
584  constexpr _Optional_base(_Optional_base&& __other) = default;
585 
586  // Assignment operators.
587  _Optional_base& operator=(const _Optional_base&) = default;
588  _Optional_base& operator=(_Optional_base&&) = default;
589 
590  _Optional_payload<_Tp> _M_payload;
591  };
592 
593  template<typename _Tp>
594  struct _Optional_base<_Tp, true, false>
595  : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
596  {
597  // Constructors for disengaged optionals.
598  constexpr _Optional_base() = default;
599 
600  // Constructors for engaged optionals.
601  template<typename... _Args,
602  enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
603  constexpr explicit
604  _Optional_base(in_place_t, _Args&&... __args)
605  : _M_payload(in_place, std::forward<_Args>(__args)...)
606  { }
607 
608  template<typename _Up, typename... _Args,
609  enable_if_t<is_constructible_v<_Tp,
610  initializer_list<_Up>&,
611  _Args...>, bool> = false>
612  constexpr explicit
613  _Optional_base(in_place_t,
614  initializer_list<_Up> __il,
615  _Args&&... __args)
616  : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
617  { }
618 
619  // Copy and move constructors.
620  constexpr _Optional_base(const _Optional_base& __other) = default;
621 
622  constexpr
623  _Optional_base(_Optional_base&& __other)
624  noexcept(is_nothrow_move_constructible_v<_Tp>)
625  : _M_payload(__other._M_payload._M_engaged,
626  std::move(__other._M_payload))
627  { }
628 
629  // Assignment operators.
630  _Optional_base& operator=(const _Optional_base&) = default;
631  _Optional_base& operator=(_Optional_base&&) = default;
632 
633  _Optional_payload<_Tp> _M_payload;
634  };
635 
636  template<typename _Tp>
637  struct _Optional_base<_Tp, true, true>
638  : _Optional_base_impl<_Tp, _Optional_base<_Tp>>
639  {
640  // Constructors for disengaged optionals.
641  constexpr _Optional_base() = default;
642 
643  // Constructors for engaged optionals.
644  template<typename... _Args,
645  enable_if_t<is_constructible_v<_Tp, _Args...>, bool> = false>
646  constexpr explicit
647  _Optional_base(in_place_t, _Args&&... __args)
648  : _M_payload(in_place, std::forward<_Args>(__args)...)
649  { }
650 
651  template<typename _Up, typename... _Args,
652  enable_if_t<is_constructible_v<_Tp,
653  initializer_list<_Up>&,
654  _Args...>, bool> = false>
655  constexpr explicit
656  _Optional_base(in_place_t,
657  initializer_list<_Up> __il,
658  _Args&&... __args)
659  : _M_payload(in_place, __il, std::forward<_Args>(__args)...)
660  { }
661 
662  // Copy and move constructors.
663  constexpr _Optional_base(const _Optional_base& __other) = default;
664  constexpr _Optional_base(_Optional_base&& __other) = default;
665 
666  // Assignment operators.
667  _Optional_base& operator=(const _Optional_base&) = default;
668  _Optional_base& operator=(_Optional_base&&) = default;
669 
670  _Optional_payload<_Tp> _M_payload;
671  };
672 
673  template<typename _Tp>
674  class optional;
675 
676  template<typename _Tp>
677  inline constexpr bool __is_optional_v = false;
678  template<typename _Tp>
679  inline constexpr bool __is_optional_v<optional<_Tp>> = true;
680 
681  template<typename _Tp, typename _Up>
682  using __converts_from_optional =
683  __or_<is_constructible<_Tp, const optional<_Up>&>,
684  is_constructible<_Tp, optional<_Up>&>,
685  is_constructible<_Tp, const optional<_Up>&&>,
686  is_constructible<_Tp, optional<_Up>&&>,
687  is_convertible<const optional<_Up>&, _Tp>,
688  is_convertible<optional<_Up>&, _Tp>,
689  is_convertible<const optional<_Up>&&, _Tp>,
690  is_convertible<optional<_Up>&&, _Tp>>;
691 
692  template<typename _Tp, typename _Up>
693  using __assigns_from_optional =
694  __or_<is_assignable<_Tp&, const optional<_Up>&>,
695  is_assignable<_Tp&, optional<_Up>&>,
696  is_assignable<_Tp&, const optional<_Up>&&>,
697  is_assignable<_Tp&, optional<_Up>&&>>;
698 
699  /**
700  * @brief Class template for optional values.
701  */
702  template<typename _Tp>
703  class optional
704  : private _Optional_base<_Tp>,
705  private _Enable_copy_move<
706  // Copy constructor.
707  is_copy_constructible_v<_Tp>,
708  // Copy assignment.
709  __and_v<is_copy_constructible<_Tp>, is_copy_assignable<_Tp>>,
710  // Move constructor.
711  is_move_constructible_v<_Tp>,
712  // Move assignment.
713  __and_v<is_move_constructible<_Tp>, is_move_assignable<_Tp>>,
714  // Unique tag type.
715  optional<_Tp>>
716  {
717  static_assert(!is_same_v<remove_cv_t<_Tp>, nullopt_t>);
718  static_assert(!is_same_v<remove_cv_t<_Tp>, in_place_t>);
719  static_assert(!is_reference_v<_Tp>);
720 
721  private:
722  using _Base = _Optional_base<_Tp>;
723 
724  // SFINAE helpers
725  template<typename _Up>
726  using __not_self = __not_<is_same<optional, __remove_cvref_t<_Up>>>;
727  template<typename _Up>
728  using __not_tag = __not_<is_same<in_place_t, __remove_cvref_t<_Up>>>;
729  template<typename... _Cond>
730  using _Requires = enable_if_t<__and_v<_Cond...>, bool>;
731 
732  public:
733  using value_type = _Tp;
734 
735  constexpr optional() noexcept { }
736 
737  constexpr optional(nullopt_t) noexcept { }
738 
739  // Converting constructors for engaged optionals.
740  template<typename _Up = _Tp,
741  _Requires<__not_self<_Up>, __not_tag<_Up>,
743  is_convertible<_Up, _Tp>> = true>
744  constexpr
745  optional(_Up&& __t)
746  noexcept(is_nothrow_constructible_v<_Tp, _Up>)
747  : _Base(std::in_place, std::forward<_Up>(__t)) { }
748 
749  template<typename _Up = _Tp,
750  _Requires<__not_self<_Up>, __not_tag<_Up>,
752  __not_<is_convertible<_Up, _Tp>>> = false>
753  explicit constexpr
754  optional(_Up&& __t)
755  noexcept(is_nothrow_constructible_v<_Tp, _Up>)
756  : _Base(std::in_place, std::forward<_Up>(__t)) { }
757 
758  template<typename _Up,
759  _Requires<__not_<is_same<_Tp, _Up>>,
762  __not_<__converts_from_optional<_Tp, _Up>>> = true>
763  constexpr
764  optional(const optional<_Up>& __t)
765  noexcept(is_nothrow_constructible_v<_Tp, const _Up&>)
766  {
767  if (__t)
768  emplace(*__t);
769  }
770 
771  template<typename _Up,
772  _Requires<__not_<is_same<_Tp, _Up>>,
774  __not_<is_convertible<const _Up&, _Tp>>,
775  __not_<__converts_from_optional<_Tp, _Up>>> = false>
776  explicit constexpr
777  optional(const optional<_Up>& __t)
778  noexcept(is_nothrow_constructible_v<_Tp, const _Up&>)
779  {
780  if (__t)
781  emplace(*__t);
782  }
783 
784  template<typename _Up,
785  _Requires<__not_<is_same<_Tp, _Up>>,
788  __not_<__converts_from_optional<_Tp, _Up>>> = true>
789  constexpr
790  optional(optional<_Up>&& __t)
791  noexcept(is_nothrow_constructible_v<_Tp, _Up>)
792  {
793  if (__t)
794  emplace(std::move(*__t));
795  }
796 
797  template<typename _Up,
798  _Requires<__not_<is_same<_Tp, _Up>>,
800  __not_<is_convertible<_Up, _Tp>>,
801  __not_<__converts_from_optional<_Tp, _Up>>> = false>
802  explicit constexpr
803  optional(optional<_Up>&& __t)
804  noexcept(is_nothrow_constructible_v<_Tp, _Up>)
805  {
806  if (__t)
807  emplace(std::move(*__t));
808  }
809 
810  template<typename... _Args,
811  _Requires<is_constructible<_Tp, _Args...>> = false>
812  explicit constexpr
813  optional(in_place_t, _Args&&... __args)
814  noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
815  : _Base(std::in_place, std::forward<_Args>(__args)...) { }
816 
817  template<typename _Up, typename... _Args,
818  _Requires<is_constructible<_Tp,
820  _Args...>> = false>
821  explicit constexpr
822  optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
823  noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
824  _Args...>)
825  : _Base(std::in_place, __il, std::forward<_Args>(__args)...) { }
826 
827 
828  // Assignment operators.
829  _GLIBCXX20_CONSTEXPR optional&
830  operator=(nullopt_t) noexcept
831  {
832  this->_M_reset();
833  return *this;
834  }
835 
836  template<typename _Up = _Tp>
837  _GLIBCXX20_CONSTEXPR
839  __not_<__and_<is_scalar<_Tp>,
843  optional&>
844  operator=(_Up&& __u)
845  noexcept(__and_v<is_nothrow_constructible<_Tp, _Up>,
847  {
848  if (this->_M_is_engaged())
849  this->_M_get() = std::forward<_Up>(__u);
850  else
851  this->_M_construct(std::forward<_Up>(__u));
852 
853  return *this;
854  }
855 
856  template<typename _Up>
857  _GLIBCXX20_CONSTEXPR
861  __not_<__converts_from_optional<_Tp, _Up>>,
862  __not_<__assigns_from_optional<_Tp, _Up>>>,
863  optional&>
864  operator=(const optional<_Up>& __u)
867  {
868  if (__u)
869  {
870  if (this->_M_is_engaged())
871  this->_M_get() = *__u;
872  else
873  this->_M_construct(*__u);
874  }
875  else
876  {
877  this->_M_reset();
878  }
879  return *this;
880  }
881 
882  template<typename _Up>
883  _GLIBCXX20_CONSTEXPR
887  __not_<__converts_from_optional<_Tp, _Up>>,
888  __not_<__assigns_from_optional<_Tp, _Up>>>,
889  optional&>
890  operator=(optional<_Up>&& __u)
891  noexcept(__and_v<is_nothrow_constructible<_Tp, _Up>,
893  {
894  if (__u)
895  {
896  if (this->_M_is_engaged())
897  this->_M_get() = std::move(*__u);
898  else
899  this->_M_construct(std::move(*__u));
900  }
901  else
902  {
903  this->_M_reset();
904  }
905 
906  return *this;
907  }
908 
909  template<typename... _Args>
910  _GLIBCXX20_CONSTEXPR
911  enable_if_t<is_constructible_v<_Tp, _Args...>, _Tp&>
912  emplace(_Args&&... __args)
913  noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
914  {
915  this->_M_reset();
916  this->_M_construct(std::forward<_Args>(__args)...);
917  return this->_M_get();
918  }
919 
920  template<typename _Up, typename... _Args>
921  _GLIBCXX20_CONSTEXPR
923  _Tp&>
924  emplace(initializer_list<_Up> __il, _Args&&... __args)
925  noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&,
926  _Args...>)
927  {
928  this->_M_reset();
929  this->_M_construct(__il, std::forward<_Args>(__args)...);
930  return this->_M_get();
931  }
932 
933  // Destructor is implicit, implemented in _Optional_base.
934 
935  // Swap.
936  _GLIBCXX20_CONSTEXPR void
937  swap(optional& __other)
938  noexcept(is_nothrow_move_constructible_v<_Tp>
939  && is_nothrow_swappable_v<_Tp>)
940  {
941  using std::swap;
942 
943  if (this->_M_is_engaged() && __other._M_is_engaged())
944  swap(this->_M_get(), __other._M_get());
945  else if (this->_M_is_engaged())
946  {
947  __other._M_construct(std::move(this->_M_get()));
948  this->_M_destruct();
949  }
950  else if (__other._M_is_engaged())
951  {
952  this->_M_construct(std::move(__other._M_get()));
953  __other._M_destruct();
954  }
955  }
956 
957  // Observers.
958  constexpr const _Tp*
959  operator->() const noexcept
960  { return std::__addressof(this->_M_get()); }
961 
962  constexpr _Tp*
963  operator->() noexcept
964  { return std::__addressof(this->_M_get()); }
965 
966  constexpr const _Tp&
967  operator*() const& noexcept
968  { return this->_M_get(); }
969 
970  constexpr _Tp&
971  operator*()& noexcept
972  { return this->_M_get(); }
973 
974  constexpr _Tp&&
975  operator*()&& noexcept
976  { return std::move(this->_M_get()); }
977 
978  constexpr const _Tp&&
979  operator*() const&& noexcept
980  { return std::move(this->_M_get()); }
981 
982  constexpr explicit operator bool() const noexcept
983  { return this->_M_is_engaged(); }
984 
985  constexpr bool has_value() const noexcept
986  { return this->_M_is_engaged(); }
987 
988  constexpr const _Tp&
989  value() const&
990  {
991  if (this->_M_is_engaged())
992  return this->_M_get();
993  __throw_bad_optional_access();
994  }
995 
996  constexpr _Tp&
997  value()&
998  {
999  if (this->_M_is_engaged())
1000  return this->_M_get();
1001  __throw_bad_optional_access();
1002  }
1003 
1004  constexpr _Tp&&
1005  value()&&
1006  {
1007  if (this->_M_is_engaged())
1008  return std::move(this->_M_get());
1009  __throw_bad_optional_access();
1010  }
1011 
1012  constexpr const _Tp&&
1013  value() const&&
1014  {
1015  if (this->_M_is_engaged())
1016  return std::move(this->_M_get());
1017  __throw_bad_optional_access();
1018  }
1019 
1020  template<typename _Up>
1021  constexpr _Tp
1022  value_or(_Up&& __u) const&
1023  {
1024  static_assert(is_copy_constructible_v<_Tp>);
1025  static_assert(is_convertible_v<_Up&&, _Tp>);
1026 
1027  if (this->_M_is_engaged())
1028  return this->_M_get();
1029  else
1030  return static_cast<_Tp>(std::forward<_Up>(__u));
1031  }
1032 
1033  template<typename _Up>
1034  constexpr _Tp
1035  value_or(_Up&& __u) &&
1036  {
1037  static_assert(is_move_constructible_v<_Tp>);
1038  static_assert(is_convertible_v<_Up&&, _Tp>);
1039 
1040  if (this->_M_is_engaged())
1041  return std::move(this->_M_get());
1042  else
1043  return static_cast<_Tp>(std::forward<_Up>(__u));
1044  }
1045 
1046 #if __cplusplus > 202002L && __cpp_lib_concepts
1047 #define __cpp_lib_monadic_optional 202110L
1048 
1049  // [optional.monadic]
1050 
1051  template<typename _Fn>
1052  constexpr auto
1053  and_then(_Fn&& __f) &
1054  {
1056  static_assert(__is_optional_v<remove_cvref_t<_Up>>);
1057  if (has_value())
1058  return std::__invoke(std::forward<_Fn>(__f), **this);
1059  else
1060  return _Up();
1061  }
1062 
1063  template<typename _Fn>
1064  constexpr auto
1065  and_then(_Fn&& __f) const &
1066  {
1068  static_assert(__is_optional_v<_Up>);
1069  if (has_value())
1070  return std::__invoke(std::forward<_Fn>(__f), **this);
1071  else
1072  return _Up();
1073  }
1074 
1075  template<typename _Fn>
1076  constexpr auto
1077  and_then(_Fn&& __f) &&
1078  {
1080  static_assert(__is_optional_v<remove_cvref_t<_Up>>);
1081  if (has_value())
1082  return std::__invoke(std::forward<_Fn>(__f), std::move(**this));
1083  else
1084  return _Up();
1085  }
1086 
1087  template<typename _Fn>
1088  constexpr auto
1089  and_then(_Fn&& __f) const &&
1090  {
1092  static_assert(__is_optional_v<remove_cvref_t<_Up>>);
1093  if (has_value())
1094  return std::__invoke(std::forward<_Fn>(__f), std::move(**this));
1095  else
1096  return _Up();
1097  }
1098 
1099  template<typename _Fn>
1100  constexpr auto
1101  transform(_Fn&& __f) &
1102  {
1103  using _Up = invoke_result_t<_Fn, _Tp&>;
1104  if (has_value())
1105  return optional<_Up>(_Optional_func<_Fn>{__f}, **this);
1106  else
1107  return optional<_Up>();
1108  }
1109 
1110  template<typename _Fn>
1111  constexpr auto
1112  transform(_Fn&& __f) const &
1113  {
1115  if (has_value())
1116  return optional<_Up>(_Optional_func<_Fn>{__f}, **this);
1117  else
1118  return optional<_Up>();
1119  }
1120 
1121  template<typename _Fn>
1122  constexpr auto
1123  transform(_Fn&& __f) &&
1124  {
1125  using _Up = invoke_result_t<_Fn, _Tp>;
1126  if (has_value())
1127  return optional<_Up>(_Optional_func<_Fn>{__f}, std::move(**this));
1128  else
1129  return optional<_Up>();
1130  }
1131 
1132  template<typename _Fn>
1133  constexpr auto
1134  transform(_Fn&& __f) const &&
1135  {
1136  using _Up = invoke_result_t<_Fn, const _Tp>;
1137  if (has_value())
1138  return optional<_Up>(_Optional_func<_Fn>{__f}, std::move(**this));
1139  else
1140  return optional<_Up>();
1141  }
1142 
1143  template<typename _Fn> requires invocable<_Fn> && copy_constructible<_Tp>
1144  constexpr optional
1145  or_else(_Fn&& __f) const&
1146  {
1147  using _Up = invoke_result_t<_Fn>;
1148  static_assert( is_same_v<remove_cvref_t<_Up>, optional> );
1149 
1150  if (has_value())
1151  return *this;
1152  else
1153  return std::forward<_Fn>(__f)();
1154  }
1155 
1156  template<typename _Fn> requires invocable<_Fn> && move_constructible<_Tp>
1157  constexpr optional
1158  or_else(_Fn&& __f) &&
1159  {
1160  using _Up = invoke_result_t<_Fn>;
1161  static_assert( is_same_v<remove_cvref_t<_Up>, optional> );
1162 
1163  if (has_value())
1164  return std::move(*this);
1165  else
1166  return std::forward<_Fn>(__f)();
1167  }
1168 #endif
1169 
1170  _GLIBCXX20_CONSTEXPR void reset() noexcept { this->_M_reset(); }
1171 
1172  private:
1173 #if __cplusplus >= 202002L
1174  template<typename _Up> friend class optional;
1175 
1176  template<typename _Fn, typename _Value>
1177  explicit constexpr
1178  optional(_Optional_func<_Fn> __f, _Value&& __v)
1179  {
1180  this->_M_payload._M_apply(__f, std::forward<_Value>(__v));
1181  }
1182 #endif
1183  };
1184 
1185  template<typename _Tp>
1186  using __optional_relop_t =
1188 
1189  template<typename _Tp, typename _Up>
1190  using __optional_eq_t = __optional_relop_t<
1191  decltype(std::declval<const _Tp&>() == std::declval<const _Up&>())
1192  >;
1193 
1194  template<typename _Tp, typename _Up>
1195  using __optional_ne_t = __optional_relop_t<
1196  decltype(std::declval<const _Tp&>() != std::declval<const _Up&>())
1197  >;
1198 
1199  template<typename _Tp, typename _Up>
1200  using __optional_lt_t = __optional_relop_t<
1201  decltype(std::declval<const _Tp&>() < std::declval<const _Up&>())
1202  >;
1203 
1204  template<typename _Tp, typename _Up>
1205  using __optional_gt_t = __optional_relop_t<
1206  decltype(std::declval<const _Tp&>() > std::declval<const _Up&>())
1207  >;
1208 
1209  template<typename _Tp, typename _Up>
1210  using __optional_le_t = __optional_relop_t<
1211  decltype(std::declval<const _Tp&>() <= std::declval<const _Up&>())
1212  >;
1213 
1214  template<typename _Tp, typename _Up>
1215  using __optional_ge_t = __optional_relop_t<
1216  decltype(std::declval<const _Tp&>() >= std::declval<const _Up&>())
1217  >;
1218 
1219  // Comparisons between optional values.
1220  template<typename _Tp, typename _Up>
1221  constexpr auto
1222  operator==(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1223  -> __optional_eq_t<_Tp, _Up>
1224  {
1225  return static_cast<bool>(__lhs) == static_cast<bool>(__rhs)
1226  && (!__lhs || *__lhs == *__rhs);
1227  }
1228 
1229  template<typename _Tp, typename _Up>
1230  constexpr auto
1231  operator!=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1232  -> __optional_ne_t<_Tp, _Up>
1233  {
1234  return static_cast<bool>(__lhs) != static_cast<bool>(__rhs)
1235  || (static_cast<bool>(__lhs) && *__lhs != *__rhs);
1236  }
1237 
1238  template<typename _Tp, typename _Up>
1239  constexpr auto
1240  operator<(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1241  -> __optional_lt_t<_Tp, _Up>
1242  {
1243  return static_cast<bool>(__rhs) && (!__lhs || *__lhs < *__rhs);
1244  }
1245 
1246  template<typename _Tp, typename _Up>
1247  constexpr auto
1248  operator>(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1249  -> __optional_gt_t<_Tp, _Up>
1250  {
1251  return static_cast<bool>(__lhs) && (!__rhs || *__lhs > *__rhs);
1252  }
1253 
1254  template<typename _Tp, typename _Up>
1255  constexpr auto
1256  operator<=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1257  -> __optional_le_t<_Tp, _Up>
1258  {
1259  return !__lhs || (static_cast<bool>(__rhs) && *__lhs <= *__rhs);
1260  }
1261 
1262  template<typename _Tp, typename _Up>
1263  constexpr auto
1264  operator>=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
1265  -> __optional_ge_t<_Tp, _Up>
1266  {
1267  return !__rhs || (static_cast<bool>(__lhs) && *__lhs >= *__rhs);
1268  }
1269 
1270 #ifdef __cpp_lib_three_way_comparison
1271  template<typename _Tp, three_way_comparable_with<_Tp> _Up>
1272  constexpr compare_three_way_result_t<_Tp, _Up>
1273  operator<=>(const optional<_Tp>& __x, const optional<_Up>& __y)
1274  {
1275  return __x && __y ? *__x <=> *__y : bool(__x) <=> bool(__y);
1276  }
1277 #endif
1278 
1279  // Comparisons with nullopt.
1280  template<typename _Tp>
1281  constexpr bool
1282  operator==(const optional<_Tp>& __lhs, nullopt_t) noexcept
1283  { return !__lhs; }
1284 
1285 #ifdef __cpp_lib_three_way_comparison
1286  template<typename _Tp>
1287  constexpr strong_ordering
1288  operator<=>(const optional<_Tp>& __x, nullopt_t) noexcept
1289  { return bool(__x) <=> false; }
1290 #else
1291  template<typename _Tp>
1292  constexpr bool
1293  operator==(nullopt_t, const optional<_Tp>& __rhs) noexcept
1294  { return !__rhs; }
1295 
1296  template<typename _Tp>
1297  constexpr bool
1298  operator!=(const optional<_Tp>& __lhs, nullopt_t) noexcept
1299  { return static_cast<bool>(__lhs); }
1300 
1301  template<typename _Tp>
1302  constexpr bool
1303  operator!=(nullopt_t, const optional<_Tp>& __rhs) noexcept
1304  { return static_cast<bool>(__rhs); }
1305 
1306  template<typename _Tp>
1307  constexpr bool
1308  operator<(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
1309  { return false; }
1310 
1311  template<typename _Tp>
1312  constexpr bool
1313  operator<(nullopt_t, const optional<_Tp>& __rhs) noexcept
1314  { return static_cast<bool>(__rhs); }
1315 
1316  template<typename _Tp>
1317  constexpr bool
1318  operator>(const optional<_Tp>& __lhs, nullopt_t) noexcept
1319  { return static_cast<bool>(__lhs); }
1320 
1321  template<typename _Tp>
1322  constexpr bool
1323  operator>(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
1324  { return false; }
1325 
1326  template<typename _Tp>
1327  constexpr bool
1328  operator<=(const optional<_Tp>& __lhs, nullopt_t) noexcept
1329  { return !__lhs; }
1330 
1331  template<typename _Tp>
1332  constexpr bool
1333  operator<=(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
1334  { return true; }
1335 
1336  template<typename _Tp>
1337  constexpr bool
1338  operator>=(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
1339  { return true; }
1340 
1341  template<typename _Tp>
1342  constexpr bool
1343  operator>=(nullopt_t, const optional<_Tp>& __rhs) noexcept
1344  { return !__rhs; }
1345 #endif // three-way-comparison
1346 
1347  // Comparisons with value type.
1348  template<typename _Tp, typename _Up>
1349  constexpr auto
1350  operator==(const optional<_Tp>& __lhs, const _Up& __rhs)
1351  -> __optional_eq_t<_Tp, _Up>
1352  { return __lhs && *__lhs == __rhs; }
1353 
1354  template<typename _Tp, typename _Up>
1355  constexpr auto
1356  operator==(const _Up& __lhs, const optional<_Tp>& __rhs)
1357  -> __optional_eq_t<_Up, _Tp>
1358  { return __rhs && __lhs == *__rhs; }
1359 
1360  template<typename _Tp, typename _Up>
1361  constexpr auto
1362  operator!=(const optional<_Tp>& __lhs, const _Up& __rhs)
1363  -> __optional_ne_t<_Tp, _Up>
1364  { return !__lhs || *__lhs != __rhs; }
1365 
1366  template<typename _Tp, typename _Up>
1367  constexpr auto
1368  operator!=(const _Up& __lhs, const optional<_Tp>& __rhs)
1369  -> __optional_ne_t<_Up, _Tp>
1370  { return !__rhs || __lhs != *__rhs; }
1371 
1372  template<typename _Tp, typename _Up>
1373  constexpr auto
1374  operator<(const optional<_Tp>& __lhs, const _Up& __rhs)
1375  -> __optional_lt_t<_Tp, _Up>
1376  { return !__lhs || *__lhs < __rhs; }
1377 
1378  template<typename _Tp, typename _Up>
1379  constexpr auto
1380  operator<(const _Up& __lhs, const optional<_Tp>& __rhs)
1381  -> __optional_lt_t<_Up, _Tp>
1382  { return __rhs && __lhs < *__rhs; }
1383 
1384  template<typename _Tp, typename _Up>
1385  constexpr auto
1386  operator>(const optional<_Tp>& __lhs, const _Up& __rhs)
1387  -> __optional_gt_t<_Tp, _Up>
1388  { return __lhs && *__lhs > __rhs; }
1389 
1390  template<typename _Tp, typename _Up>
1391  constexpr auto
1392  operator>(const _Up& __lhs, const optional<_Tp>& __rhs)
1393  -> __optional_gt_t<_Up, _Tp>
1394  { return !__rhs || __lhs > *__rhs; }
1395 
1396  template<typename _Tp, typename _Up>
1397  constexpr auto
1398  operator<=(const optional<_Tp>& __lhs, const _Up& __rhs)
1399  -> __optional_le_t<_Tp, _Up>
1400  { return !__lhs || *__lhs <= __rhs; }
1401 
1402  template<typename _Tp, typename _Up>
1403  constexpr auto
1404  operator<=(const _Up& __lhs, const optional<_Tp>& __rhs)
1405  -> __optional_le_t<_Up, _Tp>
1406  { return __rhs && __lhs <= *__rhs; }
1407 
1408  template<typename _Tp, typename _Up>
1409  constexpr auto
1410  operator>=(const optional<_Tp>& __lhs, const _Up& __rhs)
1411  -> __optional_ge_t<_Tp, _Up>
1412  { return __lhs && *__lhs >= __rhs; }
1413 
1414  template<typename _Tp, typename _Up>
1415  constexpr auto
1416  operator>=(const _Up& __lhs, const optional<_Tp>& __rhs)
1417  -> __optional_ge_t<_Up, _Tp>
1418  { return !__rhs || __lhs >= *__rhs; }
1419 
1420 #ifdef __cpp_lib_three_way_comparison
1421  template<typename _Tp, typename _Up>
1422  requires (!__is_optional_v<_Up>)
1423  && three_way_comparable_with<_Tp, _Up>
1424  constexpr compare_three_way_result_t<_Tp, _Up>
1425  operator<=>(const optional<_Tp>& __x, const _Up& __v)
1426  { return bool(__x) ? *__x <=> __v : strong_ordering::less; }
1427 #endif
1428 
1429  // Swap and creation functions.
1430 
1431  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1432  // 2748. swappable traits for optionals
1433  template<typename _Tp>
1434  _GLIBCXX20_CONSTEXPR
1435  inline enable_if_t<is_move_constructible_v<_Tp> && is_swappable_v<_Tp>>
1436  swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs)
1437  noexcept(noexcept(__lhs.swap(__rhs)))
1438  { __lhs.swap(__rhs); }
1439 
1440  template<typename _Tp>
1441  enable_if_t<!(is_move_constructible_v<_Tp> && is_swappable_v<_Tp>)>
1442  swap(optional<_Tp>&, optional<_Tp>&) = delete;
1443 
1444  template<typename _Tp>
1445  constexpr
1446  enable_if_t<is_constructible_v<decay_t<_Tp>, _Tp>,
1447  optional<decay_t<_Tp>>>
1448  make_optional(_Tp&& __t)
1449  noexcept(is_nothrow_constructible_v<optional<decay_t<_Tp>>, _Tp>)
1450  { return optional<decay_t<_Tp>>{ std::forward<_Tp>(__t) }; }
1451 
1452  template<typename _Tp, typename... _Args>
1453  constexpr
1454  enable_if_t<is_constructible_v<_Tp, _Args...>,
1455  optional<_Tp>>
1456  make_optional(_Args&&... __args)
1457  noexcept(is_nothrow_constructible_v<_Tp, _Args...>)
1458  { return optional<_Tp>{ in_place, std::forward<_Args>(__args)... }; }
1459 
1460  template<typename _Tp, typename _Up, typename... _Args>
1461  constexpr
1462  enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>,
1463  optional<_Tp>>
1464  make_optional(initializer_list<_Up> __il, _Args&&... __args)
1465  noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>)
1466  { return optional<_Tp>{ in_place, __il, std::forward<_Args>(__args)... }; }
1467 
1468  // Hash.
1469 
1470  template<typename _Tp, typename _Up = remove_const_t<_Tp>,
1471  bool = __poison_hash<_Up>::__enable_hash_call>
1472  struct __optional_hash_call_base
1473  {
1474  size_t
1475  operator()(const optional<_Tp>& __t) const
1476  noexcept(noexcept(hash<_Up>{}(*__t)))
1477  {
1478  // We pick an arbitrary hash for disengaged optionals which hopefully
1479  // usual values of _Tp won't typically hash to.
1480  constexpr size_t __magic_disengaged_hash = static_cast<size_t>(-3333);
1481  return __t ? hash<_Up>{}(*__t) : __magic_disengaged_hash;
1482  }
1483  };
1484 
1485  template<typename _Tp, typename _Up>
1486  struct __optional_hash_call_base<_Tp, _Up, false> {};
1487 
1488  template<typename _Tp>
1489  struct hash<optional<_Tp>>
1490  : private __poison_hash<remove_const_t<_Tp>>,
1491  public __optional_hash_call_base<_Tp>
1492  {
1493  using result_type [[__deprecated__]] = size_t;
1494  using argument_type [[__deprecated__]] = optional<_Tp>;
1495  };
1496 
1497  template<typename _Tp>
1498  struct __is_fast_hash<hash<optional<_Tp>>> : __is_fast_hash<hash<_Tp>>
1499  { };
1500 
1501  /// @}
1502 
1503 #if __cpp_deduction_guides >= 201606
1504  template <typename _Tp> optional(_Tp) -> optional<_Tp>;
1505 #endif
1506 
1507 _GLIBCXX_END_NAMESPACE_VERSION
1508 } // namespace std
1509 
1510 #endif // C++17
1511 
1512 #endif // _GLIBCXX_OPTIONAL
typename remove_cv< _Tp >::type remove_cv_t
Alias template for remove_cv.
Definition: type_traits:1609
typename remove_cvref< _Tp >::type remove_cvref_t
Definition: type_traits:3357
typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.
Definition: type_traits:2614
typename invoke_result< _Fn, _Args... >::type invoke_result_t
std::invoke_result_t
Definition: type_traits:3034
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition: move.h:49
constexpr __invoke_result< _Callable, _Args... >::type __invoke(_Callable &&__fn, _Args &&... __args) noexcept(__is_nothrow_invocable< _Callable, _Args... >::value)
Invoke a callable object.
Definition: invoke.h:90
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:104
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition: move.h:77
constexpr nullopt_t nullopt
Tag to disengage optional objects.
Definition: optional:87
void swap(any &__x, any &__y) noexcept
Exchange the states of two any objects.
Definition: any:429
ISO C++ entities toplevel namespace is std.
constexpr void _Construct(_Tp *__p, _Args &&... __args)
initializer_list
Class template for optional values.
Definition: optional:716
Tag type to disengage optional objects.
Definition: optional:74
Exception class thrown when a disengaged optional object is dereferenced.
Definition: optional:97
const char * what() const noexcept override
Definition: optional:102
Class template that provides copy/move constructors of optional.
Definition: optional:510
is_same
Definition: type_traits:1435
is_constructible
Definition: type_traits:979
is_nothrow_constructible
Definition: type_traits:1049
is_assignable
Definition: type_traits:1113
is_nothrow_assignable
Definition: type_traits:1168
is_convertible
Definition: type_traits:1484
Base class for all library exceptions.
Definition: exception.h:62