1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef _LIBCPP___OPTIONAL_OPTIONAL_H
10#define _LIBCPP___OPTIONAL_OPTIONAL_H
11
12#include <__assert>
13#include <__concepts/invocable.h>
14#include <__config>
15#include <__functional/invoke.h>
16#include <__fwd/optional.h>
17#include <__iterator/bounded_iter.h>
18#include <__iterator/capacity_aware_iterator.h>
19#include <__iterator/static_packed_bounded_iter.h>
20#include <__memory/addressof.h>
21#include <__memory/construct_at.h>
22#include <__optional/common.h>
23#include <__optional/comparison.h>
24#include <__optional/hash.h>
25#include <__optional/nullopt_t.h>
26#include <__optional/swap.h>
27#include <__tuple/sfinae_helpers.h>
28#include <__type_traits/add_pointer.h>
29#include <__type_traits/conditional.h>
30#include <__type_traits/conjunction.h>
31#include <__type_traits/decay.h>
32#include <__type_traits/disjunction.h>
33#include <__type_traits/enable_if.h>
34#include <__type_traits/invoke.h>
35#include <__type_traits/is_array.h>
36#include <__type_traits/is_assignable.h>
37#include <__type_traits/is_constructible.h>
38#include <__type_traits/is_convertible.h>
39#include <__type_traits/is_destructible.h>
40#include <__type_traits/is_nothrow_assignable.h>
41#include <__type_traits/is_nothrow_constructible.h>
42#include <__type_traits/is_object.h>
43#include <__type_traits/is_reference.h>
44#include <__type_traits/is_relocatable.h>
45#include <__type_traits/is_same.h>
46#include <__type_traits/is_scalar.h>
47#include <__type_traits/is_swappable.h>
48#include <__type_traits/is_trivially_assignable.h>
49#include <__type_traits/is_trivially_constructible.h>
50#include <__type_traits/is_trivially_destructible.h>
51#include <__type_traits/negation.h>
52#include <__type_traits/remove_cv.h>
53#include <__type_traits/remove_cvref.h>
54#include <__type_traits/remove_reference.h>
55#include <__utility/forward.h>
56#include <__utility/in_place.h>
57#include <__utility/move.h>
58#include <initializer_list>
59
60#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
61# pragma GCC system_header
62#endif
63
64_LIBCPP_PUSH_MACROS
65#include <__undef_macros>
66
67#if _LIBCPP_STD_VER >= 17
68
69_LIBCPP_BEGIN_NAMESPACE_STD
70
71template <class _Tp, bool = is_trivially_destructible<_Tp>::value>
72struct __optional_destruct_base;
73
74template <class _Tp>
75struct __optional_destruct_base<_Tp, false> {
76 typedef _Tp value_type;
77# if _LIBCPP_STD_VER >= 26
78 static_assert(!is_rvalue_reference_v<_Tp>, "instantiation of optional with an rvalue reference type is ill-formed");
79# else
80 static_assert(!is_reference_v<_Tp>, "instantiation of optional with a reference type is ill-formed");
81# endif
82 static_assert(is_object_v<value_type>, "instantiation of optional with a non-object type is undefined behavior");
83 union {
84 char __null_state_;
85 remove_cv_t<value_type> __val_;
86 };
87 bool __engaged_;
88
89 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__optional_destruct_base() {
90 if (__engaged_)
91 __val_.~value_type();
92 }
93
94 _LIBCPP_HIDE_FROM_ABI constexpr __optional_destruct_base() noexcept : __null_state_(), __engaged_(false) {}
95
96 template <class... _Args>
97 _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args)
98 : __val_(std::forward<_Args>(__args)...), __engaged_(true) {}
99
100# if _LIBCPP_STD_VER >= 23
101 template <class _Fp, class... _Args>
102 _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_destruct_base(
103 __optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args)
104 : __val_(std::invoke(std::forward<_Fp>(__f), std::forward<_Args>(__args)...)), __engaged_(true) {}
105# endif
106
107 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reset() noexcept {
108 if (__engaged_) {
109 __val_.~value_type();
110 __engaged_ = false;
111 }
112 }
113};
114
115template <class _Tp>
116struct __optional_destruct_base<_Tp, true> {
117 typedef _Tp value_type;
118# if _LIBCPP_STD_VER >= 26
119 static_assert(!is_rvalue_reference_v<_Tp>, "instantiation of optional with an rvalue reference type is ill-formed");
120# else
121 static_assert(!is_reference_v<_Tp>, "instantiation of optional with a reference type is ill-formed");
122# endif
123 static_assert(is_object_v<value_type>, "instantiation of optional with a non-object type is undefined behavior");
124 union {
125 char __null_state_;
126 remove_cv_t<value_type> __val_;
127 };
128 bool __engaged_;
129
130 _LIBCPP_HIDE_FROM_ABI constexpr __optional_destruct_base() noexcept : __null_state_(), __engaged_(false) {}
131
132 template <class... _Args>
133 _LIBCPP_HIDE_FROM_ABI constexpr explicit __optional_destruct_base(in_place_t, _Args&&... __args)
134 : __val_(std::forward<_Args>(__args)...), __engaged_(true) {}
135
136# if _LIBCPP_STD_VER >= 23
137 template <class _Fp, class... _Args>
138 _LIBCPP_HIDE_FROM_ABI constexpr __optional_destruct_base(
139 __optional_construct_from_invoke_tag, _Fp&& __f, _Args&&... __args)
140 : __val_(std::invoke(std::forward<_Fp>(__f), std::forward<_Args>(__args)...)), __engaged_(true) {}
141# endif
142
143 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void reset() noexcept {
144 if (__engaged_) {
145 __engaged_ = false;
146 }
147 }
148};
149
150template <class _Tp>
151struct __optional_storage_base : __optional_destruct_base<_Tp> {
152 using __base _LIBCPP_NODEBUG = __optional_destruct_base<_Tp>;
153 using value_type = _Tp;
154 using __base::__base;
155
156 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool has_value() const noexcept { return this->__engaged_; }
157
158 // [optional.observe]
159 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp const& value() const& {
160 if (!this->has_value())
161 std::__throw_bad_optional_access();
162 return this->__val_;
163 }
164
165 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp& value() & {
166 if (!this->has_value())
167 std::__throw_bad_optional_access();
168 return this->__val_;
169 }
170
171 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& value() && {
172 if (!this->has_value())
173 std::__throw_bad_optional_access();
174 return std::move(this->__val_);
175 }
176
177 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp const&& value() const&& {
178 if (!this->has_value())
179 std::__throw_bad_optional_access();
180 return std::move(this->__val_);
181 }
182
183 template <class... _Args>
184 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct(_Args&&... __args) {
185 _LIBCPP_ASSERT_INTERNAL(!has_value(), "__construct called for engaged __optional_storage");
186 std::__construct_at(std::addressof(this->__val_), std::forward<_Args>(__args)...);
187 this->__engaged_ = true;
188 }
189
190 template <class _That>
191 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __construct_from(_That&& __opt) {
192 if (__opt.has_value())
193 __construct(std::forward<_That>(__opt).value());
194 }
195
196 template <class _That>
197 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __assign_from(_That&& __opt) {
198 if (this->__engaged_ == __opt.has_value()) {
199 if (this->__engaged_)
200 static_cast<_Tp&>(this->__val_) = std::forward<_That>(__opt).value();
201 } else {
202 if (this->__engaged_)
203 this->reset();
204 else
205 __construct(std::forward<_That>(__opt).value());
206 }
207 }
208};
209
210template <class _Tp, bool = is_trivially_copy_constructible_v<_Tp>>
211struct __optional_copy_base : __optional_storage_base<_Tp> {
212 using __optional_storage_base<_Tp>::__optional_storage_base;
213};
214
215template <class _Tp>
216struct __optional_copy_base<_Tp, false> : __optional_storage_base<_Tp> {
217 using __optional_storage_base<_Tp>::__optional_storage_base;
218
219 _LIBCPP_HIDE_FROM_ABI __optional_copy_base() = default;
220
221 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __optional_copy_base(const __optional_copy_base& __opt) {
222 this->__construct_from(__opt);
223 }
224
225 _LIBCPP_HIDE_FROM_ABI __optional_copy_base(__optional_copy_base&&) = default;
226 _LIBCPP_HIDE_FROM_ABI __optional_copy_base& operator=(const __optional_copy_base&) = default;
227 _LIBCPP_HIDE_FROM_ABI __optional_copy_base& operator=(__optional_copy_base&&) = default;
228};
229
230template <class _Tp, bool = is_trivially_move_constructible_v<_Tp>>
231struct __optional_move_base : __optional_copy_base<_Tp> {
232 using __optional_copy_base<_Tp>::__optional_copy_base;
233};
234
235template <class _Tp>
236struct __optional_move_base<_Tp, false> : __optional_copy_base<_Tp> {
237 using value_type = _Tp;
238 using __optional_copy_base<_Tp>::__optional_copy_base;
239
240 _LIBCPP_HIDE_FROM_ABI __optional_move_base() = default;
241 _LIBCPP_HIDE_FROM_ABI __optional_move_base(const __optional_move_base&) = default;
242
243 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
244 __optional_move_base(__optional_move_base&& __opt) noexcept(is_nothrow_move_constructible_v<value_type>) {
245 this->__construct_from(std::move(__opt));
246 }
247
248 _LIBCPP_HIDE_FROM_ABI __optional_move_base& operator=(const __optional_move_base&) = default;
249 _LIBCPP_HIDE_FROM_ABI __optional_move_base& operator=(__optional_move_base&&) = default;
250};
251
252template <class _Tp,
253 bool = (is_trivially_destructible_v<_Tp> && is_trivially_copy_constructible_v<_Tp> &&
254 is_trivially_copy_assignable_v<_Tp>)>
255struct __optional_copy_assign_base : __optional_move_base<_Tp> {
256 using __optional_move_base<_Tp>::__optional_move_base;
257};
258
259template <class _Tp>
260struct __optional_copy_assign_base<_Tp, false> : __optional_move_base<_Tp> {
261 using __optional_move_base<_Tp>::__optional_move_base;
262
263 _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base() = default;
264 _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base(const __optional_copy_assign_base&) = default;
265 _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base(__optional_copy_assign_base&&) = default;
266
267 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __optional_copy_assign_base&
268 operator=(const __optional_copy_assign_base& __opt) {
269 this->__assign_from(__opt);
270 return *this;
271 }
272
273 _LIBCPP_HIDE_FROM_ABI __optional_copy_assign_base& operator=(__optional_copy_assign_base&&) = default;
274};
275
276template <class _Tp,
277 bool = (is_trivially_destructible_v<_Tp> && is_trivially_move_constructible_v<_Tp> &&
278 is_trivially_move_assignable_v<_Tp>)>
279struct __optional_move_assign_base : __optional_copy_assign_base<_Tp> {
280 using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base;
281};
282
283template <class _Tp>
284struct __optional_move_assign_base<_Tp, false> : __optional_copy_assign_base<_Tp> {
285 using value_type = _Tp;
286 using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base;
287
288 _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base() = default;
289 _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base(const __optional_move_assign_base& __opt) = default;
290 _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base(__optional_move_assign_base&&) = default;
291 _LIBCPP_HIDE_FROM_ABI __optional_move_assign_base& operator=(const __optional_move_assign_base&) = default;
292
293 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __optional_move_assign_base&
294 operator=(__optional_move_assign_base&& __opt) noexcept(
295 is_nothrow_move_assignable_v<value_type> && is_nothrow_move_constructible_v<value_type>) {
296 this->__assign_from(std::move(__opt));
297 return *this;
298 }
299};
300
301template <class _Tp>
302using __optional_sfinae_ctor_base_t _LIBCPP_NODEBUG =
303 __sfinae_ctor_base< is_copy_constructible<_Tp>::value, is_move_constructible<_Tp>::value >;
304
305template <class _Tp>
306using __optional_sfinae_assign_base_t _LIBCPP_NODEBUG =
307 __sfinae_assign_base< (is_copy_constructible_v<_Tp> && is_copy_assignable_v<_Tp>),
308 (is_move_constructible_v<_Tp> && is_move_assignable_v<_Tp>)>;
309
310template <class _Tp>
311class _LIBCPP_DECLSPEC_EMPTY_BASES optional
312 : public __optional_move_assign_base<_Tp>,
313 private __optional_sfinae_ctor_base_t<_Tp>,
314 private __optional_sfinae_assign_base_t<_Tp> {
315 using __base _LIBCPP_NODEBUG = __optional_move_assign_base<_Tp>;
316
317public:
318 using value_type = __libcpp_remove_reference_t<_Tp>;
319
320 using __trivially_relocatable _LIBCPP_NODEBUG = conditional_t<__is_trivially_relocatable_v<_Tp>, optional, void>;
321
322private:
323 static_assert(!is_same_v<remove_cv_t<_Tp>, in_place_t>, "instantiation of optional with in_place_t is ill-formed");
324 static_assert(!is_same_v<remove_cv_t<_Tp>, nullopt_t>, "instantiation of optional with nullopt_t is ill-formed");
325 static_assert(is_destructible_v<_Tp>, "instantiation of optional with a non-destructible type is ill-formed");
326 static_assert(!is_array_v<_Tp>, "instantiation of optional with an array type is ill-formed");
327
328 // LWG2756: conditionally explicit conversion from _Up
329 struct _CheckOptionalArgsConstructor {
330 template <class _Up>
331 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_implicit() {
332 return is_constructible_v<_Tp, _Up&&> && is_convertible_v<_Up&&, _Tp>;
333 }
334
335 template <class _Up>
336 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_explicit() {
337 return is_constructible_v<_Tp, _Up&&> && !is_convertible_v<_Up&&, _Tp>;
338 }
339 };
340 template <class _Up>
341 using _CheckOptionalArgsCtor _LIBCPP_NODEBUG =
342 _If< _IsNotSame<__remove_cvref_t<_Up>, in_place_t>::value && _IsNotSame<__remove_cvref_t<_Up>, optional>::value &&
343 (!is_same_v<remove_cv_t<_Tp>, bool> || !__is_std_optional_v<__remove_cvref_t<_Up>>),
344 _CheckOptionalArgsConstructor,
345 __check_tuple_constructor_fail >;
346 template <class _QualUp>
347 struct _CheckOptionalLikeConstructor {
348 template <class _Up, class _Opt = optional<_Up>>
349 using __check_constructible_from_opt _LIBCPP_NODEBUG =
350 _Or< is_constructible<_Tp, _Opt&>,
351 is_constructible<_Tp, _Opt const&>,
352 is_constructible<_Tp, _Opt&&>,
353 is_constructible<_Tp, _Opt const&&>,
354 is_convertible<_Opt&, _Tp>,
355 is_convertible<_Opt const&, _Tp>,
356 is_convertible<_Opt&&, _Tp>,
357 is_convertible<_Opt const&&, _Tp> >;
358 template <class _Up, class _Opt = optional<_Up>>
359 using __check_assignable_from_opt _LIBCPP_NODEBUG =
360 _Or< is_assignable<_Tp&, _Opt&>,
361 is_assignable<_Tp&, _Opt const&>,
362 is_assignable<_Tp&, _Opt&&>,
363 is_assignable<_Tp&, _Opt const&&> >;
364 template <class _Up, class _QUp = _QualUp>
365 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_implicit() {
366 return is_convertible<_QUp, _Tp>::value &&
367 (is_same_v<remove_cv_t<_Tp>, bool> || !__check_constructible_from_opt<_Up>::value);
368 }
369 template <class _Up, class _QUp = _QualUp>
370 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_explicit() {
371 return !is_convertible<_QUp, _Tp>::value &&
372 (is_same_v<remove_cv_t<_Tp>, bool> || !__check_constructible_from_opt<_Up>::value);
373 }
374 template <class _Up, class _QUp = _QualUp>
375 _LIBCPP_HIDE_FROM_ABI static constexpr bool __enable_assign() {
376 // Construction and assignability of _QUp to _Tp has already been
377 // checked.
378 return !__check_constructible_from_opt<_Up>::value && !__check_assignable_from_opt<_Up>::value;
379 }
380 };
381
382 template <class _Up, class _QualUp>
383 using _CheckOptionalLikeCtor _LIBCPP_NODEBUG =
384 _If< _And< _IsNotSame<_Up, _Tp>, is_constructible<_Tp, _QualUp> >::value,
385 _CheckOptionalLikeConstructor<_QualUp>,
386 __check_tuple_constructor_fail >;
387 template <class _Up, class _QualUp>
388 using _CheckOptionalLikeAssign _LIBCPP_NODEBUG =
389 _If< _And< _IsNotSame<_Up, _Tp>, is_constructible<_Tp, _QualUp>, is_assignable<_Tp&, _QualUp> >::value,
390 _CheckOptionalLikeConstructor<_QualUp>,
391 __check_tuple_constructor_fail >;
392
393public:
394 _LIBCPP_HIDE_FROM_ABI constexpr optional() noexcept {}
395 _LIBCPP_HIDE_FROM_ABI constexpr optional(const optional&) = default;
396 _LIBCPP_HIDE_FROM_ABI constexpr optional(optional&&) = default;
397 _LIBCPP_HIDE_FROM_ABI constexpr optional(nullopt_t) noexcept {}
398
399 template <class _InPlaceT,
400 class... _Args,
401 enable_if_t<_And<_IsSame<_InPlaceT, in_place_t>, is_constructible<_Tp, _Args...>>::value, int> = 0>
402 _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(_InPlaceT, _Args&&... __args)
403 : __base(in_place, std::forward<_Args>(__args)...) {}
404
405 template <class _Up, class... _Args, enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
406 _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
407 : __base(in_place, __il, std::forward<_Args>(__args)...) {}
408
409 template <class _Up = _Tp, enable_if_t<_CheckOptionalArgsCtor<_Up>::template __enable_implicit<_Up>(), int> = 0>
410 _LIBCPP_HIDE_FROM_ABI constexpr optional(_Up&& __v) noexcept(is_nothrow_constructible_v<_Tp, _Up>) // strengthened
411 : __base(in_place, std::forward<_Up>(__v)) {}
412
413 template <class _Up = remove_cv_t<_Tp>,
414 enable_if_t<_CheckOptionalArgsCtor<_Up>::template __enable_explicit<_Up>(), int> = 0>
415 _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(_Up&& __v) noexcept(
416 is_nothrow_constructible_v<_Tp, _Up>) // strengthened
417 : __base(in_place, std::forward<_Up>(__v)) {}
418
419 // LWG2756: conditionally explicit conversion from const optional<_Up>&
420 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_implicit<_Up>(), int> = 0>
421 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional(const optional<_Up>& __v) {
422 this->__construct_from(__v);
423 }
424
425 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up const&>::template __enable_explicit<_Up>(), int> = 0>
426 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit optional(const optional<_Up>& __v) {
427 this->__construct_from(__v);
428 }
429
430 // LWG2756: conditionally explicit conversion from optional<_Up>&&
431 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_implicit<_Up>(), int> = 0>
432 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional(optional<_Up>&& __v) {
433 this->__construct_from(std::move(__v));
434 }
435
436 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_explicit<_Up>(), int> = 0>
437 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit optional(optional<_Up>&& __v) {
438 this->__construct_from(std::move(__v));
439 }
440
441# if _LIBCPP_STD_VER >= 23
442 template <class _Tag,
443 class _Fp,
444 class... _Args,
445 enable_if_t<_IsSame<_Tag, __optional_construct_from_invoke_tag>::value, int> = 0>
446 _LIBCPP_HIDE_FROM_ABI constexpr explicit optional(_Tag, _Fp&& __f, _Args&&... __args)
447 : __base(__optional_construct_from_invoke_tag{}, std::forward<_Fp>(__f), std::forward<_Args>(__args)...) {}
448# endif
449
450 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(nullopt_t) noexcept {
451 reset();
452 return *this;
453 }
454
455 _LIBCPP_HIDE_FROM_ABI constexpr optional& operator=(const optional&) = default;
456 _LIBCPP_HIDE_FROM_ABI constexpr optional& operator=(optional&&) = default;
457
458 // LWG2756
459 template <class _Up = remove_cv_t<_Tp>,
460 enable_if_t<_And<_IsNotSame<__remove_cvref_t<_Up>, optional>,
461 _Or<_IsNotSame<__remove_cvref_t<_Up>, _Tp>, _Not<is_scalar<_Tp>>>,
462 is_constructible<_Tp, _Up>,
463 is_assignable<_Tp&, _Up>>::value,
464 int> = 0>
465 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(_Up&& __v) {
466 if (this->has_value())
467 this->__val_ = std::forward<_Up>(__v);
468 else
469 this->__construct(std::forward<_Up>(__v));
470 return *this;
471 }
472
473 // LWG2756
474 template <class _Up, enable_if_t<_CheckOptionalLikeAssign<_Up, _Up const&>::template __enable_assign<_Up>(), int> = 0>
475 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(const optional<_Up>& __v) {
476 this->__assign_from(__v);
477 return *this;
478 }
479
480 // LWG2756
481 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up&&>::template __enable_assign<_Up>(), int> = 0>
482 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 optional& operator=(optional<_Up>&& __v) {
483 this->__assign_from(std::move(__v));
484 return *this;
485 }
486
487 template <class... _Args, enable_if_t<is_constructible_v<_Tp, _Args...>, int> = 0>
488 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(_Args&&... __args) {
489 reset();
490 this->__construct(std::forward<_Args>(__args)...);
491 return this->__val_;
492 }
493
494 template <class _Up, class... _Args, enable_if_t<is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>, int> = 0>
495 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& emplace(initializer_list<_Up> __il, _Args&&... __args) {
496 reset();
497 this->__construct(__il, std::forward<_Args>(__args)...);
498 return this->__val_;
499 }
500
501 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void
502 swap(optional& __opt) noexcept((is_nothrow_move_constructible_v<_Tp> && is_nothrow_swappable_v<_Tp>)) {
503 using std::swap;
504 if (this->has_value() == __opt.has_value()) {
505 if (this->has_value())
506 swap(this->__val_, __opt.__val_);
507 } else {
508 if (this->has_value()) {
509 __opt.__construct(std::move(this->__val_));
510 this->reset();
511 } else {
512 this->__construct(std::move(__opt.__val_));
513 __opt.reset();
514 }
515 }
516 }
517
518 // [optional.observe]
519 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator*() const& noexcept {
520 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
521 return this->__val_;
522 }
523
524 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() & noexcept {
525 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
526 return this->__val_;
527 }
528
529 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp&& operator*() && noexcept {
530 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
531 return std::move(this->__val_);
532 }
533
534 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr const _Tp&& operator*() const&& noexcept {
535 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator* called on a disengaged value");
536 return std::move(this->__val_);
537 }
538 _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<_Tp const> operator->() const noexcept {
539 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator-> called on a disengaged value");
540 return std::addressof(this->__val_);
541 }
542
543 _LIBCPP_HIDE_FROM_ABI constexpr add_pointer_t<_Tp> operator->() noexcept {
544 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(this->has_value(), "optional operator-> called on a disengaged value");
545 return std::addressof(this->__val_);
546 }
547
548 _LIBCPP_HIDE_FROM_ABI constexpr explicit operator bool() const noexcept { return has_value(); }
549
550 using __base::has_value;
551 using __base::value;
552
553 template <class _Up = remove_cv_t<_Tp>>
554 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) const& {
555 static_assert(is_copy_constructible_v<_Tp>, "optional<T>::value_or: T must be copy constructible");
556 static_assert(is_convertible_v<_Up, _Tp>, "optional<T>::value_or: U must be convertible to T");
557 return this->has_value() ? this->__val_ : static_cast<_Tp>(std::forward<_Up>(__v));
558 }
559
560 template <class _Up = remove_cv_t<_Tp>>
561 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp value_or(_Up&& __v) && {
562 static_assert(is_move_constructible_v<_Tp>, "optional<T>::value_or: T must be move constructible");
563 static_assert(is_convertible_v<_Up, _Tp>, "optional<T>::value_or: U must be convertible to T");
564 return this->has_value() ? std::move(this->__val_) : static_cast<_Tp>(std::forward<_Up>(__v));
565 }
566
567# if _LIBCPP_STD_VER >= 23
568 template <class _Func>
569 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) & {
570 using _Up = invoke_result_t<_Func, _Tp&>;
571 static_assert(
572 __is_std_optional_v<remove_cvref_t<_Up>>, "Result of f(value()) must be a specialization of std::optional");
573 if (*this)
574 return std::invoke(std::forward<_Func>(__f), value());
575 return remove_cvref_t<_Up>();
576 }
577
578 template <class _Func>
579 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const& {
580 using _Up = invoke_result_t<_Func, const _Tp&>;
581 static_assert(
582 __is_std_optional_v<remove_cvref_t<_Up>>, "Result of f(value()) must be a specialization of std::optional");
583 if (*this)
584 return std::invoke(std::forward<_Func>(__f), value());
585 return remove_cvref_t<_Up>();
586 }
587
588 template <class _Func>
589 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) && {
590 using _Up = invoke_result_t<_Func, _Tp&&>;
591 static_assert(__is_std_optional_v<remove_cvref_t<_Up>>,
592 "Result of f(std::move(value())) must be a specialization of std::optional");
593 if (*this)
594 return std::invoke(std::forward<_Func>(__f), std::move(value()));
595 return remove_cvref_t<_Up>();
596 }
597
598 template <class _Func>
599 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto and_then(_Func&& __f) const&& {
600 using _Up = invoke_result_t<_Func, const _Tp&&>;
601 static_assert(__is_std_optional_v<remove_cvref_t<_Up>>,
602 "Result of f(std::move(value())) must be a specialization of std::optional");
603 if (*this)
604 return std::invoke(std::forward<_Func>(__f), std::move(value()));
605 return remove_cvref_t<_Up>();
606 }
607
608 template <class _Func>
609 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) & {
610 using _Up = remove_cv_t<invoke_result_t<_Func, _Tp&>>;
611 static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array");
612 static_assert(!is_same_v<_Up, in_place_t>, "Result of f(value()) should not be std::in_place_t");
613 static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(value()) should not be std::nullopt_t");
614 static_assert(
615 __is_valid_optional_contained_type<_Up>, "Result of f(value()) should be a valid contained type for optional");
616 if (*this)
617 return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), value());
618 return optional<_Up>();
619 }
620
621 template <class _Func>
622 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const& {
623 using _Up = remove_cv_t<invoke_result_t<_Func, const _Tp&>>;
624 static_assert(!is_array_v<_Up>, "Result of f(value()) should not be an Array");
625 static_assert(!is_same_v<_Up, in_place_t>, "Result of f(value()) should not be std::in_place_t");
626 static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(value()) should not be std::nullopt_t");
627 static_assert(
628 __is_valid_optional_contained_type<_Up>, "Result of f(value()) should be a valid contained type for optional");
629 if (*this)
630 return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), value());
631 return optional<_Up>();
632 }
633
634 template <class _Func>
635 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) && {
636 using _Up = remove_cv_t<invoke_result_t<_Func, _Tp&&>>;
637 static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array");
638 static_assert(!is_same_v<_Up, in_place_t>, "Result of f(std::move(value())) should not be std::in_place_t");
639 static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(std::move(value())) should not be std::nullopt_t");
640 static_assert(
641 __is_valid_optional_contained_type<_Up>, "Result of f(value()) should be a valid contained type for optional");
642 if (*this)
643 return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), std::move(value()));
644 return optional<_Up>();
645 }
646
647 template <class _Func>
648 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto transform(_Func&& __f) const&& {
649 using _Up = remove_cv_t<invoke_result_t<_Func, const _Tp&&>>;
650 static_assert(!is_array_v<_Up>, "Result of f(std::move(value())) should not be an Array");
651 static_assert(!is_same_v<_Up, in_place_t>, "Result of f(std::move(value())) should not be std::in_place_t");
652 static_assert(!is_same_v<_Up, nullopt_t>, "Result of f(std::move(value())) should not be std::nullopt_t");
653 static_assert(
654 __is_valid_optional_contained_type<_Up>, "Result of f(value()) should be a valid contained type for optional");
655 if (*this)
656 return optional<_Up>(__optional_construct_from_invoke_tag{}, std::forward<_Func>(__f), std::move(value()));
657 return optional<_Up>();
658 }
659
660 template <invocable _Func>
661 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) const&
662 requires is_copy_constructible_v<_Tp>
663 {
664 static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>,
665 "Result of f() should be the same type as this optional");
666 if (*this)
667 return *this;
668 return std::forward<_Func>(__f)();
669 }
670
671 template <invocable _Func>
672 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional or_else(_Func&& __f) &&
673 requires is_move_constructible_v<_Tp>
674 {
675 static_assert(is_same_v<remove_cvref_t<invoke_result_t<_Func>>, optional>,
676 "Result of f() should be the same type as this optional");
677 if (*this)
678 return std::move(*this);
679 return std::forward<_Func>(__f)();
680 }
681# endif // _LIBCPP_STD_VER >= 23
682
683 using __base::reset;
684
685# if _LIBCPP_STD_VER >= 26 && _LIBCPP_HAS_EXPERIMENTAL_OPTIONAL_ITERATOR
686
687private:
688 using __pointer _LIBCPP_NODEBUG = add_pointer_t<_Tp>;
689 using __const_pointer _LIBCPP_NODEBUG = add_pointer_t<const _Tp>;
690
691 template <class _Up = _Tp, bool = std::__range_fits_in_alignment(alignment: alignof(_Up), num_elems: 1)>
692 struct __iterator {
693# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL
694 using __type _LIBCPP_NODEBUG = std::__bounded_iter<__pointer>;
695 using __const_type _LIBCPP_NODEBUG = std::__bounded_iter<__const_pointer>;
696# else
697 using __type _LIBCPP_NODEBUG = std::__static_packed_bounded_iterator<__pointer, 1>;
698 using __const_type _LIBCPP_NODEBUG = std::__static_packed_bounded_iterator<__const_pointer, 1>;
699# endif
700
701 template <class _PtrType>
702 static auto __make(_PtrType __p, [[__maybe_unused__]] bool __engaged) noexcept {
703 // It is valid to pass in the current size of the optional rather than a fixed capacity because
704 // engaging/disengaging the member invalidates iterators.
705# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL
706 return std::__make_bounded_iter<_PtrType>(__p, __p, __p + (__engaged ? 1 : 0));
707# else
708 return std::__make_static_packed_bounded_iter<_PtrType, 1>(__p, 0);
709# endif
710 }
711 };
712
713 template <class _Up>
714 struct __iterator<_Up, false> {
715# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL
716 using __type _LIBCPP_NODEBUG = std::__bounded_iter<__pointer>;
717 using __const_type _LIBCPP_NODEBUG = std::__bounded_iter<__const_pointer>;
718# else
719 using __type _LIBCPP_NODEBUG = std::__capacity_aware_iterator<__pointer, 1>;
720 using __const_type _LIBCPP_NODEBUG = std::__capacity_aware_iterator<__const_pointer, 1>;
721# endif
722
723 template <class _PtrType>
724 static auto __make(_PtrType __p, [[__maybe_unused__]] bool __engaged) noexcept {
725# ifdef _LIBCPP_ABI_BOUNDED_ITERATORS_IN_OPTIONAL
726 return std::__make_bounded_iter<_PtrType>(__p, __p, __p + (__engaged ? 1 : 0));
727# else
728 return std::__make_capacity_aware_iterator<_PtrType, 1>(__p);
729# endif
730 }
731 };
732
733 using __iter _LIBCPP_NODEBUG = __iterator<>;
734
735public:
736 using iterator = __iter::__type;
737 using const_iterator = __iter::__const_type;
738
739 // [optional.iterators], iterator support
740 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI iterator begin() noexcept {
741 auto* __ptr = std::addressof(this->__val_);
742
743 return __iter::__make(__ptr, this->has_value());
744 }
745
746 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI const_iterator begin() const noexcept {
747 auto* __ptr = std::addressof(this->__val_);
748
749 return __iter::__make(__ptr, this->has_value());
750 }
751
752 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI iterator end() noexcept { return begin() + (this->has_value() ? 1 : 0); }
753 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI const_iterator end() const noexcept {
754 return begin() + (this->has_value() ? 1 : 0);
755 }
756
757# endif // _LIBCPP_STD_VER >= 26 && _LIBCPP_HAS_EXPERIMENTAL_OPTIONAL_ITERATOR
758};
759
760_LIBCPP_END_NAMESPACE_STD
761
762#endif // _LIBCPP_STD_VER >= 17
763
764_LIBCPP_POP_MACROS
765
766#endif // _LIBCPP___OPTIONAL_OPTIONAL_H
767