blob: b251748fbf37f597f449edb14f7caf136f14a0f1 [file] [log] [blame]
Howard Hinnant01afa5c2013-09-02 20:30:371// -*- C++ -*-
2//===-------------------------- optional ----------------------------------===//
3//
4// The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
Eric Fiselierb9231a22016-09-07 01:56:0711#ifndef _LIBCPP_EXPERIMENTAL_OPTIONAL
12#define _LIBCPP_EXPERIMENTAL_OPTIONAL
Howard Hinnant01afa5c2013-09-02 20:30:3713
14/*
15 optional synopsis
16
17// C++1y
18
Marshall Clow4ad67e02014-12-09 14:49:1719namespace std { namespace experimental { inline namespace fundamentals_v1 {
Howard Hinnant01afa5c2013-09-02 20:30:3720
Marshall Clow4ad67e02014-12-09 14:49:1721 // 5.3, optional for object types
22 template <class T> class optional;
Howard Hinnant01afa5c2013-09-02 20:30:3723
Marshall Clow4ad67e02014-12-09 14:49:1724 // 5.4, In-place construction
25 struct in_place_t{};
26 constexpr in_place_t in_place{};
Howard Hinnant01afa5c2013-09-02 20:30:3727
Marshall Clow4ad67e02014-12-09 14:49:1728 // 5.5, No-value state indicator
29 struct nullopt_t{see below};
30 constexpr nullopt_t nullopt(unspecified);
Howard Hinnant01afa5c2013-09-02 20:30:3731
Marshall Clow4ad67e02014-12-09 14:49:1732 // 5.6, Class bad_optional_access
33 class bad_optional_access;
Howard Hinnant01afa5c2013-09-02 20:30:3734
Marshall Clow4ad67e02014-12-09 14:49:1735 // 5.7, Relational operators
36 template <class T>
37 constexpr bool operator==(const optional<T>&, const optional<T>&);
38 template <class T>
39 constexpr bool operator!=(const optional<T>&, const optional<T>&);
40 template <class T>
41 constexpr bool operator<(const optional<T>&, const optional<T>&);
42 template <class T>
43 constexpr bool operator>(const optional<T>&, const optional<T>&);
44 template <class T>
45 constexpr bool operator<=(const optional<T>&, const optional<T>&);
46 template <class T>
47 constexpr bool operator>=(const optional<T>&, const optional<T>&);
Howard Hinnant01afa5c2013-09-02 20:30:3748
Marshall Clow4ad67e02014-12-09 14:49:1749 // 5.8, Comparison with nullopt
50 template <class T> constexpr bool operator==(const optional<T>&, nullopt_t) noexcept;
51 template <class T> constexpr bool operator==(nullopt_t, const optional<T>&) noexcept;
52 template <class T> constexpr bool operator!=(const optional<T>&, nullopt_t) noexcept;
53 template <class T> constexpr bool operator!=(nullopt_t, const optional<T>&) noexcept;
54 template <class T> constexpr bool operator<(const optional<T>&, nullopt_t) noexcept;
55 template <class T> constexpr bool operator<(nullopt_t, const optional<T>&) noexcept;
56 template <class T> constexpr bool operator<=(const optional<T>&, nullopt_t) noexcept;
57 template <class T> constexpr bool operator<=(nullopt_t, const optional<T>&) noexcept;
58 template <class T> constexpr bool operator>(const optional<T>&, nullopt_t) noexcept;
59 template <class T> constexpr bool operator>(nullopt_t, const optional<T>&) noexcept;
60 template <class T> constexpr bool operator>=(const optional<T>&, nullopt_t) noexcept;
61 template <class T> constexpr bool operator>=(nullopt_t, const optional<T>&) noexcept;
Howard Hinnant01afa5c2013-09-02 20:30:3762
Marshall Clow4ad67e02014-12-09 14:49:1763 // 5.9, Comparison with T
64 template <class T> constexpr bool operator==(const optional<T>&, const T&);
65 template <class T> constexpr bool operator==(const T&, const optional<T>&);
66 template <class T> constexpr bool operator!=(const optional<T>&, const T&);
67 template <class T> constexpr bool operator!=(const T&, const optional<T>&);
68 template <class T> constexpr bool operator<(const optional<T>&, const T&);
69 template <class T> constexpr bool operator<(const T&, const optional<T>&);
70 template <class T> constexpr bool operator<=(const optional<T>&, const T&);
71 template <class T> constexpr bool operator<=(const T&, const optional<T>&);
72 template <class T> constexpr bool operator>(const optional<T>&, const T&);
73 template <class T> constexpr bool operator>(const T&, const optional<T>&);
74 template <class T> constexpr bool operator>=(const optional<T>&, const T&);
75 template <class T> constexpr bool operator>=(const T&, const optional<T>&);
Howard Hinnant01afa5c2013-09-02 20:30:3776
Marshall Clow4ad67e02014-12-09 14:49:1777 // 5.10, Specialized algorithms
78 template <class T> void swap(optional<T>&, optional<T>&) noexcept(see below);
79 template <class T> constexpr optional<see below> make_optional(T&&);
Howard Hinnant01afa5c2013-09-02 20:30:3780
Marshall Clow4ad67e02014-12-09 14:49:1781template <class T>
82class optional
83{
84public:
85 typedef T value_type;
Howard Hinnant01afa5c2013-09-02 20:30:3786
Marshall Clow4ad67e02014-12-09 14:49:1787 // 5.3.1, Constructors
88 constexpr optional() noexcept;
89 constexpr optional(nullopt_t) noexcept;
90 optional(const optional&);
91 optional(optional&&) noexcept(see below);
92 constexpr optional(const T&);
93 constexpr optional(T&&);
94 template <class... Args> constexpr explicit optional(in_place_t, Args&&...);
95 template <class U, class... Args>
96constexpr explicit optional(in_place_t, initializer_list<U>, Args&&...);
Howard Hinnant01afa5c2013-09-02 20:30:3797
Marshall Clow4ad67e02014-12-09 14:49:1798 // 5.3.2, Destructor
99 ~optional();
Howard Hinnant01afa5c2013-09-02 20:30:37100
Marshall Clow4ad67e02014-12-09 14:49:17101 // 5.3.3, Assignment
102 optional& operator=(nullopt_t) noexcept;
103 optional& operator=(const optional&);
104 optional& operator=(optional&&) noexcept(see below);
105 template <class U> optional& operator=(U&&);
106 template <class... Args> void emplace(Args&&...);
107 template <class U, class... Args>
108void emplace(initializer_list<U>, Args&&...);
Howard Hinnant01afa5c2013-09-02 20:30:37109
Marshall Clow4ad67e02014-12-09 14:49:17110 // 5.3.4, Swap
111 void swap(optional&) noexcept(see below);
Howard Hinnant01afa5c2013-09-02 20:30:37112
Marshall Clow4ad67e02014-12-09 14:49:17113 // 5.3.5, Observers
114 constexpr T const* operator ->() const;
115 constexpr T* operator ->();
116 constexpr T const& operator *() const &;
117 constexpr T& operator *() &;
118 constexpr T&& operator *() &&;
119 constexpr const T&& operator *() const &&;
120 constexpr explicit operator bool() const noexcept;
121 constexpr T const& value() const &;
122 constexpr T& value() &;
123 constexpr T&& value() &&;
124 constexpr const T&& value() const &&;
125 template <class U> constexpr T value_or(U&&) const &;
126 template <class U> constexpr T value_or(U&&) &&;
Howard Hinnant01afa5c2013-09-02 20:30:37127
Marshall Clow4ad67e02014-12-09 14:49:17128private:
129 T* val; // exposition only
130};
Howard Hinnant01afa5c2013-09-02 20:30:37131
Marshall Clow4ad67e02014-12-09 14:49:17132 } // namespace fundamentals_v1
133 } // namespace experimental
134
135 // 5.11, Hash support
136 template <class T> struct hash;
137 template <class T> struct hash<experimental::optional<T>>;
138
139} // namespace std
Howard Hinnant01afa5c2013-09-02 20:30:37140
141*/
142
Marshall Clow4ad67e02014-12-09 14:49:17143#include <experimental/__config>
Howard Hinnant01afa5c2013-09-02 20:30:37144#include <functional>
145#include <stdexcept>
Eric Fiselier018a3d52017-05-31 22:07:49146#if _LIBCPP_STD_VER > 11
147#include <initializer_list>
148#include <type_traits>
149#include <new>
150#include <__functional_base>
151#include <__debug>
152#endif
153
154#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
155#pragma GCC system_header
156#endif
157
158_LIBCPP_PUSH_MACROS
159#include <__undef_macros>
160
Howard Hinnant01afa5c2013-09-02 20:30:37161
Marshall Clow4ad67e02014-12-09 14:49:17162_LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL
Mehdi Amini907c1192017-05-04 17:08:54163class _LIBCPP_EXCEPTION_ABI _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS bad_optional_access
Marshall Clow4ad67e02014-12-09 14:49:17164 : public std::logic_error
Howard Hinnant01afa5c2013-09-02 20:30:37165{
166public:
Marshall Clow4ad67e02014-12-09 14:49:17167bad_optional_access() : std::logic_error("Bad optional Access") {}
168
169// Get the key function ~bad_optional_access() into the dylib
Marshall Clow0becc202017-02-05 20:52:32170 virtual ~bad_optional_access() _NOEXCEPT;
Howard Hinnant01afa5c2013-09-02 20:30:37171};
172
Marshall Clow4ad67e02014-12-09 14:49:17173_LIBCPP_END_NAMESPACE_EXPERIMENTAL
174
Howard Hinnant01afa5c2013-09-02 20:30:37175
176#if _LIBCPP_STD_VER > 11
177
Marshall Clow4ad67e02014-12-09 14:49:17178_LIBCPP_BEGIN_NAMESPACE_LFTS
Howard Hinnant01afa5c2013-09-02 20:30:37179
180struct in_place_t {};
181constexpr in_place_t in_place{};
182
183struct nullopt_t
184{
185 explicit constexpr nullopt_t(int) noexcept {}
186};
187
188constexpr nullopt_t nullopt{0};
189
190template <class _Tp, bool = is_trivially_destructible<_Tp>::value>
191class __optional_storage
192{
193protected:
194 typedef _Tp value_type;
195 union
196 {
197 char __null_state_;
198 value_type __val_;
199 };
200 bool __engaged_ = false;
201
202 _LIBCPP_INLINE_VISIBILITY
203 ~__optional_storage()
204 {
205 if (__engaged_)
206 __val_.~value_type();
207 }
208
209 _LIBCPP_INLINE_VISIBILITY
210 constexpr __optional_storage() noexcept
211 : __null_state_('\0') {}
212
213 _LIBCPP_INLINE_VISIBILITY
214 __optional_storage(const __optional_storage& __x)
215 : __engaged_(__x.__engaged_)
216 {
217 if (__engaged_)
Eric Fiselierb9231a22016-09-07 01:56:07218 ::new((void*)_VSTD::addressof(__val_)) value_type(__x.__val_);
Howard Hinnant01afa5c2013-09-02 20:30:37219 }
220
221 _LIBCPP_INLINE_VISIBILITY
222 __optional_storage(__optional_storage&& __x)
223 noexcept(is_nothrow_move_constructible<value_type>::value)
224 : __engaged_(__x.__engaged_)
225 {
226 if (__engaged_)
Eric Fiselierb9231a22016-09-07 01:56:07227 ::new((void*)_VSTD::addressof(__val_)) value_type(_VSTD::move(__x.__val_));
Howard Hinnant01afa5c2013-09-02 20:30:37228 }
229
230 _LIBCPP_INLINE_VISIBILITY
231 constexpr __optional_storage(const value_type& __v)
232 : __val_(__v),
233 __engaged_(true) {}
234
235 _LIBCPP_INLINE_VISIBILITY
236 constexpr __optional_storage(value_type&& __v)
237 : __val_(_VSTD::move(__v)),
238 __engaged_(true) {}
239
240 template <class... _Args>
241 _LIBCPP_INLINE_VISIBILITY
242 constexpr
243 explicit __optional_storage(in_place_t, _Args&&... __args)
244 : __val_(_VSTD::forward<_Args>(__args)...),
245 __engaged_(true) {}
246};
247
248template <class _Tp>
249class __optional_storage<_Tp, true>
250{
251protected:
252 typedef _Tp value_type;
253 union
254 {
255 char __null_state_;
256 value_type __val_;
257 };
258 bool __engaged_ = false;
259
260 _LIBCPP_INLINE_VISIBILITY
261 constexpr __optional_storage() noexcept
262 : __null_state_('\0') {}
263
264 _LIBCPP_INLINE_VISIBILITY
265 __optional_storage(const __optional_storage& __x)
266 : __engaged_(__x.__engaged_)
267 {
268 if (__engaged_)
Eric Fiselierb9231a22016-09-07 01:56:07269 ::new((void*)_VSTD::addressof(__val_)) value_type(__x.__val_);
Howard Hinnant01afa5c2013-09-02 20:30:37270 }
271
272 _LIBCPP_INLINE_VISIBILITY
273 __optional_storage(__optional_storage&& __x)
274 noexcept(is_nothrow_move_constructible<value_type>::value)
275 : __engaged_(__x.__engaged_)
276 {
277 if (__engaged_)
Eric Fiselierb9231a22016-09-07 01:56:07278 ::new((void*)_VSTD::addressof(__val_)) value_type(_VSTD::move(__x.__val_));
Howard Hinnant01afa5c2013-09-02 20:30:37279 }
280
281 _LIBCPP_INLINE_VISIBILITY
282 constexpr __optional_storage(const value_type& __v)
283 : __val_(__v),
284 __engaged_(true) {}
285
286 _LIBCPP_INLINE_VISIBILITY
287 constexpr __optional_storage(value_type&& __v)
288 : __val_(_VSTD::move(__v)),
289 __engaged_(true) {}
290
291 template <class... _Args>
292 _LIBCPP_INLINE_VISIBILITY
293 constexpr
294 explicit __optional_storage(in_place_t, _Args&&... __args)
295 : __val_(_VSTD::forward<_Args>(__args)...),
296 __engaged_(true) {}
297};
298
299template <class _Tp>
300class optional
301 : private __optional_storage<_Tp>
302{
303 typedef __optional_storage<_Tp> __base;
304public:
305 typedef _Tp value_type;
306
307 static_assert(!is_reference<value_type>::value,
308 "Instantiation of optional with a reference type is ill-formed.");
309 static_assert(!is_same<typename remove_cv<value_type>::type, in_place_t>::value,
310 "Instantiation of optional with a in_place_t type is ill-formed.");
311 static_assert(!is_same<typename remove_cv<value_type>::type, nullopt_t>::value,
312 "Instantiation of optional with a nullopt_t type is ill-formed.");
313 static_assert(is_object<value_type>::value,
314 "Instantiation of optional with a non-object type is undefined behavior.");
315 static_assert(is_nothrow_destructible<value_type>::value,
316 "Instantiation of optional with an object type that is not noexcept destructible is undefined behavior.");
317
318 _LIBCPP_INLINE_VISIBILITY constexpr optional() noexcept {}
319 _LIBCPP_INLINE_VISIBILITY optional(const optional&) = default;
320 _LIBCPP_INLINE_VISIBILITY optional(optional&&) = default;
321 _LIBCPP_INLINE_VISIBILITY ~optional() = default;
322 _LIBCPP_INLINE_VISIBILITY constexpr optional(nullopt_t) noexcept {}
323 _LIBCPP_INLINE_VISIBILITY constexpr optional(const value_type& __v)
324 : __base(__v) {}
325 _LIBCPP_INLINE_VISIBILITY constexpr optional(value_type&& __v)
326 : __base(_VSTD::move(__v)) {}
327
328 template <class... _Args,
329 class = typename enable_if
330 <
331 is_constructible<value_type, _Args...>::value
332 >::type
333 >
334 _LIBCPP_INLINE_VISIBILITY
335 constexpr
336 explicit optional(in_place_t, _Args&&... __args)
337 : __base(in_place, _VSTD::forward<_Args>(__args)...) {}
338
339 template <class _Up, class... _Args,
340 class = typename enable_if
341 <
342 is_constructible<value_type, initializer_list<_Up>&, _Args...>::value
343 >::type
344 >
345 _LIBCPP_INLINE_VISIBILITY
346 constexpr
347 explicit optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
348 : __base(in_place, __il, _VSTD::forward<_Args>(__args)...) {}
349
350 _LIBCPP_INLINE_VISIBILITY
351 optional& operator=(nullopt_t) noexcept
352 {
353 if (this->__engaged_)
354 {
355 this->__val_.~value_type();
356 this->__engaged_ = false;
357 }
358 return *this;
359 }
360
361 _LIBCPP_INLINE_VISIBILITY
362 optional&
363 operator=(const optional& __opt)
364 {
365 if (this->__engaged_ == __opt.__engaged_)
366 {
367 if (this->__engaged_)
368 this->__val_ = __opt.__val_;
369 }
370 else
371 {
372 if (this->__engaged_)
373 this->__val_.~value_type();
374 else
Eric Fiselierb9231a22016-09-07 01:56:07375 ::new((void*)_VSTD::addressof(this->__val_)) value_type(__opt.__val_);
Howard Hinnant01afa5c2013-09-02 20:30:37376 this->__engaged_ = __opt.__engaged_;
377 }
378 return *this;
379 }
380
381 _LIBCPP_INLINE_VISIBILITY
382 optional&
383 operator=(optional&& __opt)
384 noexcept(is_nothrow_move_assignable<value_type>::value &&
385 is_nothrow_move_constructible<value_type>::value)
386 {
387 if (this->__engaged_ == __opt.__engaged_)
388 {
389 if (this->__engaged_)
390 this->__val_ = _VSTD::move(__opt.__val_);
391 }
392 else
393 {
394 if (this->__engaged_)
395 this->__val_.~value_type();
396 else
Eric Fiselierb9231a22016-09-07 01:56:07397 ::new((void*)_VSTD::addressof(this->__val_))
398 value_type(_VSTD::move(__opt.__val_));
Howard Hinnant01afa5c2013-09-02 20:30:37399 this->__engaged_ = __opt.__engaged_;
400 }
401 return *this;
402 }
403
404 template <class _Up,
405 class = typename enable_if
406 <
407 is_same<typename remove_reference<_Up>::type, value_type>::value &&
408 is_constructible<value_type, _Up>::value &&
409 is_assignable<value_type&, _Up>::value
410 >::type
411 >
412 _LIBCPP_INLINE_VISIBILITY
413 optional&
414 operator=(_Up&& __v)
415 {
416 if (this->__engaged_)
417 this->__val_ = _VSTD::forward<_Up>(__v);
418 else
419 {
Eric Fiselierb9231a22016-09-07 01:56:07420 ::new((void*)_VSTD::addressof(this->__val_)) value_type(_VSTD::forward<_Up>(__v));
Howard Hinnant01afa5c2013-09-02 20:30:37421 this->__engaged_ = true;
422 }
423 return *this;
424 }
425
426 template <class... _Args,
427 class = typename enable_if
428 <
429 is_constructible<value_type, _Args...>::value
430 >::type
431 >
432 _LIBCPP_INLINE_VISIBILITY
433 void
434 emplace(_Args&&... __args)
435 {
436 *this = nullopt;
Eric Fiselierb9231a22016-09-07 01:56:07437 ::new((void*)_VSTD::addressof(this->__val_))
438 value_type(_VSTD::forward<_Args>(__args)...);
Howard Hinnant01afa5c2013-09-02 20:30:37439 this->__engaged_ = true;
440 }
441
442 template <class _Up, class... _Args,
443 class = typename enable_if
444 <
445 is_constructible<value_type, initializer_list<_Up>&, _Args...>::value
446 >::type
447 >
448 _LIBCPP_INLINE_VISIBILITY
449 void
450 emplace(initializer_list<_Up> __il, _Args&&... __args)
451 {
452 *this = nullopt;
Eric Fiselierb9231a22016-09-07 01:56:07453 ::new((void*)_VSTD::addressof(this->__val_))
454 value_type(__il, _VSTD::forward<_Args>(__args)...);
Howard Hinnant01afa5c2013-09-02 20:30:37455 this->__engaged_ = true;
456 }
457
458 _LIBCPP_INLINE_VISIBILITY
459 void
460 swap(optional& __opt)
461 noexcept(is_nothrow_move_constructible<value_type>::value &&
462 __is_nothrow_swappable<value_type>::value)
463 {
464 using _VSTD::swap;
465 if (this->__engaged_ == __opt.__engaged_)
466 {
467 if (this->__engaged_)
468 swap(this->__val_, __opt.__val_);
469 }
470 else
471 {
472 if (this->__engaged_)
473 {
Eric Fiselierb9231a22016-09-07 01:56:07474 ::new((void*)_VSTD::addressof(__opt.__val_))
475 value_type(_VSTD::move(this->__val_));
Howard Hinnant01afa5c2013-09-02 20:30:37476 this->__val_.~value_type();
477 }
478 else
479 {
Eric Fiselierb9231a22016-09-07 01:56:07480 ::new((void*)_VSTD::addressof(this->__val_))
481 value_type(_VSTD::move(__opt.__val_));
Howard Hinnant01afa5c2013-09-02 20:30:37482 __opt.__val_.~value_type();
483 }
484 swap(this->__engaged_, __opt.__engaged_);
485 }
486 }
487
488 _LIBCPP_INLINE_VISIBILITY
489 constexpr
490 value_type const*
491 operator->() const
492 {
493 _LIBCPP_ASSERT(this->__engaged_, "optional operator-> called for disengaged value");
Eric Fiselier46c0fcb2016-10-16 03:21:35494#ifndef _LIBCPP_HAS_NO_BUILTIN_ADDRESSOF
Eric Fiselier10060352016-10-16 03:49:18495 return __builtin_addressof(this->__val_);
Eric Fiselier46c0fcb2016-10-16 03:21:35496#else
Howard Hinnant01afa5c2013-09-02 20:30:37497 return __operator_arrow(__has_operator_addressof<value_type>{});
Eric Fiselier46c0fcb2016-10-16 03:21:35498#endif
Howard Hinnant01afa5c2013-09-02 20:30:37499 }
500
501 _LIBCPP_INLINE_VISIBILITY
502 value_type*
503 operator->()
504 {
505 _LIBCPP_ASSERT(this->__engaged_, "optional operator-> called for disengaged value");
506 return _VSTD::addressof(this->__val_);
507 }
508
509 _LIBCPP_INLINE_VISIBILITY
510 constexpr
511 const value_type&
512 operator*() const
513 {
514 _LIBCPP_ASSERT(this->__engaged_, "optional operator* called for disengaged value");
515 return this->__val_;
516 }
517
518 _LIBCPP_INLINE_VISIBILITY
519 value_type&
520 operator*()
521 {
522 _LIBCPP_ASSERT(this->__engaged_, "optional operator* called for disengaged value");
523 return this->__val_;
524 }
525
526 _LIBCPP_INLINE_VISIBILITY
527 constexpr explicit operator bool() const noexcept {return this->__engaged_;}
528
Marshall Clow14c09a22016-08-25 15:09:01529_LIBCPP_NORETURN _LIBCPP_INLINE_VISIBILITY
Mehdi Amini907c1192017-05-04 17:08:54530#ifndef _LIBCPP_NO_EXCEPTIONS
531_LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS
532#endif
Marshall Clow14c09a22016-08-25 15:09:01533constexpr void __throw_bad_optional_access() const
534{
535#ifndef _LIBCPP_NO_EXCEPTIONS
536 throw bad_optional_access();
537#else
538 _VSTD::abort();
539#endif
540}
541
Mehdi Amini907c1192017-05-04 17:08:54542 _LIBCPP_INLINE_VISIBILITY _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS
Howard Hinnant01afa5c2013-09-02 20:30:37543 constexpr value_type const& value() const
544 {
545 if (!this->__engaged_)
Marshall Clow14c09a22016-08-25 15:09:01546 __throw_bad_optional_access();
Howard Hinnant01afa5c2013-09-02 20:30:37547 return this->__val_;
548 }
549
Mehdi Amini907c1192017-05-04 17:08:54550 _LIBCPP_INLINE_VISIBILITY _LIBCPP_AVAILABILITY_BAD_OPTIONAL_ACCESS
Howard Hinnant01afa5c2013-09-02 20:30:37551 value_type& value()
552 {
553 if (!this->__engaged_)
Marshall Clow14c09a22016-08-25 15:09:01554 __throw_bad_optional_access();
Howard Hinnant01afa5c2013-09-02 20:30:37555 return this->__val_;
556 }
557
558 template <class _Up>
559 _LIBCPP_INLINE_VISIBILITY
560 constexpr value_type value_or(_Up&& __v) const&
561 {
562 static_assert(is_copy_constructible<value_type>::value,
563 "optional<T>::value_or: T must be copy constructible");
564 static_assert(is_convertible<_Up, value_type>::value,
565 "optional<T>::value_or: U must be convertible to T");
566 return this->__engaged_ ? this->__val_ :
567 static_cast<value_type>(_VSTD::forward<_Up>(__v));
568 }
569
570 template <class _Up>
571 _LIBCPP_INLINE_VISIBILITY
572 value_type value_or(_Up&& __v) &&
573 {
574 static_assert(is_move_constructible<value_type>::value,
575 "optional<T>::value_or: T must be move constructible");
576 static_assert(is_convertible<_Up, value_type>::value,
577 "optional<T>::value_or: U must be convertible to T");
578 return this->__engaged_ ? _VSTD::move(this->__val_) :
579 static_cast<value_type>(_VSTD::forward<_Up>(__v));
580 }
581
582private:
583 _LIBCPP_INLINE_VISIBILITY
584 value_type const*
585 __operator_arrow(true_type) const
586 {
587 return _VSTD::addressof(this->__val_);
588 }
589
590 _LIBCPP_INLINE_VISIBILITY
591 constexpr
592 value_type const*
593 __operator_arrow(false_type) const
594 {
595 return &this->__val_;
596 }
597};
598
Marshall Clow4ad67e02014-12-09 14:49:17599// Comparisons between optionals
Howard Hinnant01afa5c2013-09-02 20:30:37600template <class _Tp>
601inline _LIBCPP_INLINE_VISIBILITY
602constexpr
603bool
604operator==(const optional<_Tp>& __x, const optional<_Tp>& __y)
605{
606 if (static_cast<bool>(__x) != static_cast<bool>(__y))
607 return false;
608 if (!static_cast<bool>(__x))
609 return true;
610 return *__x == *__y;
611}
612
613template <class _Tp>
614inline _LIBCPP_INLINE_VISIBILITY
615constexpr
616bool
Marshall Clow4ad67e02014-12-09 14:49:17617operator!=(const optional<_Tp>& __x, const optional<_Tp>& __y)
618{
619 return !(__x == __y);
620}
621
622template <class _Tp>
623inline _LIBCPP_INLINE_VISIBILITY
624constexpr
625bool
Howard Hinnant01afa5c2013-09-02 20:30:37626operator<(const optional<_Tp>& __x, const optional<_Tp>& __y)
627{
628 if (!static_cast<bool>(__y))
629 return false;
630 if (!static_cast<bool>(__x))
631 return true;
Marshall Clow4ad67e02014-12-09 14:49:17632 return *__x < *__y;
Howard Hinnant01afa5c2013-09-02 20:30:37633}
634
635template <class _Tp>
636inline _LIBCPP_INLINE_VISIBILITY
637constexpr
638bool
Marshall Clow4ad67e02014-12-09 14:49:17639operator>(const optional<_Tp>& __x, const optional<_Tp>& __y)
640{
641 return __y < __x;
642}
643
644template <class _Tp>
645inline _LIBCPP_INLINE_VISIBILITY
646constexpr
647bool
648operator<=(const optional<_Tp>& __x, const optional<_Tp>& __y)
649{
650 return !(__y < __x);
651}
652
653template <class _Tp>
654inline _LIBCPP_INLINE_VISIBILITY
655constexpr
656bool
657operator>=(const optional<_Tp>& __x, const optional<_Tp>& __y)
658{
659 return !(__x < __y);
660}
661
662
663// Comparisons with nullopt
664template <class _Tp>
665inline _LIBCPP_INLINE_VISIBILITY
666constexpr
667bool
Howard Hinnant01afa5c2013-09-02 20:30:37668operator==(const optional<_Tp>& __x, nullopt_t) noexcept
669{
670 return !static_cast<bool>(__x);
671}
672
673template <class _Tp>
674inline _LIBCPP_INLINE_VISIBILITY
675constexpr
676bool
677operator==(nullopt_t, const optional<_Tp>& __x) noexcept
678{
679 return !static_cast<bool>(__x);
680}
681
682template <class _Tp>
683inline _LIBCPP_INLINE_VISIBILITY
684constexpr
685bool
Marshall Clow4ad67e02014-12-09 14:49:17686operator!=(const optional<_Tp>& __x, nullopt_t) noexcept
687{
688 return static_cast<bool>(__x);
689}
690
691template <class _Tp>
692inline _LIBCPP_INLINE_VISIBILITY
693constexpr
694bool
695operator!=(nullopt_t, const optional<_Tp>& __x) noexcept
696{
697 return static_cast<bool>(__x);
698}
699
700template <class _Tp>
701inline _LIBCPP_INLINE_VISIBILITY
702constexpr
703bool
Howard Hinnant01afa5c2013-09-02 20:30:37704operator<(const optional<_Tp>&, nullopt_t) noexcept
705{
706 return false;
707}
708
709template <class _Tp>
710inline _LIBCPP_INLINE_VISIBILITY
711constexpr
712bool
713operator<(nullopt_t, const optional<_Tp>& __x) noexcept
714{
715 return static_cast<bool>(__x);
716}
717
718template <class _Tp>
719inline _LIBCPP_INLINE_VISIBILITY
720constexpr
721bool
Marshall Clow4ad67e02014-12-09 14:49:17722operator<=(const optional<_Tp>& __x, nullopt_t) noexcept
723{
724 return !static_cast<bool>(__x);
725}
726
727template <class _Tp>
728inline _LIBCPP_INLINE_VISIBILITY
729constexpr
730bool
Eric Fiselier0e5ebbc2016-12-23 23:37:52731operator<=(nullopt_t, const optional<_Tp>&) noexcept
Marshall Clow4ad67e02014-12-09 14:49:17732{
733 return true;
734}
735
736template <class _Tp>
737inline _LIBCPP_INLINE_VISIBILITY
738constexpr
739bool
740operator>(const optional<_Tp>& __x, nullopt_t) noexcept
741{
742 return static_cast<bool>(__x);
743}
744
745template <class _Tp>
746inline _LIBCPP_INLINE_VISIBILITY
747constexpr
748bool
Eric Fiselier0e5ebbc2016-12-23 23:37:52749operator>(nullopt_t, const optional<_Tp>&) noexcept
Marshall Clow4ad67e02014-12-09 14:49:17750{
751 return false;
752}
753
754template <class _Tp>
755inline _LIBCPP_INLINE_VISIBILITY
756constexpr
757bool
758operator>=(const optional<_Tp>&, nullopt_t) noexcept
759{
760 return true;
761}
762
763template <class _Tp>
764inline _LIBCPP_INLINE_VISIBILITY
765constexpr
766bool
767operator>=(nullopt_t, const optional<_Tp>& __x) noexcept
768{
769 return !static_cast<bool>(__x);
770}
771
772// Comparisons with T
773template <class _Tp>
774inline _LIBCPP_INLINE_VISIBILITY
775constexpr
776bool
Howard Hinnant01afa5c2013-09-02 20:30:37777operator==(const optional<_Tp>& __x, const _Tp& __v)
778{
779 return static_cast<bool>(__x) ? *__x == __v : false;
780}
781
782template <class _Tp>
783inline _LIBCPP_INLINE_VISIBILITY
784constexpr
785bool
786operator==(const _Tp& __v, const optional<_Tp>& __x)
787{
788 return static_cast<bool>(__x) ? *__x == __v : false;
789}
790
791template <class _Tp>
792inline _LIBCPP_INLINE_VISIBILITY
793constexpr
794bool
Marshall Clow4ad67e02014-12-09 14:49:17795operator!=(const optional<_Tp>& __x, const _Tp& __v)
796{
797 return static_cast<bool>(__x) ? !(*__x == __v) : true;
798}
799
800template <class _Tp>
801inline _LIBCPP_INLINE_VISIBILITY
802constexpr
803bool
804operator!=(const _Tp& __v, const optional<_Tp>& __x)
805{
806 return static_cast<bool>(__x) ? !(*__x == __v) : true;
807}
808
809template <class _Tp>
810inline _LIBCPP_INLINE_VISIBILITY
811constexpr
812bool
Howard Hinnant01afa5c2013-09-02 20:30:37813operator<(const optional<_Tp>& __x, const _Tp& __v)
814{
815 return static_cast<bool>(__x) ? less<_Tp>{}(*__x, __v) : true;
816}
817
818template <class _Tp>
819inline _LIBCPP_INLINE_VISIBILITY
820constexpr
821bool
822operator<(const _Tp& __v, const optional<_Tp>& __x)
823{
824 return static_cast<bool>(__x) ? less<_Tp>{}(__v, *__x) : false;
825}
826
827template <class _Tp>
828inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow4ad67e02014-12-09 14:49:17829constexpr
830bool
831operator<=(const optional<_Tp>& __x, const _Tp& __v)
832{
833 return !(__x > __v);
834}
835
836template <class _Tp>
837inline _LIBCPP_INLINE_VISIBILITY
838constexpr
839bool
840operator<=(const _Tp& __v, const optional<_Tp>& __x)
841{
842 return !(__v > __x);
843}
844
845template <class _Tp>
846inline _LIBCPP_INLINE_VISIBILITY
847constexpr
848bool
849operator>(const optional<_Tp>& __x, const _Tp& __v)
850{
851 return static_cast<bool>(__x) ? __v < __x : false;
852}
853
854template <class _Tp>
855inline _LIBCPP_INLINE_VISIBILITY
856constexpr
857bool
858operator>(const _Tp& __v, const optional<_Tp>& __x)
859{
860 return static_cast<bool>(__x) ? __x < __v : true;
861}
862
863template <class _Tp>
864inline _LIBCPP_INLINE_VISIBILITY
865constexpr
866bool
867operator>=(const optional<_Tp>& __x, const _Tp& __v)
868{
869 return !(__x < __v);
870}
871
872template <class _Tp>
873inline _LIBCPP_INLINE_VISIBILITY
874constexpr
875bool
876operator>=(const _Tp& __v, const optional<_Tp>& __x)
877{
878 return !(__v < __x);
879}
880
881
882template <class _Tp>
883inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant01afa5c2013-09-02 20:30:37884void
885swap(optional<_Tp>& __x, optional<_Tp>& __y) noexcept(noexcept(__x.swap(__y)))
886{
887 __x.swap(__y);
888}
889
890template <class _Tp>
891inline _LIBCPP_INLINE_VISIBILITY
892constexpr
893optional<typename decay<_Tp>::type>
894make_optional(_Tp&& __v)
895{
896 return optional<typename decay<_Tp>::type>(_VSTD::forward<_Tp>(__v));
897}
898
Marshall Clow4ad67e02014-12-09 14:49:17899_LIBCPP_END_NAMESPACE_LFTS
Marshall Clow0cdbe602013-11-15 22:42:10900
901_LIBCPP_BEGIN_NAMESPACE_STD
902
Howard Hinnant01afa5c2013-09-02 20:30:37903template <class _Tp>
Eric Fiselierc3589a82017-01-04 23:56:00904struct _LIBCPP_TEMPLATE_VIS hash<std::experimental::optional<_Tp> >
Howard Hinnant01afa5c2013-09-02 20:30:37905{
Marshall Clow0cdbe602013-11-15 22:42:10906 typedef std::experimental::optional<_Tp> argument_type;
Howard Hinnant01afa5c2013-09-02 20:30:37907 typedef size_t result_type;
908
909 _LIBCPP_INLINE_VISIBILITY
910 result_type operator()(const argument_type& __opt) const _NOEXCEPT
911 {
912 return static_cast<bool>(__opt) ? hash<_Tp>()(*__opt) : 0;
913 }
914};
915
916_LIBCPP_END_NAMESPACE_STD
917
918#endif // _LIBCPP_STD_VER > 11
919
Eric Fiselier018a3d52017-05-31 22:07:49920_LIBCPP_POP_MACROS
921
Eric Fiselierb9231a22016-09-07 01:56:07922#endif // _LIBCPP_EXPERIMENTAL_OPTIONAL