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