]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/std/optional
Implement std::optional.
[thirdparty/gcc.git] / libstdc++-v3 / include / std / optional
1 // <optional> -*- C++ -*-
2
3 // Copyright (C) 2013-2016 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.
10
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.
15
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.
19
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/>.
24
25 /** @file include/optional
26 * This is a Standard C++ Library header.
27 */
28
29 #ifndef _GLIBCXX_OPTIONAL
30 #define _GLIBCXX_OPTIONAL 1
31
32 #if __cplusplus <= 201402L
33 # include <bits/c++17_warning.h>
34 #else
35
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>
44
45 namespace std _GLIBCXX_VISIBILITY(default)
46 {
47 _GLIBCXX_BEGIN_NAMESPACE_VERSION
48
49 /**
50 * @addtogroup utilities
51 * @{
52 */
53
54 // All subsequent [X.Y.n] references are against n3793.
55
56 // [X.Y.4]
57 template<typename _Tp>
58 class optional;
59
60 // [X.Y.5]
61 /// Tag type for in-place construction.
62 struct in_place_t { };
63
64 /// Tag for in-place construction.
65 constexpr in_place_t in_place { };
66
67 // [X.Y.6]
68 /// Tag type to disengage optional objects.
69 struct nullopt_t
70 {
71 // Do not user-declare default constructor at all for
72 // optional_value = {} syntax to work.
73 // nullopt_t() = delete;
74
75 // Used for constructing nullopt.
76 enum class _Construct { _Token };
77
78 // Must be constexpr for nullopt_t to be literal.
79 explicit constexpr nullopt_t(_Construct) { }
80 };
81
82 // [X.Y.6]
83 /// Tag to disengage optional objects.
84 constexpr nullopt_t nullopt { nullopt_t::_Construct::_Token };
85
86 // [X.Y.7]
87 /**
88 * @brief Exception class thrown when a disengaged optional object is
89 * dereferenced.
90 * @ingroup exceptions
91 */
92 class bad_optional_access : public logic_error
93 {
94 public:
95 bad_optional_access() : logic_error("bad optional access") { }
96
97 // XXX This constructor is non-standard. Should not be inline
98 explicit bad_optional_access(const char* __arg) : logic_error(__arg) { }
99
100 virtual ~bad_optional_access() noexcept = default;
101 };
102
103 void
104 __throw_bad_optional_access(const char*)
105 __attribute__((__noreturn__));
106
107 // XXX Does not belong here.
108 inline void
109 __throw_bad_optional_access(const char* __s)
110 { _GLIBCXX_THROW_OR_ABORT(bad_optional_access(__s)); }
111
112 template<typename _Tp, typename = void>
113 struct _Has_addressof_mem : std::false_type { };
114
115 template<typename _Tp>
116 struct _Has_addressof_mem<_Tp,
117 __void_t<decltype( std::declval<const _Tp&>().operator&() )>
118 >
119 : std::true_type { };
120
121 template<typename _Tp, typename = void>
122 struct _Has_addressof_free : std::false_type { };
123
124 template<typename _Tp>
125 struct _Has_addressof_free<_Tp,
126 __void_t<decltype( operator&(std::declval<const _Tp&>()) )>
127 >
128 : std::true_type { };
129
130 /**
131 * @brief Trait that detects the presence of an overloaded unary operator&.
132 *
133 * Practically speaking this detects the presence of such an operator when
134 * called on a const-qualified lvalue (i.e.
135 * declval<_Tp * const&>().operator&()).
136 */
137 template<typename _Tp>
138 struct _Has_addressof
139 : std::__or_<_Has_addressof_mem<_Tp>, _Has_addressof_free<_Tp>>::type
140 { };
141
142 /**
143 * @brief An overload that attempts to take the address of an lvalue as a
144 * constant expression. Falls back to __addressof in the presence of an
145 * overloaded addressof operator (unary operator&), in which case the call
146 * will not be a constant expression.
147 */
148 template<typename _Tp, enable_if_t<!_Has_addressof<_Tp>::value, int>...>
149 constexpr _Tp* __constexpr_addressof(_Tp& __t)
150 { return &__t; }
151
152 /**
153 * @brief Fallback overload that defers to __addressof.
154 */
155 template<typename _Tp, enable_if_t<_Has_addressof<_Tp>::value, int>...>
156 inline _Tp* __constexpr_addressof(_Tp& __t)
157 { return std::__addressof(__t); }
158
159 /**
160 * @brief Class template that holds the necessary state for @ref optional
161 * and that has the responsibility for construction and the special members.
162 *
163 * Such a separate base class template is necessary in order to
164 * conditionally enable the special members (e.g. copy/move constructors).
165 * Note that this means that @ref _Optional_base implements the
166 * functionality for copy and move assignment, but not for converting
167 * assignment.
168 *
169 * @see optional, _Enable_special_members
170 */
171 template<typename _Tp, bool _ShouldProvideDestructor =
172 !is_trivially_destructible<_Tp>::value>
173 class _Optional_base
174 {
175 private:
176 // Remove const to avoid prohibition of reusing object storage for
177 // const-qualified types in [3.8/9]. This is strictly internal
178 // and even optional itself is oblivious to it.
179 using _Stored_type = remove_const_t<_Tp>;
180
181 public:
182 // [X.Y.4.1] Constructors.
183
184 // Constructors for disengaged optionals.
185 constexpr _Optional_base() noexcept
186 : _M_empty{} { }
187
188 constexpr _Optional_base(nullopt_t) noexcept
189 : _Optional_base{} { }
190
191 // Constructors for engaged optionals.
192 constexpr _Optional_base(const _Tp& __t)
193 : _M_payload(__t), _M_engaged(true) { }
194
195 constexpr _Optional_base(_Tp&& __t)
196 : _M_payload(std::move(__t)), _M_engaged(true) { }
197
198 template<typename... _Args>
199 constexpr explicit _Optional_base(in_place_t, _Args&&... __args)
200 : _M_payload(std::forward<_Args>(__args)...), _M_engaged(true) { }
201
202 template<typename _Up, typename... _Args,
203 enable_if_t<is_constructible<_Tp,
204 initializer_list<_Up>&,
205 _Args&&...>::value,
206 int>...>
207 constexpr explicit _Optional_base(in_place_t,
208 initializer_list<_Up> __il,
209 _Args&&... __args)
210 : _M_payload(__il, std::forward<_Args>(__args)...),
211 _M_engaged(true) { }
212
213 // Copy and move constructors.
214 _Optional_base(const _Optional_base& __other)
215 {
216 if (__other._M_engaged)
217 this->_M_construct(__other._M_get());
218 }
219
220 _Optional_base(_Optional_base&& __other)
221 noexcept(is_nothrow_move_constructible<_Tp>())
222 {
223 if (__other._M_engaged)
224 this->_M_construct(std::move(__other._M_get()));
225 }
226
227 // [X.Y.4.3] (partly) Assignment.
228 _Optional_base&
229 operator=(const _Optional_base& __other)
230 {
231 if (this->_M_engaged && __other._M_engaged)
232 this->_M_get() = __other._M_get();
233 else
234 {
235 if (__other._M_engaged)
236 this->_M_construct(__other._M_get());
237 else
238 this->_M_reset();
239 }
240
241 return *this;
242 }
243
244 _Optional_base&
245 operator=(_Optional_base&& __other)
246 noexcept(__and_<is_nothrow_move_constructible<_Tp>,
247 is_nothrow_move_assignable<_Tp>>())
248 {
249 if (this->_M_engaged && __other._M_engaged)
250 this->_M_get() = std::move(__other._M_get());
251 else
252 {
253 if (__other._M_engaged)
254 this->_M_construct(std::move(__other._M_get()));
255 else
256 this->_M_reset();
257 }
258 return *this;
259 }
260
261 // [X.Y.4.2] Destructor.
262 ~_Optional_base()
263 {
264 if (this->_M_engaged)
265 this->_M_payload.~_Stored_type();
266 }
267
268 // The following functionality is also needed by optional, hence the
269 // protected accessibility.
270 protected:
271 constexpr bool _M_is_engaged() const noexcept
272 { return this->_M_engaged; }
273
274 // The _M_get operations have _M_engaged as a precondition.
275 constexpr _Tp&
276 _M_get() noexcept
277 { return _M_payload; }
278
279 constexpr const _Tp&
280 _M_get() const noexcept
281 { return _M_payload; }
282
283 // The _M_construct operation has !_M_engaged as a precondition
284 // while _M_destruct has _M_engaged as a precondition.
285 template<typename... _Args>
286 void
287 _M_construct(_Args&&... __args)
288 noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
289 {
290 ::new (std::__addressof(this->_M_payload))
291 _Stored_type(std::forward<_Args>(__args)...);
292 this->_M_engaged = true;
293 }
294
295 void
296 _M_destruct()
297 {
298 this->_M_engaged = false;
299 this->_M_payload.~_Stored_type();
300 }
301
302 // _M_reset is a 'safe' operation with no precondition.
303 void
304 _M_reset()
305 {
306 if (this->_M_engaged)
307 this->_M_destruct();
308 }
309
310 private:
311 struct _Empty_byte { };
312 union {
313 _Empty_byte _M_empty;
314 _Stored_type _M_payload;
315 };
316 bool _M_engaged = false;
317 };
318
319 /// Partial specialization that is exactly identical to the primary template
320 /// save for not providing a destructor, to fulfill triviality requirements.
321 template<typename _Tp>
322 class _Optional_base<_Tp, false>
323 {
324 private:
325 using _Stored_type = remove_const_t<_Tp>;
326
327 public:
328 constexpr _Optional_base() noexcept
329 : _M_empty{} { }
330
331 constexpr _Optional_base(nullopt_t) noexcept
332 : _Optional_base{} { }
333
334 constexpr _Optional_base(const _Tp& __t)
335 : _M_payload(__t), _M_engaged(true) { }
336
337 constexpr _Optional_base(_Tp&& __t)
338 : _M_payload(std::move(__t)), _M_engaged(true) { }
339
340 template<typename... _Args>
341 constexpr explicit _Optional_base(in_place_t, _Args&&... __args)
342 : _M_payload(std::forward<_Args>(__args)...), _M_engaged(true) { }
343
344 template<typename _Up, typename... _Args,
345 enable_if_t<is_constructible<_Tp,
346 initializer_list<_Up>&,
347 _Args&&...>::value,
348 int>...>
349 constexpr explicit _Optional_base(in_place_t,
350 initializer_list<_Up> __il,
351 _Args&&... __args)
352 : _M_payload(__il, std::forward<_Args>(__args)...),
353 _M_engaged(true) { }
354
355 _Optional_base(const _Optional_base& __other)
356 {
357 if (__other._M_engaged)
358 this->_M_construct(__other._M_get());
359 }
360
361 _Optional_base(_Optional_base&& __other)
362 noexcept(is_nothrow_move_constructible<_Tp>())
363 {
364 if (__other._M_engaged)
365 this->_M_construct(std::move(__other._M_get()));
366 }
367
368 _Optional_base&
369 operator=(const _Optional_base& __other)
370 {
371 if (this->_M_engaged && __other._M_engaged)
372 this->_M_get() = __other._M_get();
373 else
374 {
375 if (__other._M_engaged)
376 this->_M_construct(__other._M_get());
377 else
378 this->_M_reset();
379 }
380 return *this;
381 }
382
383 _Optional_base&
384 operator=(_Optional_base&& __other)
385 noexcept(__and_<is_nothrow_move_constructible<_Tp>,
386 is_nothrow_move_assignable<_Tp>>())
387 {
388 if (this->_M_engaged && __other._M_engaged)
389 this->_M_get() = std::move(__other._M_get());
390 else
391 {
392 if (__other._M_engaged)
393 this->_M_construct(std::move(__other._M_get()));
394 else
395 this->_M_reset();
396 }
397 return *this;
398 }
399
400 // Sole difference
401 // ~_Optional_base() noexcept = default;
402
403 protected:
404 constexpr bool _M_is_engaged() const noexcept
405 { return this->_M_engaged; }
406
407 _Tp&
408 _M_get() noexcept
409 { return _M_payload; }
410
411 constexpr const _Tp&
412 _M_get() const noexcept
413 { return _M_payload; }
414
415 template<typename... _Args>
416 void
417 _M_construct(_Args&&... __args)
418 noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
419 {
420 ::new (std::__addressof(this->_M_payload))
421 _Stored_type(std::forward<_Args>(__args)...);
422 this->_M_engaged = true;
423 }
424
425 void
426 _M_destruct()
427 {
428 this->_M_engaged = false;
429 this->_M_payload.~_Stored_type();
430 }
431
432 void
433 _M_reset()
434 {
435 if (this->_M_engaged)
436 this->_M_destruct();
437 }
438
439 private:
440 struct _Empty_byte { };
441 union
442 {
443 _Empty_byte _M_empty;
444 _Stored_type _M_payload;
445 };
446 bool _M_engaged = false;
447 };
448
449 template<typename _Tp>
450 class optional;
451
452 template<typename>
453 struct __is_optional_impl : false_type
454 { };
455
456 template<typename _Tp>
457 struct __is_optional_impl<optional<_Tp>> : true_type
458 { };
459
460 template<typename _Tp>
461 struct __is_optional
462 : public __is_optional_impl<std::remove_cv_t<std::remove_reference_t<_Tp>>>
463 { };
464
465
466 /**
467 * @brief Class template for optional values.
468 */
469 template<typename _Tp>
470 class optional
471 : private _Optional_base<_Tp>,
472 private _Enable_copy_move<
473 // Copy constructor.
474 is_copy_constructible<_Tp>::value,
475 // Copy assignment.
476 __and_<is_copy_constructible<_Tp>, is_copy_assignable<_Tp>>::value,
477 // Move constructor.
478 is_move_constructible<_Tp>::value,
479 // Move assignment.
480 __and_<is_move_constructible<_Tp>, is_move_assignable<_Tp>>::value,
481 // Unique tag type.
482 optional<_Tp>>
483 {
484 static_assert(__and_<__not_<is_same<remove_cv_t<_Tp>, nullopt_t>>,
485 __not_<is_same<remove_cv_t<_Tp>, in_place_t>>,
486 __not_<is_reference<_Tp>>>(),
487 "Invalid instantiation of optional<T>");
488
489 private:
490 using _Base = _Optional_base<_Tp>;
491
492 public:
493 using value_type = _Tp;
494
495 // _Optional_base has the responsibility for construction.
496 using _Base::_Base;
497
498 constexpr optional() = default;
499 // Converting constructors for engaged optionals.
500 template <typename _Up,
501 enable_if_t<__and_<
502 __not_<is_same<_Tp, _Up>>,
503 is_constructible<_Tp, _Up&&>,
504 is_convertible<_Up&&, _Tp>
505 >::value, bool> = true>
506 constexpr optional(_Up&& __t)
507 : _Base(_Tp(std::forward<_Up>(__t))) { }
508
509 template <typename _Up,
510 enable_if_t<__and_<
511 __not_<is_same<_Tp, _Up>>,
512 is_constructible<_Tp, _Up&&>,
513 __not_<is_convertible<_Up&&, _Tp>>
514 >::value, bool> = false>
515 explicit constexpr optional(_Up&& __t)
516 : _Base(_Tp(std::forward<_Up>(__t))) { }
517
518 template <typename _Up,
519 enable_if_t<__and_<
520 __not_<is_same<_Tp, _Up>>,
521 __not_<is_constructible<
522 _Tp, const optional<_Up>&>>,
523 __not_<is_convertible<
524 const optional<_Up>&, _Tp>>,
525 is_constructible<_Tp, const _Up&>,
526 is_convertible<const _Up&, _Tp>
527 >::value, bool> = true>
528 constexpr optional(const optional<_Up>& __t)
529 : _Base(__t ? optional<_Tp>(*__t) : optional<_Tp>()) { }
530
531 template <typename _Up,
532 enable_if_t<__and_<
533 __not_<is_same<_Tp, _Up>>,
534 __not_<is_constructible<
535 _Tp, const optional<_Up>&>>,
536 __not_<is_convertible<
537 const optional<_Up>&, _Tp>>,
538 is_constructible<_Tp, const _Up&>,
539 __not_<is_convertible<const _Up&, _Tp>>
540 >::value, bool> = false>
541 explicit constexpr optional(const optional<_Up>& __t)
542 : _Base(__t ? optional<_Tp>(*__t) : optional<_Tp>()) { }
543
544 template <typename _Up,
545 enable_if_t<__and_<
546 __not_<is_same<_Tp, _Up>>,
547 __not_<is_constructible<
548 _Tp, optional<_Up>&&>>,
549 __not_<is_convertible<
550 optional<_Up>&&, _Tp>>,
551 is_constructible<_Tp, _Up&&>,
552 is_convertible<_Up&&, _Tp>
553 >::value, bool> = true>
554 constexpr optional(optional<_Up>&& __t)
555 : _Base(__t ? optional<_Tp>(std::move(*__t)) : optional<_Tp>()) { }
556
557 template <typename _Up,
558 enable_if_t<__and_<
559 __not_<is_same<_Tp, _Up>>,
560 __not_<is_constructible<
561 _Tp, optional<_Up>&&>>,
562 __not_<is_convertible<
563 optional<_Up>&&, _Tp>>,
564 is_constructible<_Tp, _Up&&>,
565 __not_<is_convertible<_Up&&, _Tp>>
566 >::value, bool> = false>
567 explicit constexpr optional(optional<_Up>&& __t)
568 : _Base(__t ? optional<_Tp>(std::move(*__t)) : optional<_Tp>()) { }
569
570 // [X.Y.4.3] (partly) Assignment.
571 optional&
572 operator=(nullopt_t) noexcept
573 {
574 this->_M_reset();
575 return *this;
576 }
577
578 template<typename _Up,
579 enable_if_t<__and_<
580 __not_<is_same<_Up, nullopt_t>>,
581 __not_<__is_optional<_Up>>>::value,
582 bool> = true>
583 optional&
584 operator=(_Up&& __u)
585 {
586 static_assert(__and_<is_constructible<_Tp, _Up>,
587 is_assignable<_Tp&, _Up>>(),
588 "Cannot assign to value type from argument");
589
590 if (this->_M_is_engaged())
591 this->_M_get() = std::forward<_Up>(__u);
592 else
593 this->_M_construct(std::forward<_Up>(__u));
594
595 return *this;
596 }
597
598 template<typename _Up,
599 enable_if_t<__and_<
600 __not_<is_same<_Tp, _Up>>>::value,
601 bool> = true>
602 optional&
603 operator=(const optional<_Up>& __u)
604 {
605 static_assert(__and_<is_constructible<_Tp, _Up>,
606 is_assignable<_Tp&, _Up>>(),
607 "Cannot assign to value type from argument");
608
609 if (__u)
610 {
611 if (this->_M_is_engaged())
612 this->_M_get() = *__u;
613 else
614 this->_M_construct(*__u);
615 }
616 else
617 {
618 this->_M_reset();
619 }
620 return *this;
621 }
622
623 template<typename _Up,
624 enable_if_t<__and_<
625 __not_<is_same<_Tp, _Up>>>::value,
626 bool> = true>
627 optional&
628 operator=(optional<_Up>&& __u)
629 {
630 static_assert(__and_<is_constructible<_Tp, _Up>,
631 is_assignable<_Tp&, _Up>>(),
632 "Cannot assign to value type from argument");
633
634 if (__u)
635 {
636 if (this->_M_is_engaged())
637 this->_M_get() = std::move(*__u);
638 else
639 this->_M_construct(std::move(*__u));
640 }
641 else
642 {
643 this->_M_reset();
644 }
645
646 return *this;
647 }
648
649 template<typename... _Args>
650 void
651 emplace(_Args&&... __args)
652 {
653 static_assert(is_constructible<_Tp, _Args&&...>(),
654 "Cannot emplace value type from arguments");
655
656 this->_M_reset();
657 this->_M_construct(std::forward<_Args>(__args)...);
658 }
659
660 template<typename _Up, typename... _Args>
661 enable_if_t<is_constructible<_Tp, initializer_list<_Up>&,
662 _Args&&...>::value>
663 emplace(initializer_list<_Up> __il, _Args&&... __args)
664 {
665 this->_M_reset();
666 this->_M_construct(__il, std::forward<_Args>(__args)...);
667 }
668
669 // [X.Y.4.2] Destructor is implicit, implemented in _Optional_base.
670
671 // [X.Y.4.4] Swap.
672 void
673 swap(optional& __other)
674 noexcept(is_nothrow_move_constructible<_Tp>()
675 && noexcept(swap(declval<_Tp&>(), declval<_Tp&>())))
676 {
677 using std::swap;
678
679 if (this->_M_is_engaged() && __other._M_is_engaged())
680 swap(this->_M_get(), __other._M_get());
681 else if (this->_M_is_engaged())
682 {
683 __other._M_construct(std::move(this->_M_get()));
684 this->_M_destruct();
685 }
686 else if (__other._M_is_engaged())
687 {
688 this->_M_construct(std::move(__other._M_get()));
689 __other._M_destruct();
690 }
691 }
692
693 // [X.Y.4.5] Observers.
694 constexpr const _Tp*
695 operator->() const
696 { return __constexpr_addressof(this->_M_get()); }
697
698 _Tp*
699 operator->()
700 { return std::__addressof(this->_M_get()); }
701
702 constexpr const _Tp&
703 operator*() const&
704 { return this->_M_get(); }
705
706 constexpr _Tp&
707 operator*()&
708 { return this->_M_get(); }
709
710 constexpr _Tp&&
711 operator*()&&
712 { return std::move(this->_M_get()); }
713
714 constexpr const _Tp&&
715 operator*() const&&
716 { return std::move(this->_M_get()); }
717
718 constexpr explicit operator bool() const noexcept
719 { return this->_M_is_engaged(); }
720
721 constexpr const _Tp&
722 value() const&
723 {
724 return this->_M_is_engaged()
725 ? this->_M_get()
726 : (__throw_bad_optional_access("Attempt to access value of a "
727 "disengaged optional object"),
728 this->_M_get());
729 }
730
731 constexpr _Tp&
732 value()&
733 {
734 return this->_M_is_engaged()
735 ? this->_M_get()
736 : (__throw_bad_optional_access("Attempt to access value of a "
737 "disengaged optional object"),
738 this->_M_get());
739 }
740
741 constexpr _Tp&&
742 value()&&
743 {
744 return this->_M_is_engaged()
745 ? std::move(this->_M_get())
746 : (__throw_bad_optional_access("Attempt to access value of a "
747 "disengaged optional object"),
748 std::move(this->_M_get()));
749 }
750
751 constexpr const _Tp&&
752 value() const&&
753 {
754 return this->_M_is_engaged()
755 ? std::move(this->_M_get())
756 : (__throw_bad_optional_access("Attempt to access value of a "
757 "disengaged optional object"),
758 std::move(this->_M_get()));
759 }
760
761 template<typename _Up>
762 constexpr _Tp
763 value_or(_Up&& __u) const&
764 {
765 static_assert(__and_<is_copy_constructible<_Tp>,
766 is_convertible<_Up&&, _Tp>>(),
767 "Cannot return value");
768
769 return this->_M_is_engaged()
770 ? this->_M_get()
771 : static_cast<_Tp>(std::forward<_Up>(__u));
772 }
773
774 template<typename _Up>
775 _Tp
776 value_or(_Up&& __u) &&
777 {
778 static_assert(__and_<is_move_constructible<_Tp>,
779 is_convertible<_Up&&, _Tp>>(),
780 "Cannot return value" );
781
782 return this->_M_is_engaged()
783 ? std::move(this->_M_get())
784 : static_cast<_Tp>(std::forward<_Up>(__u));
785 }
786 };
787
788 // [X.Y.8] Comparisons between optional values.
789 template<typename _Tp>
790 constexpr bool
791 operator==(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
792 {
793 return static_cast<bool>(__lhs) == static_cast<bool>(__rhs)
794 && (!__lhs || *__lhs == *__rhs);
795 }
796
797 template<typename _Tp>
798 constexpr bool
799 operator!=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
800 { return !(__lhs == __rhs); }
801
802 template<typename _Tp>
803 constexpr bool
804 operator<(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
805 {
806 return static_cast<bool>(__rhs) && (!__lhs || *__lhs < *__rhs);
807 }
808
809 template<typename _Tp>
810 constexpr bool
811 operator>(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
812 { return __rhs < __lhs; }
813
814 template<typename _Tp>
815 constexpr bool
816 operator<=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
817 { return !(__rhs < __lhs); }
818
819 template<typename _Tp>
820 constexpr bool
821 operator>=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
822 { return !(__lhs < __rhs); }
823
824 // [X.Y.9] Comparisons with nullopt.
825 template<typename _Tp>
826 constexpr bool
827 operator==(const optional<_Tp>& __lhs, nullopt_t) noexcept
828 { return !__lhs; }
829
830 template<typename _Tp>
831 constexpr bool
832 operator==(nullopt_t, const optional<_Tp>& __rhs) noexcept
833 { return !__rhs; }
834
835 template<typename _Tp>
836 constexpr bool
837 operator!=(const optional<_Tp>& __lhs, nullopt_t) noexcept
838 { return static_cast<bool>(__lhs); }
839
840 template<typename _Tp>
841 constexpr bool
842 operator!=(nullopt_t, const optional<_Tp>& __rhs) noexcept
843 { return static_cast<bool>(__rhs); }
844
845 template<typename _Tp>
846 constexpr bool
847 operator<(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
848 { return false; }
849
850 template<typename _Tp>
851 constexpr bool
852 operator<(nullopt_t, const optional<_Tp>& __rhs) noexcept
853 { return static_cast<bool>(__rhs); }
854
855 template<typename _Tp>
856 constexpr bool
857 operator>(const optional<_Tp>& __lhs, nullopt_t) noexcept
858 { return static_cast<bool>(__lhs); }
859
860 template<typename _Tp>
861 constexpr bool
862 operator>(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
863 { return false; }
864
865 template<typename _Tp>
866 constexpr bool
867 operator<=(const optional<_Tp>& __lhs, nullopt_t) noexcept
868 { return !__lhs; }
869
870 template<typename _Tp>
871 constexpr bool
872 operator<=(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
873 { return true; }
874
875 template<typename _Tp>
876 constexpr bool
877 operator>=(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
878 { return true; }
879
880 template<typename _Tp>
881 constexpr bool
882 operator>=(nullopt_t, const optional<_Tp>& __rhs) noexcept
883 { return !__rhs; }
884
885 // [X.Y.10] Comparisons with value type.
886 template<typename _Tp>
887 constexpr bool
888 operator==(const optional<_Tp>& __lhs, const _Tp& __rhs)
889 { return __lhs && *__lhs == __rhs; }
890
891 template<typename _Tp>
892 constexpr bool
893 operator==(const _Tp& __lhs, const optional<_Tp>& __rhs)
894 { return __rhs && __lhs == *__rhs; }
895
896 template<typename _Tp>
897 constexpr bool
898 operator!=(const optional<_Tp>& __lhs, _Tp const& __rhs)
899 { return !__lhs || !(*__lhs == __rhs); }
900
901 template<typename _Tp>
902 constexpr bool
903 operator!=(const _Tp& __lhs, const optional<_Tp>& __rhs)
904 { return !__rhs || !(__lhs == *__rhs); }
905
906 template<typename _Tp>
907 constexpr bool
908 operator<(const optional<_Tp>& __lhs, const _Tp& __rhs)
909 { return !__lhs || *__lhs < __rhs; }
910
911 template<typename _Tp>
912 constexpr bool
913 operator<(const _Tp& __lhs, const optional<_Tp>& __rhs)
914 { return __rhs && __lhs < *__rhs; }
915
916 template<typename _Tp>
917 constexpr bool
918 operator>(const optional<_Tp>& __lhs, const _Tp& __rhs)
919 { return __lhs && __rhs < *__lhs; }
920
921 template<typename _Tp>
922 constexpr bool
923 operator>(const _Tp& __lhs, const optional<_Tp>& __rhs)
924 { return !__rhs || *__rhs < __lhs; }
925
926 template<typename _Tp>
927 constexpr bool
928 operator<=(const optional<_Tp>& __lhs, const _Tp& __rhs)
929 { return !__lhs || !(__rhs < *__lhs); }
930
931 template<typename _Tp>
932 constexpr bool
933 operator<=(const _Tp& __lhs, const optional<_Tp>& __rhs)
934 { return __rhs && !(*__rhs < __lhs); }
935
936 template<typename _Tp>
937 constexpr bool
938 operator>=(const optional<_Tp>& __lhs, const _Tp& __rhs)
939 { return __lhs && !(*__lhs < __rhs); }
940
941 template<typename _Tp>
942 constexpr bool
943 operator>=(const _Tp& __lhs, const optional<_Tp>& __rhs)
944 { return !__rhs || !(__lhs < *__rhs); }
945
946 // [X.Y.11]
947 template<typename _Tp>
948 inline void
949 swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs)
950 noexcept(noexcept(__lhs.swap(__rhs)))
951 { __lhs.swap(__rhs); }
952
953 template<typename _Tp>
954 constexpr optional<decay_t<_Tp>>
955 make_optional(_Tp&& __t)
956 { return optional<decay_t<_Tp>> { std::forward<_Tp>(__t) }; }
957
958 // [X.Y.12]
959 template<typename _Tp>
960 struct hash<optional<_Tp>>
961 {
962 using result_type = size_t;
963 using argument_type = optional<_Tp>;
964
965 size_t
966 operator()(const optional<_Tp>& __t) const
967 noexcept(noexcept(hash<_Tp> {}(*__t)))
968 {
969 // We pick an arbitrary hash for disengaged optionals which hopefully
970 // usual values of _Tp won't typically hash to.
971 constexpr size_t __magic_disengaged_hash = static_cast<size_t>(-3333);
972 return __t ? hash<_Tp> {}(*__t) : __magic_disengaged_hash;
973 }
974 };
975
976 /// @}
977
978 _GLIBCXX_END_NAMESPACE_VERSION
979 } // namespace std
980
981 #endif // C++17
982
983 #endif // _GLIBCXX_OPTIONAL