]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/std/optional
Implement LWG 2900, The copy and move constructors of optional are not constexpr.
[thirdparty/gcc.git] / libstdc++-v3 / include / std / optional
1 // <optional> -*- C++ -*-
2
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.
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 inline 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 exception
80 {
81 public:
82 bad_optional_access() { }
83 virtual const char* what() const noexcept override
84 {return "bad optional access";}
85
86 virtual ~bad_optional_access() noexcept = default;
87 };
88
89 void
90 __throw_bad_optional_access()
91 __attribute__((__noreturn__));
92
93 // XXX Does not belong here.
94 inline void
95 __throw_bad_optional_access()
96 { _GLIBCXX_THROW_OR_ABORT(bad_optional_access()); }
97
98
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() {}
110
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 {}
116
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) {}
122
123 template <class _Up> struct __ctor_tag {};
124
125 constexpr _Optional_payload(__ctor_tag<bool>,
126 const _Tp& __other)
127 : _M_payload(__other),
128 _M_engaged(true)
129 {}
130
131 constexpr _Optional_payload(__ctor_tag<void>)
132 : _M_empty()
133 {}
134
135 constexpr _Optional_payload(__ctor_tag<bool>, _Tp&& __other)
136 : _M_payload(std::move(__other)),
137 _M_engaged(true)
138 {}
139
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 {}
147
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 {}
155
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 };
164
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() {}
171
172 template <typename... _Args>
173 constexpr _Optional_payload(in_place_t, _Args&&... __args)
174 : _M_payload(std::forward<_Args>(__args)...),
175 _M_engaged(true) {}
176
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 {}
186
187 constexpr
188 _Optional_payload(bool __engaged, _Optional_payload&& __other)
189 : _Optional_payload(std::move(__other))
190 {}
191
192 constexpr _Optional_payload(const _Optional_payload& __other)
193 {
194 if (__other._M_engaged)
195 this->_M_construct(__other._M_payload);
196 }
197
198 constexpr _Optional_payload(_Optional_payload&& __other)
199 {
200 if (__other._M_engaged)
201 this->_M_construct(std::move(__other._M_payload));
202 }
203
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;
211
212 ~_Optional_payload()
213 {
214 if (_M_engaged)
215 _M_payload.~_Stored_type();
216 }
217
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 };
228
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() {}
235
236 template <typename... _Args>
237 constexpr _Optional_payload(in_place_t, _Args&&... __args)
238 : _M_payload(std::forward<_Args>(__args)...),
239 _M_engaged(true) {}
240
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 {}
250
251 constexpr
252 _Optional_payload(bool __engaged, _Optional_payload&& __other)
253 : _Optional_payload(std::move(__other))
254 {}
255
256 constexpr _Optional_payload(const _Optional_payload& __other)
257 {
258 if (__other._M_engaged)
259 this->_M_construct(__other._M_payload);
260 }
261
262 constexpr _Optional_payload(_Optional_payload&& __other)
263 {
264 if (__other._M_engaged)
265 this->_M_construct(std::move(__other._M_payload));
266 }
267
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;
275
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 };
286
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>;
307
308 public:
309
310 // Constructors for disengaged optionals.
311 constexpr _Optional_base() noexcept
312 { }
313
314 constexpr _Optional_base(nullopt_t) noexcept
315 { }
316
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)...) { }
323
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 { }
334
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 { }
340
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 { }
346
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 }
360
361 return *this;
362 }
363
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; }
385
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; }
390
391 constexpr const _Tp&
392 _M_get() const noexcept
393 { return this->_M_payload._M_payload; }
394
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 }
406
407 void
408 _M_destruct()
409 {
410 this->_M_payload._M_engaged = false;
411 this->_M_payload._M_payload.~_Stored_type();
412 }
413
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 }
421
422 private:
423 _Optional_payload<_Tp> _M_payload;
424 };
425
426 template<typename _Tp>
427 class optional;
428
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>>;
439
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>&&>>;
446
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(__and_<__not_<is_same<remove_cv_t<_Tp>, nullopt_t>>,
466 __not_<is_same<remove_cv_t<_Tp>, in_place_t>>,
467 __not_<is_reference<_Tp>>>(),
468 "Invalid instantiation of optional<T>");
469
470 private:
471 using _Base = _Optional_base<_Tp>;
472
473 public:
474 using value_type = _Tp;
475
476 constexpr optional() = default;
477
478 constexpr optional(nullopt_t) noexcept
479 : _Base(nullopt) { }
480
481 // Converting constructors for engaged optionals.
482 template <typename _Up = _Tp,
483 enable_if_t<__and_<
484 __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
485 __not_<is_same<in_place_t, decay_t<_Up>>>,
486 is_constructible<_Tp, _Up&&>,
487 is_convertible<_Up&&, _Tp>
488 >::value, bool> = true>
489 constexpr optional(_Up&& __t)
490 : _Base(std::in_place, std::forward<_Up>(__t)) { }
491
492 template <typename _Up = _Tp,
493 enable_if_t<__and_<
494 __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
495 __not_<is_same<in_place_t, decay_t<_Up>>>,
496 is_constructible<_Tp, _Up&&>,
497 __not_<is_convertible<_Up&&, _Tp>>
498 >::value, bool> = false>
499 explicit constexpr optional(_Up&& __t)
500 : _Base(std::in_place, std::forward<_Up>(__t)) { }
501
502 template <typename _Up,
503 enable_if_t<__and_<
504 __not_<is_same<_Tp, _Up>>,
505 is_constructible<_Tp, const _Up&>,
506 is_convertible<const _Up&, _Tp>,
507 __not_<__converts_from_optional<_Tp, _Up>>
508 >::value, bool> = true>
509 constexpr optional(const optional<_Up>& __t)
510 {
511 if (__t)
512 emplace(*__t);
513 }
514
515 template <typename _Up,
516 enable_if_t<__and_<
517 __not_<is_same<_Tp, _Up>>,
518 is_constructible<_Tp, const _Up&>,
519 __not_<is_convertible<const _Up&, _Tp>>,
520 __not_<__converts_from_optional<_Tp, _Up>>
521 >::value, bool> = false>
522 explicit constexpr optional(const optional<_Up>& __t)
523 {
524 if (__t)
525 emplace(*__t);
526 }
527
528 template <typename _Up,
529 enable_if_t<__and_<
530 __not_<is_same<_Tp, _Up>>,
531 is_constructible<_Tp, _Up&&>,
532 is_convertible<_Up&&, _Tp>,
533 __not_<__converts_from_optional<_Tp, _Up>>
534 >::value, bool> = true>
535 constexpr optional(optional<_Up>&& __t)
536 {
537 if (__t)
538 emplace(std::move(*__t));
539 }
540
541 template <typename _Up,
542 enable_if_t<__and_<
543 __not_<is_same<_Tp, _Up>>,
544 is_constructible<_Tp, _Up&&>,
545 __not_<is_convertible<_Up&&, _Tp>>,
546 __not_<__converts_from_optional<_Tp, _Up>>
547 >::value, bool> = false>
548 explicit constexpr optional(optional<_Up>&& __t)
549 {
550 if (__t)
551 emplace(std::move(*__t));
552 }
553
554 template<typename... _Args,
555 enable_if_t<is_constructible_v<_Tp, _Args&&...>, bool> = false>
556 explicit constexpr optional(in_place_t, _Args&&... __args)
557 : _Base(std::in_place, std::forward<_Args>(__args)...) { }
558
559 template<typename _Up, typename... _Args,
560 enable_if_t<is_constructible_v<_Tp,
561 initializer_list<_Up>&,
562 _Args&&...>, bool> = false>
563 explicit constexpr optional(in_place_t,
564 initializer_list<_Up> __il,
565 _Args&&... __args)
566 : _Base(std::in_place, __il, std::forward<_Args>(__args)...) { }
567
568 // Assignment operators.
569 optional&
570 operator=(nullopt_t) noexcept
571 {
572 this->_M_reset();
573 return *this;
574 }
575
576 template<typename _Up = _Tp>
577 enable_if_t<__and_<
578 __not_<is_same<optional<_Tp>, decay_t<_Up>>>,
579 is_constructible<_Tp, _Up>,
580 __not_<__and_<is_scalar<_Tp>,
581 is_same<_Tp, decay_t<_Up>>>>,
582 is_assignable<_Tp&, _Up>>::value,
583 optional&>
584 operator=(_Up&& __u)
585 {
586 if (this->_M_is_engaged())
587 this->_M_get() = std::forward<_Up>(__u);
588 else
589 this->_M_construct(std::forward<_Up>(__u));
590
591 return *this;
592 }
593
594 template<typename _Up>
595 enable_if_t<__and_<
596 __not_<is_same<_Tp, _Up>>,
597 is_constructible<_Tp, const _Up&>,
598 is_assignable<_Tp&, _Up>,
599 __not_<__converts_from_optional<_Tp, _Up>>,
600 __not_<__assigns_from_optional<_Tp, _Up>>
601 >::value,
602 optional&>
603 operator=(const optional<_Up>& __u)
604 {
605 if (__u)
606 {
607 if (this->_M_is_engaged())
608 this->_M_get() = *__u;
609 else
610 this->_M_construct(*__u);
611 }
612 else
613 {
614 this->_M_reset();
615 }
616 return *this;
617 }
618
619 template<typename _Up>
620 enable_if_t<__and_<
621 __not_<is_same<_Tp, _Up>>,
622 is_constructible<_Tp, _Up>,
623 is_assignable<_Tp&, _Up>,
624 __not_<__converts_from_optional<_Tp, _Up>>,
625 __not_<__assigns_from_optional<_Tp, _Up>>
626 >::value,
627 optional&>
628 operator=(optional<_Up>&& __u)
629 {
630 if (__u)
631 {
632 if (this->_M_is_engaged())
633 this->_M_get() = std::move(*__u);
634 else
635 this->_M_construct(std::move(*__u));
636 }
637 else
638 {
639 this->_M_reset();
640 }
641
642 return *this;
643 }
644
645 template<typename... _Args>
646 enable_if_t<is_constructible<_Tp, _Args&&...>::value, _Tp&>
647 emplace(_Args&&... __args)
648 {
649 this->_M_reset();
650 this->_M_construct(std::forward<_Args>(__args)...);
651 return this->_M_get();
652 }
653
654 template<typename _Up, typename... _Args>
655 enable_if_t<is_constructible<_Tp, initializer_list<_Up>&,
656 _Args&&...>::value, _Tp&>
657 emplace(initializer_list<_Up> __il, _Args&&... __args)
658 {
659 this->_M_reset();
660 this->_M_construct(__il, std::forward<_Args>(__args)...);
661 return this->_M_get();
662 }
663
664 // Destructor is implicit, implemented in _Optional_base.
665
666 // Swap.
667 void
668 swap(optional& __other)
669 noexcept(is_nothrow_move_constructible<_Tp>()
670 && is_nothrow_swappable_v<_Tp>)
671 {
672 using std::swap;
673
674 if (this->_M_is_engaged() && __other._M_is_engaged())
675 swap(this->_M_get(), __other._M_get());
676 else if (this->_M_is_engaged())
677 {
678 __other._M_construct(std::move(this->_M_get()));
679 this->_M_destruct();
680 }
681 else if (__other._M_is_engaged())
682 {
683 this->_M_construct(std::move(__other._M_get()));
684 __other._M_destruct();
685 }
686 }
687
688 // Observers.
689 constexpr const _Tp*
690 operator->() const
691 { return std::__addressof(this->_M_get()); }
692
693 _Tp*
694 operator->()
695 { return std::__addressof(this->_M_get()); }
696
697 constexpr const _Tp&
698 operator*() const&
699 { return this->_M_get(); }
700
701 constexpr _Tp&
702 operator*()&
703 { return this->_M_get(); }
704
705 constexpr _Tp&&
706 operator*()&&
707 { return std::move(this->_M_get()); }
708
709 constexpr const _Tp&&
710 operator*() const&&
711 { return std::move(this->_M_get()); }
712
713 constexpr explicit operator bool() const noexcept
714 { return this->_M_is_engaged(); }
715
716 constexpr bool has_value() const noexcept
717 { return this->_M_is_engaged(); }
718
719 constexpr const _Tp&
720 value() const&
721 {
722 return this->_M_is_engaged()
723 ? this->_M_get()
724 : (__throw_bad_optional_access(),
725 this->_M_get());
726 }
727
728 constexpr _Tp&
729 value()&
730 {
731 return this->_M_is_engaged()
732 ? this->_M_get()
733 : (__throw_bad_optional_access(),
734 this->_M_get());
735 }
736
737 constexpr _Tp&&
738 value()&&
739 {
740 return this->_M_is_engaged()
741 ? std::move(this->_M_get())
742 : (__throw_bad_optional_access(),
743 std::move(this->_M_get()));
744 }
745
746 constexpr const _Tp&&
747 value() const&&
748 {
749 return this->_M_is_engaged()
750 ? std::move(this->_M_get())
751 : (__throw_bad_optional_access(),
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, typename _Up>
789 constexpr auto
790 operator==(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
791 -> __optional_relop_t<decltype(declval<_Tp>() == declval<_Up>())>
792 {
793 return static_cast<bool>(__lhs) == static_cast<bool>(__rhs)
794 && (!__lhs || *__lhs == *__rhs);
795 }
796
797 template<typename _Tp, typename _Up>
798 constexpr auto
799 operator!=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
800 -> __optional_relop_t<decltype(declval<_Tp>() != declval<_Up>())>
801 {
802 return static_cast<bool>(__lhs) != static_cast<bool>(__rhs)
803 || (static_cast<bool>(__lhs) && *__lhs != *__rhs);
804 }
805
806 template<typename _Tp, typename _Up>
807 constexpr auto
808 operator<(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
809 -> __optional_relop_t<decltype(declval<_Tp>() < declval<_Up>())>
810 {
811 return static_cast<bool>(__rhs) && (!__lhs || *__lhs < *__rhs);
812 }
813
814 template<typename _Tp, typename _Up>
815 constexpr auto
816 operator>(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
817 -> __optional_relop_t<decltype(declval<_Tp>() > declval<_Up>())>
818 {
819 return static_cast<bool>(__lhs) && (!__rhs || *__lhs > *__rhs);
820 }
821
822 template<typename _Tp, typename _Up>
823 constexpr auto
824 operator<=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
825 -> __optional_relop_t<decltype(declval<_Tp>() <= declval<_Up>())>
826 {
827 return !__lhs || (static_cast<bool>(__rhs) && *__lhs <= *__rhs);
828 }
829
830 template<typename _Tp, typename _Up>
831 constexpr auto
832 operator>=(const optional<_Tp>& __lhs, const optional<_Up>& __rhs)
833 -> __optional_relop_t<decltype(declval<_Tp>() >= declval<_Up>())>
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, typename _Up>
901 constexpr auto
902 operator==(const optional<_Tp>& __lhs, const _Up& __rhs)
903 -> __optional_relop_t<decltype(declval<_Tp>() == declval<_Up>())>
904 { return __lhs && *__lhs == __rhs; }
905
906 template<typename _Tp, typename _Up>
907 constexpr auto
908 operator==(const _Up& __lhs, const optional<_Tp>& __rhs)
909 -> __optional_relop_t<decltype(declval<_Up>() == declval<_Tp>())>
910 { return __rhs && __lhs == *__rhs; }
911
912 template<typename _Tp, typename _Up>
913 constexpr auto
914 operator!=(const optional<_Tp>& __lhs, const _Up& __rhs)
915 -> __optional_relop_t<decltype(declval<_Tp>() != declval<_Up>())>
916 { return !__lhs || *__lhs != __rhs; }
917
918 template<typename _Tp, typename _Up>
919 constexpr auto
920 operator!=(const _Up& __lhs, const optional<_Tp>& __rhs)
921 -> __optional_relop_t<decltype(declval<_Up>() != declval<_Tp>())>
922 { return !__rhs || __lhs != *__rhs; }
923
924 template<typename _Tp, typename _Up>
925 constexpr auto
926 operator<(const optional<_Tp>& __lhs, const _Up& __rhs)
927 -> __optional_relop_t<decltype(declval<_Tp>() < declval<_Up>())>
928 { return !__lhs || *__lhs < __rhs; }
929
930 template<typename _Tp, typename _Up>
931 constexpr auto
932 operator<(const _Up& __lhs, const optional<_Tp>& __rhs)
933 -> __optional_relop_t<decltype(declval<_Up>() < declval<_Tp>())>
934 { return __rhs && __lhs < *__rhs; }
935
936 template<typename _Tp, typename _Up>
937 constexpr auto
938 operator>(const optional<_Tp>& __lhs, const _Up& __rhs)
939 -> __optional_relop_t<decltype(declval<_Tp>() > declval<_Up>())>
940 { return __lhs && *__lhs > __rhs; }
941
942 template<typename _Tp, typename _Up>
943 constexpr auto
944 operator>(const _Up& __lhs, const optional<_Tp>& __rhs)
945 -> __optional_relop_t<decltype(declval<_Up>() > declval<_Tp>())>
946 { return !__rhs || __lhs > *__rhs; }
947
948 template<typename _Tp, typename _Up>
949 constexpr auto
950 operator<=(const optional<_Tp>& __lhs, const _Up& __rhs)
951 -> __optional_relop_t<decltype(declval<_Tp>() <= declval<_Up>())>
952 { return !__lhs || *__lhs <= __rhs; }
953
954 template<typename _Tp, typename _Up>
955 constexpr auto
956 operator<=(const _Up& __lhs, const optional<_Tp>& __rhs)
957 -> __optional_relop_t<decltype(declval<_Up>() <= declval<_Tp>())>
958 { return __rhs && __lhs <= *__rhs; }
959
960 template<typename _Tp, typename _Up>
961 constexpr auto
962 operator>=(const optional<_Tp>& __lhs, const _Up& __rhs)
963 -> __optional_relop_t<decltype(declval<_Tp>() >= declval<_Up>())>
964 { return __lhs && *__lhs >= __rhs; }
965
966 template<typename _Tp, typename _Up>
967 constexpr auto
968 operator>=(const _Up& __lhs, const optional<_Tp>& __rhs)
969 -> __optional_relop_t<decltype(declval<_Up>() >= declval<_Tp>())>
970 { return !__rhs || __lhs >= *__rhs; }
971
972 // Swap and creation functions.
973
974 // _GLIBCXX_RESOLVE_LIB_DEFECTS
975 // 2748. swappable traits for optionals
976 template<typename _Tp>
977 inline enable_if_t<is_move_constructible_v<_Tp> && is_swappable_v<_Tp>>
978 swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs)
979 noexcept(noexcept(__lhs.swap(__rhs)))
980 { __lhs.swap(__rhs); }
981
982 template<typename _Tp>
983 enable_if_t<!(is_move_constructible_v<_Tp> && is_swappable_v<_Tp>)>
984 swap(optional<_Tp>&, optional<_Tp>&) = delete;
985
986 template<typename _Tp>
987 constexpr optional<decay_t<_Tp>>
988 make_optional(_Tp&& __t)
989 { return optional<decay_t<_Tp>> { std::forward<_Tp>(__t) }; }
990
991 template<typename _Tp, typename ..._Args>
992 constexpr optional<_Tp>
993 make_optional(_Args&&... __args)
994 { return optional<_Tp> { in_place, std::forward<_Args>(__args)... }; }
995
996 template<typename _Tp, typename _Up, typename ..._Args>
997 constexpr optional<_Tp>
998 make_optional(initializer_list<_Up> __il, _Args&&... __args)
999 { return optional<_Tp> { in_place, __il, std::forward<_Args>(__args)... }; }
1000
1001 // Hash.
1002
1003 template<typename _Tp, bool
1004 = __poison_hash<remove_const_t<_Tp>>::__enable_hash_call>
1005 struct __optional_hash_call_base
1006 {
1007 size_t
1008 operator()(const optional<_Tp>& __t) const
1009 noexcept(noexcept(hash<_Tp> {}(*__t)))
1010 {
1011 // We pick an arbitrary hash for disengaged optionals which hopefully
1012 // usual values of _Tp won't typically hash to.
1013 constexpr size_t __magic_disengaged_hash = static_cast<size_t>(-3333);
1014 return __t ? hash<_Tp> {}(*__t) : __magic_disengaged_hash;
1015 }
1016 };
1017
1018 template<typename _Tp>
1019 struct __optional_hash_call_base<_Tp, false> {};
1020
1021 template<typename _Tp>
1022 struct hash<optional<_Tp>>
1023 : private __poison_hash<remove_const_t<_Tp>>,
1024 public __optional_hash_call_base<_Tp>
1025 {
1026 using result_type = size_t;
1027 using argument_type = optional<_Tp>;
1028 };
1029
1030 /// @}
1031
1032 template <typename _Tp> optional(_Tp) -> optional<_Tp>;
1033
1034 _GLIBCXX_END_NAMESPACE_VERSION
1035 } // namespace std
1036
1037 #endif // C++17
1038
1039 #endif // _GLIBCXX_OPTIONAL