Tweak static assertions in std::optional
[official-gcc.git] / libstdc++-v3 / include / std / optional
blob5aa926f0cfd84727c59e08d6dcf36f232f05911b
1 // <optional> -*- C++ -*-
3 // Copyright (C) 2013-2017 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file include/optional
26  *  This is a Standard C++ Library header.
27  */
29 #ifndef _GLIBCXX_OPTIONAL
30 #define _GLIBCXX_OPTIONAL 1
32 #if __cplusplus <= 201402L
33 # include <bits/c++17_warning.h>
34 #else
36 #include <utility>
37 #include <type_traits>
38 #include <stdexcept>
39 #include <new>
40 #include <initializer_list>
41 #include <bits/functexcept.h>
42 #include <bits/functional_hash.h>
43 #include <bits/enable_special_members.h>
45 namespace std _GLIBCXX_VISIBILITY(default)
47 _GLIBCXX_BEGIN_NAMESPACE_VERSION
49   /**
50    *  @addtogroup utilities
51    *  @{
52    */
54   template<typename _Tp>
55     class optional;
57   /// Tag type to disengage optional objects.
58   struct nullopt_t
59   {
60     // Do not user-declare default constructor at all for
61     // optional_value = {} syntax to work.
62     // nullopt_t() = delete;
64     // Used for constructing nullopt.
65     enum class _Construct { _Token };
67     // Must be constexpr for nullopt_t to be literal.
68     explicit constexpr nullopt_t(_Construct) { }
69   };
71   /// Tag to disengage optional objects.
72   inline constexpr nullopt_t nullopt { nullopt_t::_Construct::_Token };
74   /**
75    *  @brief Exception class thrown when a disengaged optional object is
76    *  dereferenced.
77    *  @ingroup exceptions
78    */
79   class bad_optional_access : public exception
80   {
81   public:
82     bad_optional_access() { }
83     virtual const char* what() const noexcept override
84     {return "bad optional access";}
86     virtual ~bad_optional_access() noexcept = default;
87   };
89   void
90   __throw_bad_optional_access()
91   __attribute__((__noreturn__));
93   // XXX Does not belong here.
94   inline void
95   __throw_bad_optional_access()
96   { _GLIBCXX_THROW_OR_ABORT(bad_optional_access()); }
99   // Payload for constexpr optionals.
100   template <typename _Tp,
101             bool /*_TrivialCopyMove*/ =
102               is_trivially_copy_constructible<_Tp>::value
103               && is_trivially_move_constructible<_Tp>::value,
104             bool /*_ShouldProvideDestructor*/ =
105               is_trivially_destructible<_Tp>::value>
106     struct _Optional_payload
107     {
108       constexpr _Optional_payload()
109         : _M_empty() {}
111       template<typename... _Args>
112       constexpr _Optional_payload(in_place_t, _Args&&... __args)
113         : _M_payload(std::forward<_Args>(__args)...),
114           _M_engaged(true)
115       {}
117       template<typename _Up, typename... _Args>
118       constexpr _Optional_payload(std::initializer_list<_Up> __il,
119                                   _Args&&... __args)
120         : _M_payload(__il, std::forward<_Args>(__args)...),
121           _M_engaged(true) {}
123       template <class _Up> struct __ctor_tag {};
125       constexpr _Optional_payload(__ctor_tag<bool>,
126                                   const _Tp& __other)
127         : _M_payload(__other),
128           _M_engaged(true)
129       {}
131       constexpr _Optional_payload(__ctor_tag<void>)
132         : _M_empty()
133       {}
135       constexpr _Optional_payload(__ctor_tag<bool>, _Tp&& __other)
136         : _M_payload(std::move(__other)),
137           _M_engaged(true)
138       {}
140       constexpr _Optional_payload(bool __engaged,
141                                   const _Optional_payload& __other)
142         : _Optional_payload(__engaged ?
143                             _Optional_payload(__ctor_tag<bool>{},
144                                               __other._M_payload) :
145                             _Optional_payload(__ctor_tag<void>{}))
146       {}
148       constexpr _Optional_payload(bool __engaged,
149                                   _Optional_payload&& __other)
150         : _Optional_payload(__engaged
151                             ? _Optional_payload(__ctor_tag<bool>{},
152                                                 std::move(__other._M_payload))
153                             : _Optional_payload(__ctor_tag<void>{}))
154       {}
156       using _Stored_type = remove_const_t<_Tp>;
157       struct _Empty_byte { };
158       union {
159           _Empty_byte _M_empty;
160           _Stored_type _M_payload;
161       };
162       bool _M_engaged = false;
163     };
165   // Payload for non-constexpr optionals with non-trivial destructor.
166   template <typename _Tp>
167     struct _Optional_payload<_Tp, false, false>
168     {
169       constexpr _Optional_payload()
170         : _M_empty() {}
172       template <typename... _Args>
173       constexpr _Optional_payload(in_place_t, _Args&&... __args)
174         : _M_payload(std::forward<_Args>(__args)...),
175           _M_engaged(true) {}
177       template<typename _Up, typename... _Args>
178       constexpr _Optional_payload(std::initializer_list<_Up> __il,
179                                   _Args&&... __args)
180         : _M_payload(__il, std::forward<_Args>(__args)...),
181           _M_engaged(true) {}
182       constexpr
183       _Optional_payload(bool __engaged, const _Optional_payload& __other)
184         : _Optional_payload(__other)
185       {}
187       constexpr
188       _Optional_payload(bool __engaged, _Optional_payload&& __other)
189         : _Optional_payload(std::move(__other))
190       {}
192       constexpr _Optional_payload(const _Optional_payload& __other)
193       {
194         if (__other._M_engaged)
195           this->_M_construct(__other._M_payload);
196       }
198       constexpr _Optional_payload(_Optional_payload&& __other)
199       {
200         if (__other._M_engaged)
201           this->_M_construct(std::move(__other._M_payload));
202       }
204       using _Stored_type = remove_const_t<_Tp>;
205       struct _Empty_byte { };
206       union {
207           _Empty_byte _M_empty;
208           _Stored_type _M_payload;
209       };
210       bool _M_engaged = false;
212       ~_Optional_payload()
213       {
214         if (_M_engaged)
215           _M_payload.~_Stored_type();
216       }
218       template<typename... _Args>
219         void
220         _M_construct(_Args&&... __args)
221         noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
222         {
223           ::new ((void *) std::__addressof(this->_M_payload))
224             _Stored_type(std::forward<_Args>(__args)...);
225           this->_M_engaged = true;
226         }
227     };
229   // Payload for non-constexpr optionals with trivial destructor.
230   template <typename _Tp>
231     struct _Optional_payload<_Tp, false, true>
232     {
233       constexpr _Optional_payload()
234         : _M_empty() {}
236       template <typename... _Args>
237       constexpr _Optional_payload(in_place_t, _Args&&... __args)
238         : _M_payload(std::forward<_Args>(__args)...),
239           _M_engaged(true) {}
241       template<typename _Up, typename... _Args>
242       constexpr _Optional_payload(std::initializer_list<_Up> __il,
243                                   _Args&&... __args)
244         : _M_payload(__il, std::forward<_Args>(__args)...),
245           _M_engaged(true) {}
246       constexpr
247       _Optional_payload(bool __engaged, const _Optional_payload& __other)
248         : _Optional_payload(__other)
249       {}
251       constexpr
252       _Optional_payload(bool __engaged, _Optional_payload&& __other)
253         : _Optional_payload(std::move(__other))
254       {}
256       constexpr _Optional_payload(const _Optional_payload& __other)
257       {
258         if (__other._M_engaged)
259           this->_M_construct(__other._M_payload);
260       }
262       constexpr _Optional_payload(_Optional_payload&& __other)
263       {
264         if (__other._M_engaged)
265           this->_M_construct(std::move(__other._M_payload));
266       }
268       using _Stored_type = remove_const_t<_Tp>;
269       struct _Empty_byte { };
270       union {
271           _Empty_byte _M_empty;
272           _Stored_type _M_payload;
273       };
274       bool _M_engaged = false;
276       template<typename... _Args>
277         void
278         _M_construct(_Args&&... __args)
279         noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
280         {
281           ::new ((void *) std::__addressof(this->_M_payload))
282             _Stored_type(std::forward<_Args>(__args)...);
283           this->_M_engaged = true;
284         }
285     };
287   /**
288     * @brief Class template that holds the necessary state for @ref optional
289     * and that has the responsibility for construction and the special members.
290     *
291     * Such a separate base class template is necessary in order to
292     * conditionally enable the special members (e.g. copy/move constructors).
293     * Note that this means that @ref _Optional_base implements the
294     * functionality for copy and move assignment, but not for converting
295     * assignment.
296     *
297     * @see optional, _Enable_special_members
298     */
299   template<typename _Tp>
300     class _Optional_base
301     {
302     private:
303       // Remove const to avoid prohibition of reusing object storage for
304       // const-qualified types in [3.8/9]. This is strictly internal
305       // and even optional itself is oblivious to it.
306       using _Stored_type = remove_const_t<_Tp>;
308     public:
310       // Constructors for disengaged optionals.
311       constexpr _Optional_base() noexcept
312       { }
314       constexpr _Optional_base(nullopt_t) noexcept
315       { }
317       // Constructors for engaged optionals.
318       template<typename... _Args,
319                enable_if_t<is_constructible_v<_Tp, _Args&&...>, bool> = false>
320         constexpr explicit _Optional_base(in_place_t, _Args&&... __args)
321         : _M_payload(in_place,
322                      std::forward<_Args>(__args)...) { }
324       template<typename _Up, typename... _Args,
325                enable_if_t<is_constructible_v<_Tp,
326                                               initializer_list<_Up>&,
327                                               _Args&&...>, bool> = false>
328         constexpr explicit _Optional_base(in_place_t,
329                                           initializer_list<_Up> __il,
330                                           _Args&&... __args)
331         : _M_payload(in_place,
332                      __il, std::forward<_Args>(__args)...)
333         { }
335       // Copy and move constructors.
336       constexpr _Optional_base(const _Optional_base& __other)
337         : _M_payload(__other._M_payload._M_engaged,
338                      __other._M_payload)
339       { }
341       constexpr _Optional_base(_Optional_base&& __other)
342       noexcept(is_nothrow_move_constructible<_Tp>())
343         : _M_payload(__other._M_payload._M_engaged,
344                      std::move(__other._M_payload))
345       { }
347       // Assignment operators.
348       _Optional_base&
349       operator=(const _Optional_base& __other)
350       {
351         if (this->_M_payload._M_engaged && __other._M_payload._M_engaged)
352           this->_M_get() = __other._M_get();
353         else
354           {
355             if (__other._M_payload._M_engaged)
356               this->_M_construct(__other._M_get());
357             else
358               this->_M_reset();
359           }
361         return *this;
362       }
364       _Optional_base&
365       operator=(_Optional_base&& __other)
366       noexcept(__and_<is_nothrow_move_constructible<_Tp>,
367                       is_nothrow_move_assignable<_Tp>>())
368       {
369         if (this->_M_payload._M_engaged && __other._M_payload._M_engaged)
370           this->_M_get() = std::move(__other._M_get());
371         else
372           {
373             if (__other._M_payload._M_engaged)
374               this->_M_construct(std::move(__other._M_get()));
375             else
376               this->_M_reset();
377           }
378         return *this;
379       }
380       // The following functionality is also needed by optional, hence the
381       // protected accessibility.
382     protected:
383       constexpr bool _M_is_engaged() const noexcept
384       { return this->_M_payload._M_engaged; }
386       // The _M_get operations have _M_engaged as a precondition.
387       constexpr _Tp&
388       _M_get() noexcept
389       { return this->_M_payload._M_payload; }
391       constexpr const _Tp&
392       _M_get() const noexcept
393       { return this->_M_payload._M_payload; }
395       // The _M_construct operation has !_M_engaged as a precondition
396       // while _M_destruct has _M_engaged as a precondition.
397       template<typename... _Args>
398         void
399         _M_construct(_Args&&... __args)
400         noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
401         {
402           ::new (std::__addressof(this->_M_payload._M_payload))
403             _Stored_type(std::forward<_Args>(__args)...);
404           this->_M_payload._M_engaged = true;
405         }
407       void
408       _M_destruct()
409       {
410         this->_M_payload._M_engaged = false;
411         this->_M_payload._M_payload.~_Stored_type();
412       }
414       // _M_reset is a 'safe' operation with no precondition.
415       void
416       _M_reset()
417       {
418         if (this->_M_payload._M_engaged)
419           this->_M_destruct();
420       }
422     private:
423       _Optional_payload<_Tp> _M_payload;
424     };
426   template<typename _Tp>
427   class optional;
429   template<typename _Tp, typename _Up>
430     using __converts_from_optional =
431       __or_<is_constructible<_Tp, const optional<_Up>&>,
432             is_constructible<_Tp, optional<_Up>&>,
433             is_constructible<_Tp, const optional<_Up>&&>,
434             is_constructible<_Tp, optional<_Up>&&>,
435             is_convertible<const optional<_Up>&, _Tp>,
436             is_convertible<optional<_Up>&, _Tp>,
437             is_convertible<const optional<_Up>&&, _Tp>,
438             is_convertible<optional<_Up>&&, _Tp>>;
440   template<typename _Tp, typename _Up>
441     using __assigns_from_optional =
442       __or_<is_assignable<_Tp&, const optional<_Up>&>,
443             is_assignable<_Tp&, optional<_Up>&>,
444             is_assignable<_Tp&, const optional<_Up>&&>,
445             is_assignable<_Tp&, optional<_Up>&&>>;
447   /**
448     * @brief Class template for optional values.
449     */
450   template<typename _Tp>
451     class optional
452     : private _Optional_base<_Tp>,
453       private _Enable_copy_move<
454         // Copy constructor.
455         is_copy_constructible<_Tp>::value,
456         // Copy assignment.
457         __and_<is_copy_constructible<_Tp>, is_copy_assignable<_Tp>>::value,
458         // Move constructor.
459         is_move_constructible<_Tp>::value,
460         // Move assignment.
461         __and_<is_move_constructible<_Tp>, is_move_assignable<_Tp>>::value,
462         // Unique tag type.
463         optional<_Tp>>
464     {
465       static_assert(!is_same_v<remove_cv_t<_Tp>, nullopt_t>);
466       static_assert(!is_same_v<remove_cv_t<_Tp>, in_place_t>);
467       static_assert(!is_reference_v<_Tp>);
469     private:
470       using _Base = _Optional_base<_Tp>;
472     public:
473       using value_type = _Tp;
475       constexpr optional() = default;
477       constexpr optional(nullopt_t) noexcept
478         : _Base(nullopt) { }
480       // Converting constructors for engaged optionals.
481       template <typename _Up = _Tp,
482                 enable_if_t<__and_<
483                               __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
484                               __not_<is_same<in_place_t, decay_t<_Up>>>,
485                               is_constructible<_Tp, _Up&&>,
486                               is_convertible<_Up&&, _Tp>
487                               >::value, bool> = true>
488       constexpr optional(_Up&& __t)
489         : _Base(std::in_place, std::forward<_Up>(__t)) { }
491       template <typename _Up = _Tp,
492                 enable_if_t<__and_<
493                               __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
494                               __not_<is_same<in_place_t, decay_t<_Up>>>,
495                               is_constructible<_Tp, _Up&&>,
496                               __not_<is_convertible<_Up&&, _Tp>>
497                               >::value, bool> = false>
498       explicit constexpr optional(_Up&& __t)
499         : _Base(std::in_place, std::forward<_Up>(__t)) { }
501       template <typename _Up,
502                 enable_if_t<__and_<
503                             __not_<is_same<_Tp, _Up>>,
504                             is_constructible<_Tp, const _Up&>,
505                             is_convertible<const _Up&, _Tp>,
506                             __not_<__converts_from_optional<_Tp, _Up>>
507                             >::value, bool> = true>
508       constexpr optional(const optional<_Up>& __t)
509       {
510         if (__t)
511           emplace(*__t);
512       }
514       template <typename _Up,
515                  enable_if_t<__and_<
516                                __not_<is_same<_Tp, _Up>>,
517                                is_constructible<_Tp, const _Up&>,
518                                __not_<is_convertible<const _Up&, _Tp>>,
519                                __not_<__converts_from_optional<_Tp, _Up>>
520                                >::value, bool> = false>
521       explicit constexpr optional(const optional<_Up>& __t)
522       {
523         if (__t)
524           emplace(*__t);
525       }
527       template <typename _Up,
528                 enable_if_t<__and_<
529                               __not_<is_same<_Tp, _Up>>,
530                               is_constructible<_Tp, _Up&&>,
531                               is_convertible<_Up&&, _Tp>,
532                               __not_<__converts_from_optional<_Tp, _Up>>
533                               >::value, bool> = true>
534       constexpr optional(optional<_Up>&& __t)
535       {
536         if (__t)
537           emplace(std::move(*__t));
538       }
540       template <typename _Up,
541                 enable_if_t<__and_<
542                             __not_<is_same<_Tp, _Up>>,
543                             is_constructible<_Tp, _Up&&>,
544                             __not_<is_convertible<_Up&&, _Tp>>,
545                             __not_<__converts_from_optional<_Tp, _Up>>
546                             >::value, bool> = false>
547       explicit constexpr optional(optional<_Up>&& __t)
548       {
549         if (__t)
550           emplace(std::move(*__t));
551       }
553       template<typename... _Args,
554                enable_if_t<is_constructible_v<_Tp, _Args&&...>, bool> = false>
555       explicit constexpr optional(in_place_t, _Args&&... __args)
556         : _Base(std::in_place, std::forward<_Args>(__args)...) { }
558       template<typename _Up, typename... _Args,
559                enable_if_t<is_constructible_v<_Tp,
560                                               initializer_list<_Up>&,
561                                               _Args&&...>, bool> = false>
562       explicit constexpr optional(in_place_t,
563                                   initializer_list<_Up> __il,
564                                   _Args&&... __args)
565         : _Base(std::in_place, __il, std::forward<_Args>(__args)...) { }
567       // Assignment operators.
568       optional&
569       operator=(nullopt_t) noexcept
570       {
571         this->_M_reset();
572         return *this;
573       }
575       template<typename _Up = _Tp>
576         enable_if_t<__and_<
577                       __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
578                       is_constructible<_Tp, _Up>,
579                       __not_<__and_<is_scalar<_Tp>,
580                                     is_same<_Tp, decay_t<_Up>>>>,
581                       is_assignable<_Tp&, _Up>>::value,
582                     optional&>
583         operator=(_Up&& __u)
584         {
585           if (this->_M_is_engaged())
586             this->_M_get() = std::forward<_Up>(__u);
587           else
588             this->_M_construct(std::forward<_Up>(__u));
590           return *this;
591         }
593       template<typename _Up>
594         enable_if_t<__and_<
595                       __not_<is_same<_Tp, _Up>>,
596                       is_constructible<_Tp, const _Up&>,
597                       is_assignable<_Tp&, _Up>,
598                       __not_<__converts_from_optional<_Tp, _Up>>,
599                       __not_<__assigns_from_optional<_Tp, _Up>>
600                       >::value,
601                     optional&>
602         operator=(const optional<_Up>& __u)
603         {
604           if (__u)
605             {
606               if (this->_M_is_engaged())
607                 this->_M_get() = *__u;
608               else
609                 this->_M_construct(*__u);
610             }
611           else
612             {
613               this->_M_reset();
614             }
615           return *this;
616         }
618       template<typename _Up>
619         enable_if_t<__and_<
620                       __not_<is_same<_Tp, _Up>>,
621                       is_constructible<_Tp, _Up>,
622                       is_assignable<_Tp&, _Up>,
623                       __not_<__converts_from_optional<_Tp, _Up>>,
624                       __not_<__assigns_from_optional<_Tp, _Up>>
625                       >::value,
626                     optional&>
627         operator=(optional<_Up>&& __u)
628         {
629           if (__u)
630             {
631               if (this->_M_is_engaged())
632                 this->_M_get() = std::move(*__u);
633               else
634                 this->_M_construct(std::move(*__u));
635             }
636           else
637             {
638               this->_M_reset();
639             }
641           return *this;
642         }
644       template<typename... _Args>
645         enable_if_t<is_constructible<_Tp, _Args&&...>::value, _Tp&>
646         emplace(_Args&&... __args)
647         {
648           this->_M_reset();
649           this->_M_construct(std::forward<_Args>(__args)...);
650           return this->_M_get();
651         }
653       template<typename _Up, typename... _Args>
654         enable_if_t<is_constructible<_Tp, initializer_list<_Up>&,
655                                      _Args&&...>::value, _Tp&>
656         emplace(initializer_list<_Up> __il, _Args&&... __args)
657         {
658           this->_M_reset();
659           this->_M_construct(__il, std::forward<_Args>(__args)...);
660           return this->_M_get();
661         }
663       // Destructor is implicit, implemented in _Optional_base.
665       // Swap.
666       void
667       swap(optional& __other)
668       noexcept(is_nothrow_move_constructible<_Tp>()
669                && is_nothrow_swappable_v<_Tp>)
670       {
671         using std::swap;
673         if (this->_M_is_engaged() && __other._M_is_engaged())
674           swap(this->_M_get(), __other._M_get());
675         else if (this->_M_is_engaged())
676           {
677             __other._M_construct(std::move(this->_M_get()));
678             this->_M_destruct();
679           }
680         else if (__other._M_is_engaged())
681           {
682             this->_M_construct(std::move(__other._M_get()));
683             __other._M_destruct();
684           }
685       }
687       // Observers.
688       constexpr const _Tp*
689       operator->() const
690       { return std::__addressof(this->_M_get()); }
692       _Tp*
693       operator->()
694       { return std::__addressof(this->_M_get()); }
696       constexpr const _Tp&
697       operator*() const&
698       { return this->_M_get(); }
700       constexpr _Tp&
701       operator*()&
702       { return this->_M_get(); }
704       constexpr _Tp&&
705       operator*()&&
706       { return std::move(this->_M_get()); }
708       constexpr const _Tp&&
709       operator*() const&&
710       { return std::move(this->_M_get()); }
712       constexpr explicit operator bool() const noexcept
713       { return this->_M_is_engaged(); }
715       constexpr bool has_value() const noexcept
716       { return this->_M_is_engaged(); }
718       constexpr const _Tp&
719       value() const&
720       {
721         return this->_M_is_engaged()
722           ?  this->_M_get()
723           : (__throw_bad_optional_access(),
724              this->_M_get());
725       }
727       constexpr _Tp&
728       value()&
729       {
730         return this->_M_is_engaged()
731           ?  this->_M_get()
732           : (__throw_bad_optional_access(),
733              this->_M_get());
734       }
736       constexpr _Tp&&
737       value()&&
738       {
739         return this->_M_is_engaged()
740           ?  std::move(this->_M_get())
741           : (__throw_bad_optional_access(),
742              std::move(this->_M_get()));
743       }
745       constexpr const _Tp&&
746       value() const&&
747       {
748         return this->_M_is_engaged()
749           ?  std::move(this->_M_get())
750           : (__throw_bad_optional_access(),
751              std::move(this->_M_get()));
752       }
754       template<typename _Up>
755         constexpr _Tp
756         value_or(_Up&& __u) const&
757         {
758           static_assert(is_copy_constructible_v<_Tp>);
759           static_assert(is_convertible_v<_Up&&, _Tp>);
761           return this->_M_is_engaged()
762             ? this->_M_get()
763             : static_cast<_Tp>(std::forward<_Up>(__u));
764         }
766       template<typename _Up>
767         _Tp
768         value_or(_Up&& __u) &&
769         {
770           static_assert(is_move_constructible_v<_Tp>);
771           static_assert(is_convertible_v<_Up&&, _Tp>);
773           return this->_M_is_engaged()
774             ? std::move(this->_M_get())
775             : static_cast<_Tp>(std::forward<_Up>(__u));
776         }
777       void reset() noexcept { this->_M_reset(); }
778     };
780   template<typename _Tp>
781     using __optional_relop_t =
782     enable_if_t<is_convertible<_Tp, bool>::value, bool>;
784   // Comparisons between optional values.
785   template<typename _Tp, typename _Up>
786     constexpr auto
787     operator==(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
788     -> __optional_relop_t<decltype(declval<_Tp>() == declval<_Up>())>
789     {
790       return static_cast<bool>(__lhs) == static_cast<bool>(__rhs)
791              && (!__lhs || *__lhs == *__rhs);
792     }
794   template<typename _Tp, typename _Up>
795     constexpr auto
796     operator!=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
797     -> __optional_relop_t<decltype(declval<_Tp>() != declval<_Up>())>
798     {
799       return static_cast<bool>(__lhs) != static_cast<bool>(__rhs)
800         || (static_cast<bool>(__lhs) && *__lhs != *__rhs);
801     }
803   template<typename _Tp, typename _Up>
804     constexpr auto
805     operator<(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
806     -> __optional_relop_t<decltype(declval<_Tp>() < declval<_Up>())>
807     {
808       return static_cast<bool>(__rhs) && (!__lhs || *__lhs < *__rhs);
809     }
811   template<typename _Tp, typename _Up>
812     constexpr auto
813     operator>(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
814     -> __optional_relop_t<decltype(declval<_Tp>() > declval<_Up>())>
815     {
816       return static_cast<bool>(__lhs) && (!__rhs || *__lhs > *__rhs);
817     }
819   template<typename _Tp, typename _Up>
820     constexpr auto
821     operator<=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
822     -> __optional_relop_t<decltype(declval<_Tp>() <= declval<_Up>())>
823     {
824       return !__lhs || (static_cast<bool>(__rhs) && *__lhs <= *__rhs);
825     }
827   template<typename _Tp, typename _Up>
828     constexpr auto
829     operator>=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
830     -> __optional_relop_t<decltype(declval<_Tp>() >= declval<_Up>())>
831     {
832       return !__rhs || (static_cast<bool>(__lhs) && *__lhs >= *__rhs);
833     }
835   // Comparisons with nullopt.
836   template<typename _Tp>
837     constexpr bool
838     operator==(const optional<_Tp>& __lhs, nullopt_t) noexcept
839     { return !__lhs; }
841   template<typename _Tp>
842     constexpr bool
843     operator==(nullopt_t, const optional<_Tp>& __rhs) noexcept
844     { return !__rhs; }
846   template<typename _Tp>
847     constexpr bool
848     operator!=(const optional<_Tp>& __lhs, nullopt_t) noexcept
849     { return static_cast<bool>(__lhs); }
851   template<typename _Tp>
852     constexpr bool
853     operator!=(nullopt_t, const optional<_Tp>& __rhs) noexcept
854     { return static_cast<bool>(__rhs); }
856   template<typename _Tp>
857     constexpr bool
858     operator<(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
859     { return false; }
861   template<typename _Tp>
862     constexpr bool
863     operator<(nullopt_t, const optional<_Tp>& __rhs) noexcept
864     { return static_cast<bool>(__rhs); }
866   template<typename _Tp>
867     constexpr bool
868     operator>(const optional<_Tp>& __lhs, nullopt_t) noexcept
869     { return static_cast<bool>(__lhs); }
871   template<typename _Tp>
872     constexpr bool
873     operator>(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
874     { return false; }
876   template<typename _Tp>
877     constexpr bool
878     operator<=(const optional<_Tp>& __lhs, nullopt_t) noexcept
879     { return !__lhs; }
881   template<typename _Tp>
882     constexpr bool
883     operator<=(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
884     { return true; }
886   template<typename _Tp>
887     constexpr bool
888     operator>=(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
889     { return true; }
891   template<typename _Tp>
892     constexpr bool
893     operator>=(nullopt_t, const optional<_Tp>& __rhs) noexcept
894     { return !__rhs; }
896   // Comparisons with value type.
897   template<typename _Tp, typename _Up>
898     constexpr auto
899     operator==(const optional<_Tp>& __lhs, const _Up& __rhs)
900     -> __optional_relop_t<decltype(declval<_Tp>() == declval<_Up>())>
901     { return __lhs && *__lhs == __rhs; }
903   template<typename _Tp, typename _Up>
904     constexpr auto
905     operator==(const _Up& __lhs, const optional<_Tp>& __rhs)
906     -> __optional_relop_t<decltype(declval<_Up>() == declval<_Tp>())>
907     { return __rhs && __lhs == *__rhs; }
909   template<typename _Tp, typename _Up>
910     constexpr auto
911     operator!=(const optional<_Tp>& __lhs, const _Up& __rhs)
912     -> __optional_relop_t<decltype(declval<_Tp>() != declval<_Up>())>
913     { return !__lhs || *__lhs != __rhs; }
915   template<typename _Tp, typename _Up>
916     constexpr auto
917     operator!=(const _Up& __lhs, const optional<_Tp>& __rhs)
918     -> __optional_relop_t<decltype(declval<_Up>() != declval<_Tp>())>
919     { return !__rhs || __lhs != *__rhs; }
921   template<typename _Tp, typename _Up>
922     constexpr auto
923     operator<(const optional<_Tp>& __lhs, const _Up& __rhs)
924     -> __optional_relop_t<decltype(declval<_Tp>() < declval<_Up>())>
925     { return !__lhs || *__lhs < __rhs; }
927   template<typename _Tp, typename _Up>
928     constexpr auto
929     operator<(const _Up& __lhs, const optional<_Tp>& __rhs)
930     -> __optional_relop_t<decltype(declval<_Up>() < declval<_Tp>())>
931     { return __rhs && __lhs < *__rhs; }
933   template<typename _Tp, typename _Up>
934     constexpr auto
935     operator>(const optional<_Tp>& __lhs, const _Up& __rhs)
936     -> __optional_relop_t<decltype(declval<_Tp>() > declval<_Up>())>
937     { return __lhs && *__lhs > __rhs; }
939   template<typename _Tp, typename _Up>
940     constexpr auto
941     operator>(const _Up& __lhs, const optional<_Tp>& __rhs)
942     -> __optional_relop_t<decltype(declval<_Up>() > declval<_Tp>())>
943     { return !__rhs || __lhs > *__rhs; }
945   template<typename _Tp, typename _Up>
946     constexpr auto
947     operator<=(const optional<_Tp>& __lhs, const _Up& __rhs)
948     -> __optional_relop_t<decltype(declval<_Tp>() <= declval<_Up>())>
949     { return !__lhs || *__lhs <= __rhs; }
951   template<typename _Tp, typename _Up>
952     constexpr auto
953     operator<=(const _Up& __lhs, const optional<_Tp>& __rhs)
954     -> __optional_relop_t<decltype(declval<_Up>() <= declval<_Tp>())>
955     { return __rhs && __lhs <= *__rhs; }
957   template<typename _Tp, typename _Up>
958     constexpr auto
959     operator>=(const optional<_Tp>& __lhs, const _Up& __rhs)
960     -> __optional_relop_t<decltype(declval<_Tp>() >= declval<_Up>())>
961     { return __lhs && *__lhs >= __rhs; }
963   template<typename _Tp, typename _Up>
964     constexpr auto
965     operator>=(const _Up& __lhs, const optional<_Tp>& __rhs)
966     -> __optional_relop_t<decltype(declval<_Up>() >= declval<_Tp>())>
967     { return !__rhs || __lhs >= *__rhs; }
969   // Swap and creation functions.
971   // _GLIBCXX_RESOLVE_LIB_DEFECTS
972   // 2748. swappable traits for optionals
973   template<typename _Tp>
974     inline enable_if_t<is_move_constructible_v<_Tp> && is_swappable_v<_Tp>>
975     swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs)
976     noexcept(noexcept(__lhs.swap(__rhs)))
977     { __lhs.swap(__rhs); }
979   template<typename _Tp>
980     enable_if_t<!(is_move_constructible_v<_Tp> && is_swappable_v<_Tp>)>
981     swap(optional<_Tp>&, optional<_Tp>&) = delete;
983   template<typename _Tp>
984     constexpr optional<decay_t<_Tp>>
985     make_optional(_Tp&& __t)
986     { return optional<decay_t<_Tp>> { std::forward<_Tp>(__t) }; }
988   template<typename _Tp, typename ..._Args>
989     constexpr optional<_Tp>
990     make_optional(_Args&&... __args)
991     { return optional<_Tp> { in_place, std::forward<_Args>(__args)... }; }
993   template<typename _Tp, typename _Up, typename ..._Args>
994     constexpr optional<_Tp>
995     make_optional(initializer_list<_Up> __il, _Args&&... __args)
996     { return optional<_Tp> { in_place, __il, std::forward<_Args>(__args)... }; }
998   // Hash.
1000   template<typename _Tp, bool
1001            = __poison_hash<remove_const_t<_Tp>>::__enable_hash_call>
1002     struct __optional_hash_call_base
1003     {
1004       size_t
1005       operator()(const optional<_Tp>& __t) const
1006       noexcept(noexcept(hash<_Tp> {}(*__t)))
1007       {
1008         // We pick an arbitrary hash for disengaged optionals which hopefully
1009         // usual values of _Tp won't typically hash to.
1010         constexpr size_t __magic_disengaged_hash = static_cast<size_t>(-3333);
1011         return __t ? hash<_Tp> {}(*__t) : __magic_disengaged_hash;
1012       }
1013     };
1015   template<typename _Tp>
1016     struct __optional_hash_call_base<_Tp, false> {};
1018   template<typename _Tp>
1019     struct hash<optional<_Tp>>
1020     : private __poison_hash<remove_const_t<_Tp>>,
1021       public __optional_hash_call_base<_Tp>
1022     {
1023       using result_type = size_t;
1024       using argument_type = optional<_Tp>;
1025     };
1027   /// @}
1029   template <typename _Tp> optional(_Tp) -> optional<_Tp>;
1031 _GLIBCXX_END_NAMESPACE_VERSION
1032 } // namespace std
1034 #endif // C++17
1036 #endif // _GLIBCXX_OPTIONAL