]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/std/optional
Make optional::reset noexcept, make optional::value work in constant expressions.
[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 bool> = true>
579 optional&
580 operator=(_Up&& __u)
581 {
582 if (this->_M_is_engaged())
583 this->_M_get() = std::forward<_Up>(__u);
584 else
585 this->_M_construct(std::forward<_Up>(__u));
586
587 return *this;
588 }
589
590 template<typename _Up,
591 enable_if_t<__and_<
592 __not_<is_same<_Tp, _Up>>,
593 is_constructible<_Tp, const _Up&>,
594 is_assignable<_Tp&, _Up>,
595 __not_<__converts_from_optional<_Tp, _Up>>,
596 __not_<__assigns_from_optional<_Tp, _Up>>
597 >::value,
598 bool> = true>
599 optional&
600 operator=(const optional<_Up>& __u)
601 {
602 if (__u)
603 {
604 if (this->_M_is_engaged())
605 this->_M_get() = *__u;
606 else
607 this->_M_construct(*__u);
608 }
609 else
610 {
611 this->_M_reset();
612 }
613 return *this;
614 }
615
616 template<typename _Up,
617 enable_if_t<__and_<
618 __not_<is_same<_Tp, _Up>>,
619 is_constructible<_Tp, _Up>,
620 is_assignable<_Tp&, _Up>,
621 __not_<__converts_from_optional<_Tp, _Up>>,
622 __not_<__assigns_from_optional<_Tp, _Up>>
623 >::value,
624 bool> = true>
625 optional&
626 operator=(optional<_Up>&& __u)
627 {
628 if (__u)
629 {
630 if (this->_M_is_engaged())
631 this->_M_get() = std::move(*__u);
632 else
633 this->_M_construct(std::move(*__u));
634 }
635 else
636 {
637 this->_M_reset();
638 }
639
640 return *this;
641 }
642
643 template<typename... _Args>
644 enable_if_t<is_constructible<_Tp, _Args&&...>::value>
645 emplace(_Args&&... __args)
646 {
647 this->_M_reset();
648 this->_M_construct(std::forward<_Args>(__args)...);
649 }
650
651 template<typename _Up, typename... _Args>
652 enable_if_t<is_constructible<_Tp, initializer_list<_Up>&,
653 _Args&&...>::value>
654 emplace(initializer_list<_Up> __il, _Args&&... __args)
655 {
656 this->_M_reset();
657 this->_M_construct(__il, std::forward<_Args>(__args)...);
658 }
659
660 // Destructor is implicit, implemented in _Optional_base.
661
662 // Swap.
663 void
664 swap(optional& __other)
665 noexcept(is_nothrow_move_constructible<_Tp>()
666 && noexcept(swap(declval<_Tp&>(), declval<_Tp&>())))
667 {
668 using std::swap;
669
670 if (this->_M_is_engaged() && __other._M_is_engaged())
671 swap(this->_M_get(), __other._M_get());
672 else if (this->_M_is_engaged())
673 {
674 __other._M_construct(std::move(this->_M_get()));
675 this->_M_destruct();
676 }
677 else if (__other._M_is_engaged())
678 {
679 this->_M_construct(std::move(__other._M_get()));
680 __other._M_destruct();
681 }
682 }
683
684 // Observers.
685 constexpr const _Tp*
686 operator->() const
687 { return __constexpr_addressof(this->_M_get()); }
688
689 _Tp*
690 operator->()
691 { return std::__addressof(this->_M_get()); }
692
693 constexpr const _Tp&
694 operator*() const&
695 { return this->_M_get(); }
696
697 constexpr _Tp&
698 operator*()&
699 { return this->_M_get(); }
700
701 constexpr _Tp&&
702 operator*()&&
703 { return std::move(this->_M_get()); }
704
705 constexpr const _Tp&&
706 operator*() const&&
707 { return std::move(this->_M_get()); }
708
709 constexpr explicit operator bool() const noexcept
710 { return this->_M_is_engaged(); }
711
712 constexpr bool has_value() const noexcept
713 { return this->_M_is_engaged(); }
714
715 constexpr const _Tp&
716 value() const&
717 {
718 return this->_M_is_engaged()
719 ? this->_M_get()
720 : (__throw_bad_optional_access("Attempt to access value of a "
721 "disengaged optional object"),
722 this->_M_get());
723 }
724
725 constexpr _Tp&
726 value()&
727 {
728 return this->_M_is_engaged()
729 ? this->_M_get()
730 : (__throw_bad_optional_access("Attempt to access value of a "
731 "disengaged optional object"),
732 this->_M_get());
733 }
734
735 constexpr _Tp&&
736 value()&&
737 {
738 return this->_M_is_engaged()
739 ? std::move(this->_M_get())
740 : (__throw_bad_optional_access("Attempt to access value of a "
741 "disengaged optional object"),
742 std::move(this->_M_get()));
743 }
744
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("Attempt to access value of a "
751 "disengaged optional object"),
752 std::move(this->_M_get()));
753 }
754
755 template<typename _Up>
756 constexpr _Tp
757 value_or(_Up&& __u) const&
758 {
759 static_assert(__and_<is_copy_constructible<_Tp>,
760 is_convertible<_Up&&, _Tp>>(),
761 "Cannot return value");
762
763 return this->_M_is_engaged()
764 ? this->_M_get()
765 : static_cast<_Tp>(std::forward<_Up>(__u));
766 }
767
768 template<typename _Up>
769 _Tp
770 value_or(_Up&& __u) &&
771 {
772 static_assert(__and_<is_move_constructible<_Tp>,
773 is_convertible<_Up&&, _Tp>>(),
774 "Cannot return value" );
775
776 return this->_M_is_engaged()
777 ? std::move(this->_M_get())
778 : static_cast<_Tp>(std::forward<_Up>(__u));
779 }
780 void reset() noexcept { this->_M_reset(); }
781 };
782
783 template<typename _Tp>
784 using __optional_relop_t =
785 enable_if_t<is_convertible<_Tp, bool>::value, bool>;
786
787 // Comparisons between optional values.
788 template<typename _Tp>
789 constexpr auto
790 operator==(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
791 -> __optional_relop_t<decltype(declval<_Tp>() == declval<_Tp>())>
792 {
793 return static_cast<bool>(__lhs) == static_cast<bool>(__rhs)
794 && (!__lhs || *__lhs == *__rhs);
795 }
796
797 template<typename _Tp>
798 constexpr auto
799 operator!=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
800 -> __optional_relop_t<decltype(declval<_Tp>() != declval<_Tp>())>
801 {
802 return static_cast<bool>(__lhs) != static_cast<bool>(__rhs)
803 || (static_cast<bool>(__lhs) && *__lhs != *__rhs);
804 }
805
806 template<typename _Tp>
807 constexpr auto
808 operator<(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
809 -> __optional_relop_t<decltype(declval<_Tp>() < declval<_Tp>())>
810 {
811 return static_cast<bool>(__rhs) && (!__lhs || *__lhs < *__rhs);
812 }
813
814 template<typename _Tp>
815 constexpr auto
816 operator>(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
817 -> __optional_relop_t<decltype(declval<_Tp>() > declval<_Tp>())>
818 {
819 return static_cast<bool>(__lhs) && (!__rhs || *__lhs > *__rhs);
820 }
821
822 template<typename _Tp>
823 constexpr auto
824 operator<=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
825 -> __optional_relop_t<decltype(declval<_Tp>() <= declval<_Tp>())>
826 {
827 return !__lhs || (static_cast<bool>(__rhs) && *__lhs <= *__rhs);
828 }
829
830 template<typename _Tp>
831 constexpr auto
832 operator>=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
833 -> __optional_relop_t<decltype(declval<_Tp>() >= declval<_Tp>())>
834 {
835 return !__rhs || (static_cast<bool>(__lhs) && *__lhs >= *__rhs);
836 }
837
838 // Comparisons with nullopt.
839 template<typename _Tp>
840 constexpr bool
841 operator==(const optional<_Tp>& __lhs, nullopt_t) noexcept
842 { return !__lhs; }
843
844 template<typename _Tp>
845 constexpr bool
846 operator==(nullopt_t, const optional<_Tp>& __rhs) noexcept
847 { return !__rhs; }
848
849 template<typename _Tp>
850 constexpr bool
851 operator!=(const optional<_Tp>& __lhs, nullopt_t) noexcept
852 { return static_cast<bool>(__lhs); }
853
854 template<typename _Tp>
855 constexpr bool
856 operator!=(nullopt_t, const optional<_Tp>& __rhs) noexcept
857 { return static_cast<bool>(__rhs); }
858
859 template<typename _Tp>
860 constexpr bool
861 operator<(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
862 { return false; }
863
864 template<typename _Tp>
865 constexpr bool
866 operator<(nullopt_t, const optional<_Tp>& __rhs) noexcept
867 { return static_cast<bool>(__rhs); }
868
869 template<typename _Tp>
870 constexpr bool
871 operator>(const optional<_Tp>& __lhs, nullopt_t) noexcept
872 { return static_cast<bool>(__lhs); }
873
874 template<typename _Tp>
875 constexpr bool
876 operator>(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
877 { return false; }
878
879 template<typename _Tp>
880 constexpr bool
881 operator<=(const optional<_Tp>& __lhs, nullopt_t) noexcept
882 { return !__lhs; }
883
884 template<typename _Tp>
885 constexpr bool
886 operator<=(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
887 { return true; }
888
889 template<typename _Tp>
890 constexpr bool
891 operator>=(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
892 { return true; }
893
894 template<typename _Tp>
895 constexpr bool
896 operator>=(nullopt_t, const optional<_Tp>& __rhs) noexcept
897 { return !__rhs; }
898
899 // Comparisons with value type.
900 template<typename _Tp>
901 constexpr auto
902 operator==(const optional<_Tp>& __lhs, const _Tp& __rhs)
903 -> __optional_relop_t<decltype(declval<_Tp>() == declval<_Tp>())>
904 { return __lhs && *__lhs == __rhs; }
905
906 template<typename _Tp>
907 constexpr auto
908 operator==(const _Tp& __lhs, const optional<_Tp>& __rhs)
909 -> __optional_relop_t<decltype(declval<_Tp>() == declval<_Tp>())>
910 { return __rhs && __lhs == *__rhs; }
911
912 template<typename _Tp>
913 constexpr auto
914 operator!=(const optional<_Tp>& __lhs, const _Tp& __rhs)
915 -> __optional_relop_t<decltype(declval<_Tp>() != declval<_Tp>())>
916 { return !__lhs || *__lhs != __rhs; }
917
918 template<typename _Tp>
919 constexpr auto
920 operator!=(const _Tp& __lhs, const optional<_Tp>& __rhs)
921 -> __optional_relop_t<decltype(declval<_Tp>() != declval<_Tp>())>
922 { return !__rhs || __lhs != *__rhs; }
923
924 template<typename _Tp>
925 constexpr auto
926 operator<(const optional<_Tp>& __lhs, const _Tp& __rhs)
927 -> __optional_relop_t<decltype(declval<_Tp>() < declval<_Tp>())>
928 { return !__lhs || *__lhs < __rhs; }
929
930 template<typename _Tp>
931 constexpr auto
932 operator<(const _Tp& __lhs, const optional<_Tp>& __rhs)
933 -> __optional_relop_t<decltype(declval<_Tp>() < declval<_Tp>())>
934 { return __rhs && __lhs < *__rhs; }
935
936 template<typename _Tp>
937 constexpr auto
938 operator>(const optional<_Tp>& __lhs, const _Tp& __rhs)
939 -> __optional_relop_t<decltype(declval<_Tp>() > declval<_Tp>())>
940 { return __lhs && *__lhs > __rhs; }
941
942 template<typename _Tp>
943 constexpr auto
944 operator>(const _Tp& __lhs, const optional<_Tp>& __rhs)
945 -> __optional_relop_t<decltype(declval<_Tp>() > declval<_Tp>())>
946 { return !__rhs || __lhs > *__rhs; }
947
948 template<typename _Tp>
949 constexpr auto
950 operator<=(const optional<_Tp>& __lhs, const _Tp& __rhs)
951 -> __optional_relop_t<decltype(declval<_Tp>() <= declval<_Tp>())>
952 { return !__lhs || *__lhs <= __rhs; }
953
954 template<typename _Tp>
955 constexpr auto
956 operator<=(const _Tp& __lhs, const optional<_Tp>& __rhs)
957 -> __optional_relop_t<decltype(declval<_Tp>() <= declval<_Tp>())>
958 { return __rhs && __lhs <= *__rhs; }
959
960 template<typename _Tp>
961 constexpr auto
962 operator>=(const optional<_Tp>& __lhs, const _Tp& __rhs)
963 -> __optional_relop_t<decltype(declval<_Tp>() >= declval<_Tp>())>
964 { return __lhs && *__lhs >= __rhs; }
965
966 template<typename _Tp>
967 constexpr auto
968 operator>=(const _Tp& __lhs, const optional<_Tp>& __rhs)
969 -> __optional_relop_t<decltype(declval<_Tp>() >= declval<_Tp>())>
970 { return !__rhs || __lhs >= *__rhs; }
971
972 // Swap and creation functions.
973 template<typename _Tp>
974 inline void
975 swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs)
976 noexcept(noexcept(__lhs.swap(__rhs)))
977 { __lhs.swap(__rhs); }
978
979 template<typename _Tp>
980 constexpr optional<decay_t<_Tp>>
981 make_optional(_Tp&& __t)
982 { return optional<decay_t<_Tp>> { std::forward<_Tp>(__t) }; }
983
984 template<typename _Tp, typename ..._Args>
985 constexpr optional<_Tp>
986 make_optional(_Args&&... __args)
987 { return optional<_Tp> { in_place, std::forward<_Args>(__args)... }; }
988
989 template<typename _Tp, typename _Up, typename ..._Args>
990 constexpr optional<_Tp>
991 make_optional(initializer_list<_Up> __il, _Args&&... __args)
992 { return optional<_Tp> { in_place, __il, std::forward<_Args>(__args)... }; }
993
994 // Hash.
995 template<typename _Tp>
996 struct hash<optional<_Tp>>
997 {
998 using result_type = size_t;
999 using argument_type = optional<_Tp>;
1000
1001 size_t
1002 operator()(const optional<_Tp>& __t) const
1003 noexcept(noexcept(hash<_Tp> {}(*__t)))
1004 {
1005 // We pick an arbitrary hash for disengaged optionals which hopefully
1006 // usual values of _Tp won't typically hash to.
1007 constexpr size_t __magic_disengaged_hash = static_cast<size_t>(-3333);
1008 return __t ? hash<_Tp> {}(*__t) : __magic_disengaged_hash;
1009 }
1010 };
1011
1012 /// @}
1013
1014 _GLIBCXX_END_NAMESPACE_VERSION
1015 } // namespace std
1016
1017 #endif // C++17
1018
1019 #endif // _GLIBCXX_OPTIONAL