blob: 11bf8678da0db17ddcde5d031eb90c602c376ff9 [file] [log] [blame]
Howard Hinnante7d746d2013-09-02 20:30:37 +00001// -*- 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
11#ifndef _LIBCPP_OPTIONAL
12#define _LIBCPP_OPTIONAL
13
14/*
15 optional synopsis
16
17// C++1y
18
Marshall Clowe61fba32014-12-09 14:49:17 +000019namespace std { namespace experimental { inline namespace fundamentals_v1 {
Howard Hinnante7d746d2013-09-02 20:30:37 +000020
Marshall Clowe61fba32014-12-09 14:49:17 +000021 // 5.3, optional for object types
22 template <class T> class optional;
Howard Hinnante7d746d2013-09-02 20:30:37 +000023
Marshall Clowe61fba32014-12-09 14:49:17 +000024 // 5.4, In-place construction
25 struct in_place_t{};
26 constexpr in_place_t in_place{};
Howard Hinnante7d746d2013-09-02 20:30:37 +000027
Marshall Clowe61fba32014-12-09 14:49:17 +000028 // 5.5, No-value state indicator
29 struct nullopt_t{see below};
30 constexpr nullopt_t nullopt(unspecified);
Howard Hinnante7d746d2013-09-02 20:30:37 +000031
Marshall Clowe61fba32014-12-09 14:49:17 +000032 // 5.6, Class bad_optional_access
33 class bad_optional_access;
Howard Hinnante7d746d2013-09-02 20:30:37 +000034
Marshall Clowe61fba32014-12-09 14:49:17 +000035 // 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 Hinnante7d746d2013-09-02 20:30:37 +000048
Marshall Clowe61fba32014-12-09 14:49:17 +000049 // 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 Hinnante7d746d2013-09-02 20:30:37 +000062
Marshall Clowe61fba32014-12-09 14:49:17 +000063 // 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 Hinnante7d746d2013-09-02 20:30:37 +000076
Marshall Clowe61fba32014-12-09 14:49:17 +000077 // 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 Hinnante7d746d2013-09-02 20:30:37 +000080
Marshall Clowe61fba32014-12-09 14:49:17 +000081 template <class T>
82 class optional
83 {
84 public:
85 typedef T value_type;
Howard Hinnante7d746d2013-09-02 20:30:37 +000086
Marshall Clowe61fba32014-12-09 14:49:17 +000087 // 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>
96 constexpr explicit optional(in_place_t, initializer_list<U>, Args&&...);
Howard Hinnante7d746d2013-09-02 20:30:37 +000097
Marshall Clowe61fba32014-12-09 14:49:17 +000098 // 5.3.2, Destructor
99 ~optional();
Howard Hinnante7d746d2013-09-02 20:30:37 +0000100
Marshall Clowe61fba32014-12-09 14:49:17 +0000101 // 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>
108 void emplace(initializer_list<U>, Args&&...);
Howard Hinnante7d746d2013-09-02 20:30:37 +0000109
Marshall Clowe61fba32014-12-09 14:49:17 +0000110 // 5.3.4, Swap
111 void swap(optional&) noexcept(see below);
Howard Hinnante7d746d2013-09-02 20:30:37 +0000112
Marshall Clowe61fba32014-12-09 14:49:17 +0000113 // 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 Hinnante7d746d2013-09-02 20:30:37 +0000127
Marshall Clowe61fba32014-12-09 14:49:17 +0000128 private:
129 T* val; // exposition only
130 };
Howard Hinnante7d746d2013-09-02 20:30:37 +0000131
Marshall Clowe61fba32014-12-09 14:49:17 +0000132 } // 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 Hinnante7d746d2013-09-02 20:30:37 +0000140
141*/
142
Marshall Clowe61fba32014-12-09 14:49:17 +0000143#include <experimental/__config>
Howard Hinnante7d746d2013-09-02 20:30:37 +0000144#include <functional>
145#include <stdexcept>
146
Marshall Clowe61fba32014-12-09 14:49:17 +0000147_LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL
Howard Hinnante7d746d2013-09-02 20:30:37 +0000148class _LIBCPP_EXCEPTION_ABI bad_optional_access
Marshall Clowe61fba32014-12-09 14:49:17 +0000149 : public std::logic_error
Howard Hinnante7d746d2013-09-02 20:30:37 +0000150{
151public:
Marshall Clowe61fba32014-12-09 14:49:17 +0000152 bad_optional_access() : std::logic_error("Bad optional Access") {}
153
154// Get the key function ~bad_optional_access() into the dylib
Howard Hinnante7d746d2013-09-02 20:30:37 +0000155 virtual ~bad_optional_access() _NOEXCEPT;
156};
157
Marshall Clowe61fba32014-12-09 14:49:17 +0000158_LIBCPP_END_NAMESPACE_EXPERIMENTAL
159
Howard Hinnante7d746d2013-09-02 20:30:37 +0000160
161#if _LIBCPP_STD_VER > 11
162
163#include <initializer_list>
164#include <type_traits>
165#include <new>
166#include <__functional_base>
Howard Hinnante7d746d2013-09-02 20:30:37 +0000167#include <__undef_min_max>
Eric Fiselierc1bd9192014-08-10 23:53:08 +0000168#include <__debug>
Howard Hinnante7d746d2013-09-02 20:30:37 +0000169
170#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
171#pragma GCC system_header
172#endif
173
Marshall Clowe61fba32014-12-09 14:49:17 +0000174_LIBCPP_BEGIN_NAMESPACE_LFTS
Howard Hinnante7d746d2013-09-02 20:30:37 +0000175
176struct in_place_t {};
177constexpr in_place_t in_place{};
178
179struct nullopt_t
180{
181 explicit constexpr nullopt_t(int) noexcept {}
182};
183
184constexpr nullopt_t nullopt{0};
185
186template <class _Tp, bool = is_trivially_destructible<_Tp>::value>
187class __optional_storage
188{
189protected:
190 typedef _Tp value_type;
191 union
192 {
193 char __null_state_;
194 value_type __val_;
195 };
196 bool __engaged_ = false;
197
198 _LIBCPP_INLINE_VISIBILITY
199 ~__optional_storage()
200 {
201 if (__engaged_)
202 __val_.~value_type();
203 }
204
205 _LIBCPP_INLINE_VISIBILITY
206 constexpr __optional_storage() noexcept
207 : __null_state_('\0') {}
208
209 _LIBCPP_INLINE_VISIBILITY
210 __optional_storage(const __optional_storage& __x)
211 : __engaged_(__x.__engaged_)
212 {
213 if (__engaged_)
214 ::new(_VSTD::addressof(__val_)) value_type(__x.__val_);
215 }
216
217 _LIBCPP_INLINE_VISIBILITY
218 __optional_storage(__optional_storage&& __x)
219 noexcept(is_nothrow_move_constructible<value_type>::value)
220 : __engaged_(__x.__engaged_)
221 {
222 if (__engaged_)
223 ::new(_VSTD::addressof(__val_)) value_type(_VSTD::move(__x.__val_));
224 }
225
226 _LIBCPP_INLINE_VISIBILITY
227 constexpr __optional_storage(const value_type& __v)
228 : __val_(__v),
229 __engaged_(true) {}
230
231 _LIBCPP_INLINE_VISIBILITY
232 constexpr __optional_storage(value_type&& __v)
233 : __val_(_VSTD::move(__v)),
234 __engaged_(true) {}
235
236 template <class... _Args>
237 _LIBCPP_INLINE_VISIBILITY
238 constexpr
239 explicit __optional_storage(in_place_t, _Args&&... __args)
240 : __val_(_VSTD::forward<_Args>(__args)...),
241 __engaged_(true) {}
242};
243
244template <class _Tp>
245class __optional_storage<_Tp, true>
246{
247protected:
248 typedef _Tp value_type;
249 union
250 {
251 char __null_state_;
252 value_type __val_;
253 };
254 bool __engaged_ = false;
255
256 _LIBCPP_INLINE_VISIBILITY
257 constexpr __optional_storage() noexcept
258 : __null_state_('\0') {}
259
260 _LIBCPP_INLINE_VISIBILITY
261 __optional_storage(const __optional_storage& __x)
262 : __engaged_(__x.__engaged_)
263 {
264 if (__engaged_)
265 ::new(_VSTD::addressof(__val_)) value_type(__x.__val_);
266 }
267
268 _LIBCPP_INLINE_VISIBILITY
269 __optional_storage(__optional_storage&& __x)
270 noexcept(is_nothrow_move_constructible<value_type>::value)
271 : __engaged_(__x.__engaged_)
272 {
273 if (__engaged_)
274 ::new(_VSTD::addressof(__val_)) value_type(_VSTD::move(__x.__val_));
275 }
276
277 _LIBCPP_INLINE_VISIBILITY
278 constexpr __optional_storage(const value_type& __v)
279 : __val_(__v),
280 __engaged_(true) {}
281
282 _LIBCPP_INLINE_VISIBILITY
283 constexpr __optional_storage(value_type&& __v)
284 : __val_(_VSTD::move(__v)),
285 __engaged_(true) {}
286
287 template <class... _Args>
288 _LIBCPP_INLINE_VISIBILITY
289 constexpr
290 explicit __optional_storage(in_place_t, _Args&&... __args)
291 : __val_(_VSTD::forward<_Args>(__args)...),
292 __engaged_(true) {}
293};
294
295template <class _Tp>
296class optional
297 : private __optional_storage<_Tp>
298{
299 typedef __optional_storage<_Tp> __base;
300public:
301 typedef _Tp value_type;
302
303 static_assert(!is_reference<value_type>::value,
304 "Instantiation of optional with a reference type is ill-formed.");
305 static_assert(!is_same<typename remove_cv<value_type>::type, in_place_t>::value,
306 "Instantiation of optional with a in_place_t type is ill-formed.");
307 static_assert(!is_same<typename remove_cv<value_type>::type, nullopt_t>::value,
308 "Instantiation of optional with a nullopt_t type is ill-formed.");
309 static_assert(is_object<value_type>::value,
310 "Instantiation of optional with a non-object type is undefined behavior.");
311 static_assert(is_nothrow_destructible<value_type>::value,
312 "Instantiation of optional with an object type that is not noexcept destructible is undefined behavior.");
313
314 _LIBCPP_INLINE_VISIBILITY constexpr optional() noexcept {}
315 _LIBCPP_INLINE_VISIBILITY optional(const optional&) = default;
316 _LIBCPP_INLINE_VISIBILITY optional(optional&&) = default;
317 _LIBCPP_INLINE_VISIBILITY ~optional() = default;
318 _LIBCPP_INLINE_VISIBILITY constexpr optional(nullopt_t) noexcept {}
319 _LIBCPP_INLINE_VISIBILITY constexpr optional(const value_type& __v)
320 : __base(__v) {}
321 _LIBCPP_INLINE_VISIBILITY constexpr optional(value_type&& __v)
322 : __base(_VSTD::move(__v)) {}
323
324 template <class... _Args,
325 class = typename enable_if
326 <
327 is_constructible<value_type, _Args...>::value
328 >::type
329 >
330 _LIBCPP_INLINE_VISIBILITY
331 constexpr
332 explicit optional(in_place_t, _Args&&... __args)
333 : __base(in_place, _VSTD::forward<_Args>(__args)...) {}
334
335 template <class _Up, class... _Args,
336 class = typename enable_if
337 <
338 is_constructible<value_type, initializer_list<_Up>&, _Args...>::value
339 >::type
340 >
341 _LIBCPP_INLINE_VISIBILITY
342 constexpr
343 explicit optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
344 : __base(in_place, __il, _VSTD::forward<_Args>(__args)...) {}
345
346 _LIBCPP_INLINE_VISIBILITY
347 optional& operator=(nullopt_t) noexcept
348 {
349 if (this->__engaged_)
350 {
351 this->__val_.~value_type();
352 this->__engaged_ = false;
353 }
354 return *this;
355 }
356
357 _LIBCPP_INLINE_VISIBILITY
358 optional&
359 operator=(const optional& __opt)
360 {
361 if (this->__engaged_ == __opt.__engaged_)
362 {
363 if (this->__engaged_)
364 this->__val_ = __opt.__val_;
365 }
366 else
367 {
368 if (this->__engaged_)
369 this->__val_.~value_type();
370 else
371 ::new(_VSTD::addressof(this->__val_)) value_type(__opt.__val_);
372 this->__engaged_ = __opt.__engaged_;
373 }
374 return *this;
375 }
376
377 _LIBCPP_INLINE_VISIBILITY
378 optional&
379 operator=(optional&& __opt)
380 noexcept(is_nothrow_move_assignable<value_type>::value &&
381 is_nothrow_move_constructible<value_type>::value)
382 {
383 if (this->__engaged_ == __opt.__engaged_)
384 {
385 if (this->__engaged_)
386 this->__val_ = _VSTD::move(__opt.__val_);
387 }
388 else
389 {
390 if (this->__engaged_)
391 this->__val_.~value_type();
392 else
393 ::new(_VSTD::addressof(this->__val_)) value_type(_VSTD::move(__opt.__val_));
394 this->__engaged_ = __opt.__engaged_;
395 }
396 return *this;
397 }
398
399 template <class _Up,
400 class = typename enable_if
401 <
402 is_same<typename remove_reference<_Up>::type, value_type>::value &&
403 is_constructible<value_type, _Up>::value &&
404 is_assignable<value_type&, _Up>::value
405 >::type
406 >
407 _LIBCPP_INLINE_VISIBILITY
408 optional&
409 operator=(_Up&& __v)
410 {
411 if (this->__engaged_)
412 this->__val_ = _VSTD::forward<_Up>(__v);
413 else
414 {
415 ::new(_VSTD::addressof(this->__val_)) value_type(_VSTD::forward<_Up>(__v));
416 this->__engaged_ = true;
417 }
418 return *this;
419 }
420
421 template <class... _Args,
422 class = typename enable_if
423 <
424 is_constructible<value_type, _Args...>::value
425 >::type
426 >
427 _LIBCPP_INLINE_VISIBILITY
428 void
429 emplace(_Args&&... __args)
430 {
431 *this = nullopt;
432 ::new(_VSTD::addressof(this->__val_)) value_type(_VSTD::forward<_Args>(__args)...);
433 this->__engaged_ = true;
434 }
435
436 template <class _Up, class... _Args,
437 class = typename enable_if
438 <
439 is_constructible<value_type, initializer_list<_Up>&, _Args...>::value
440 >::type
441 >
442 _LIBCPP_INLINE_VISIBILITY
443 void
444 emplace(initializer_list<_Up> __il, _Args&&... __args)
445 {
446 *this = nullopt;
447 ::new(_VSTD::addressof(this->__val_)) value_type(__il, _VSTD::forward<_Args>(__args)...);
448 this->__engaged_ = true;
449 }
450
451 _LIBCPP_INLINE_VISIBILITY
452 void
453 swap(optional& __opt)
454 noexcept(is_nothrow_move_constructible<value_type>::value &&
455 __is_nothrow_swappable<value_type>::value)
456 {
457 using _VSTD::swap;
458 if (this->__engaged_ == __opt.__engaged_)
459 {
460 if (this->__engaged_)
461 swap(this->__val_, __opt.__val_);
462 }
463 else
464 {
465 if (this->__engaged_)
466 {
467 ::new(_VSTD::addressof(__opt.__val_)) value_type(_VSTD::move(this->__val_));
468 this->__val_.~value_type();
469 }
470 else
471 {
472 ::new(_VSTD::addressof(this->__val_)) value_type(_VSTD::move(__opt.__val_));
473 __opt.__val_.~value_type();
474 }
475 swap(this->__engaged_, __opt.__engaged_);
476 }
477 }
478
479 _LIBCPP_INLINE_VISIBILITY
480 constexpr
481 value_type const*
482 operator->() const
483 {
484 _LIBCPP_ASSERT(this->__engaged_, "optional operator-> called for disengaged value");
485 return __operator_arrow(__has_operator_addressof<value_type>{});
486 }
487
488 _LIBCPP_INLINE_VISIBILITY
489 value_type*
490 operator->()
491 {
492 _LIBCPP_ASSERT(this->__engaged_, "optional operator-> called for disengaged value");
493 return _VSTD::addressof(this->__val_);
494 }
495
496 _LIBCPP_INLINE_VISIBILITY
497 constexpr
498 const value_type&
499 operator*() const
500 {
501 _LIBCPP_ASSERT(this->__engaged_, "optional operator* called for disengaged value");
502 return this->__val_;
503 }
504
505 _LIBCPP_INLINE_VISIBILITY
506 value_type&
507 operator*()
508 {
509 _LIBCPP_ASSERT(this->__engaged_, "optional operator* called for disengaged value");
510 return this->__val_;
511 }
512
513 _LIBCPP_INLINE_VISIBILITY
514 constexpr explicit operator bool() const noexcept {return this->__engaged_;}
515
Marshall Clowd437fa52016-08-25 15:09:01 +0000516 _LIBCPP_NORETURN _LIBCPP_INLINE_VISIBILITY
517 constexpr void __throw_bad_optional_access() const
518 {
519#ifndef _LIBCPP_NO_EXCEPTIONS
520 throw bad_optional_access();
521#else
522 _VSTD::abort();
523#endif
524 }
525
Howard Hinnante7d746d2013-09-02 20:30:37 +0000526 _LIBCPP_INLINE_VISIBILITY
527 constexpr value_type const& value() const
528 {
529 if (!this->__engaged_)
Marshall Clowd437fa52016-08-25 15:09:01 +0000530 __throw_bad_optional_access();
Howard Hinnante7d746d2013-09-02 20:30:37 +0000531 return this->__val_;
532 }
533
534 _LIBCPP_INLINE_VISIBILITY
535 value_type& value()
536 {
537 if (!this->__engaged_)
Marshall Clowd437fa52016-08-25 15:09:01 +0000538 __throw_bad_optional_access();
Howard Hinnante7d746d2013-09-02 20:30:37 +0000539 return this->__val_;
540 }
541
542 template <class _Up>
543 _LIBCPP_INLINE_VISIBILITY
544 constexpr value_type value_or(_Up&& __v) const&
545 {
546 static_assert(is_copy_constructible<value_type>::value,
547 "optional<T>::value_or: T must be copy constructible");
548 static_assert(is_convertible<_Up, value_type>::value,
549 "optional<T>::value_or: U must be convertible to T");
550 return this->__engaged_ ? this->__val_ :
551 static_cast<value_type>(_VSTD::forward<_Up>(__v));
552 }
553
554 template <class _Up>
555 _LIBCPP_INLINE_VISIBILITY
556 value_type value_or(_Up&& __v) &&
557 {
558 static_assert(is_move_constructible<value_type>::value,
559 "optional<T>::value_or: T must be move constructible");
560 static_assert(is_convertible<_Up, value_type>::value,
561 "optional<T>::value_or: U must be convertible to T");
562 return this->__engaged_ ? _VSTD::move(this->__val_) :
563 static_cast<value_type>(_VSTD::forward<_Up>(__v));
564 }
565
566private:
567 _LIBCPP_INLINE_VISIBILITY
568 value_type const*
569 __operator_arrow(true_type) const
570 {
571 return _VSTD::addressof(this->__val_);
572 }
573
574 _LIBCPP_INLINE_VISIBILITY
575 constexpr
576 value_type const*
577 __operator_arrow(false_type) const
578 {
579 return &this->__val_;
580 }
581};
582
Marshall Clowe61fba32014-12-09 14:49:17 +0000583// Comparisons between optionals
Howard Hinnante7d746d2013-09-02 20:30:37 +0000584template <class _Tp>
585inline _LIBCPP_INLINE_VISIBILITY
586constexpr
587bool
588operator==(const optional<_Tp>& __x, const optional<_Tp>& __y)
589{
590 if (static_cast<bool>(__x) != static_cast<bool>(__y))
591 return false;
592 if (!static_cast<bool>(__x))
593 return true;
594 return *__x == *__y;
595}
596
597template <class _Tp>
598inline _LIBCPP_INLINE_VISIBILITY
599constexpr
600bool
Marshall Clowe61fba32014-12-09 14:49:17 +0000601operator!=(const optional<_Tp>& __x, const optional<_Tp>& __y)
602{
603 return !(__x == __y);
604}
605
606template <class _Tp>
607inline _LIBCPP_INLINE_VISIBILITY
608constexpr
609bool
Howard Hinnante7d746d2013-09-02 20:30:37 +0000610operator<(const optional<_Tp>& __x, const optional<_Tp>& __y)
611{
612 if (!static_cast<bool>(__y))
613 return false;
614 if (!static_cast<bool>(__x))
615 return true;
Marshall Clowe61fba32014-12-09 14:49:17 +0000616 return *__x < *__y;
Howard Hinnante7d746d2013-09-02 20:30:37 +0000617}
618
619template <class _Tp>
620inline _LIBCPP_INLINE_VISIBILITY
621constexpr
622bool
Marshall Clowe61fba32014-12-09 14:49:17 +0000623operator>(const optional<_Tp>& __x, const optional<_Tp>& __y)
624{
625 return __y < __x;
626}
627
628template <class _Tp>
629inline _LIBCPP_INLINE_VISIBILITY
630constexpr
631bool
632operator<=(const optional<_Tp>& __x, const optional<_Tp>& __y)
633{
634 return !(__y < __x);
635}
636
637template <class _Tp>
638inline _LIBCPP_INLINE_VISIBILITY
639constexpr
640bool
641operator>=(const optional<_Tp>& __x, const optional<_Tp>& __y)
642{
643 return !(__x < __y);
644}
645
646
647// Comparisons with nullopt
648template <class _Tp>
649inline _LIBCPP_INLINE_VISIBILITY
650constexpr
651bool
Howard Hinnante7d746d2013-09-02 20:30:37 +0000652operator==(const optional<_Tp>& __x, nullopt_t) noexcept
653{
654 return !static_cast<bool>(__x);
655}
656
657template <class _Tp>
658inline _LIBCPP_INLINE_VISIBILITY
659constexpr
660bool
661operator==(nullopt_t, const optional<_Tp>& __x) noexcept
662{
663 return !static_cast<bool>(__x);
664}
665
666template <class _Tp>
667inline _LIBCPP_INLINE_VISIBILITY
668constexpr
669bool
Marshall Clowe61fba32014-12-09 14:49:17 +0000670operator!=(const optional<_Tp>& __x, nullopt_t) noexcept
671{
672 return static_cast<bool>(__x);
673}
674
675template <class _Tp>
676inline _LIBCPP_INLINE_VISIBILITY
677constexpr
678bool
679operator!=(nullopt_t, const optional<_Tp>& __x) noexcept
680{
681 return static_cast<bool>(__x);
682}
683
684template <class _Tp>
685inline _LIBCPP_INLINE_VISIBILITY
686constexpr
687bool
Howard Hinnante7d746d2013-09-02 20:30:37 +0000688operator<(const optional<_Tp>&, nullopt_t) noexcept
689{
690 return false;
691}
692
693template <class _Tp>
694inline _LIBCPP_INLINE_VISIBILITY
695constexpr
696bool
697operator<(nullopt_t, const optional<_Tp>& __x) noexcept
698{
699 return static_cast<bool>(__x);
700}
701
702template <class _Tp>
703inline _LIBCPP_INLINE_VISIBILITY
704constexpr
705bool
Marshall Clowe61fba32014-12-09 14:49:17 +0000706operator<=(const optional<_Tp>& __x, nullopt_t) noexcept
707{
708 return !static_cast<bool>(__x);
709}
710
711template <class _Tp>
712inline _LIBCPP_INLINE_VISIBILITY
713constexpr
714bool
715operator<=(nullopt_t, const optional<_Tp>& __x) noexcept
716{
717 return true;
718}
719
720template <class _Tp>
721inline _LIBCPP_INLINE_VISIBILITY
722constexpr
723bool
724operator>(const optional<_Tp>& __x, nullopt_t) noexcept
725{
726 return static_cast<bool>(__x);
727}
728
729template <class _Tp>
730inline _LIBCPP_INLINE_VISIBILITY
731constexpr
732bool
733operator>(nullopt_t, const optional<_Tp>& __x) noexcept
734{
735 return false;
736}
737
738template <class _Tp>
739inline _LIBCPP_INLINE_VISIBILITY
740constexpr
741bool
742operator>=(const optional<_Tp>&, nullopt_t) noexcept
743{
744 return true;
745}
746
747template <class _Tp>
748inline _LIBCPP_INLINE_VISIBILITY
749constexpr
750bool
751operator>=(nullopt_t, const optional<_Tp>& __x) noexcept
752{
753 return !static_cast<bool>(__x);
754}
755
756// Comparisons with T
757template <class _Tp>
758inline _LIBCPP_INLINE_VISIBILITY
759constexpr
760bool
Howard Hinnante7d746d2013-09-02 20:30:37 +0000761operator==(const optional<_Tp>& __x, const _Tp& __v)
762{
763 return static_cast<bool>(__x) ? *__x == __v : false;
764}
765
766template <class _Tp>
767inline _LIBCPP_INLINE_VISIBILITY
768constexpr
769bool
770operator==(const _Tp& __v, const optional<_Tp>& __x)
771{
772 return static_cast<bool>(__x) ? *__x == __v : false;
773}
774
775template <class _Tp>
776inline _LIBCPP_INLINE_VISIBILITY
777constexpr
778bool
Marshall Clowe61fba32014-12-09 14:49:17 +0000779operator!=(const optional<_Tp>& __x, const _Tp& __v)
780{
781 return static_cast<bool>(__x) ? !(*__x == __v) : true;
782}
783
784template <class _Tp>
785inline _LIBCPP_INLINE_VISIBILITY
786constexpr
787bool
788operator!=(const _Tp& __v, const optional<_Tp>& __x)
789{
790 return static_cast<bool>(__x) ? !(*__x == __v) : true;
791}
792
793template <class _Tp>
794inline _LIBCPP_INLINE_VISIBILITY
795constexpr
796bool
Howard Hinnante7d746d2013-09-02 20:30:37 +0000797operator<(const optional<_Tp>& __x, const _Tp& __v)
798{
799 return static_cast<bool>(__x) ? less<_Tp>{}(*__x, __v) : true;
800}
801
802template <class _Tp>
803inline _LIBCPP_INLINE_VISIBILITY
804constexpr
805bool
806operator<(const _Tp& __v, const optional<_Tp>& __x)
807{
808 return static_cast<bool>(__x) ? less<_Tp>{}(__v, *__x) : false;
809}
810
811template <class _Tp>
812inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowe61fba32014-12-09 14:49:17 +0000813constexpr
814bool
815operator<=(const optional<_Tp>& __x, const _Tp& __v)
816{
817 return !(__x > __v);
818}
819
820template <class _Tp>
821inline _LIBCPP_INLINE_VISIBILITY
822constexpr
823bool
824operator<=(const _Tp& __v, const optional<_Tp>& __x)
825{
826 return !(__v > __x);
827}
828
829template <class _Tp>
830inline _LIBCPP_INLINE_VISIBILITY
831constexpr
832bool
833operator>(const optional<_Tp>& __x, const _Tp& __v)
834{
835 return static_cast<bool>(__x) ? __v < __x : false;
836}
837
838template <class _Tp>
839inline _LIBCPP_INLINE_VISIBILITY
840constexpr
841bool
842operator>(const _Tp& __v, const optional<_Tp>& __x)
843{
844 return static_cast<bool>(__x) ? __x < __v : true;
845}
846
847template <class _Tp>
848inline _LIBCPP_INLINE_VISIBILITY
849constexpr
850bool
851operator>=(const optional<_Tp>& __x, const _Tp& __v)
852{
853 return !(__x < __v);
854}
855
856template <class _Tp>
857inline _LIBCPP_INLINE_VISIBILITY
858constexpr
859bool
860operator>=(const _Tp& __v, const optional<_Tp>& __x)
861{
862 return !(__v < __x);
863}
864
865
866template <class _Tp>
867inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante7d746d2013-09-02 20:30:37 +0000868void
869swap(optional<_Tp>& __x, optional<_Tp>& __y) noexcept(noexcept(__x.swap(__y)))
870{
871 __x.swap(__y);
872}
873
874template <class _Tp>
875inline _LIBCPP_INLINE_VISIBILITY
876constexpr
877optional<typename decay<_Tp>::type>
878make_optional(_Tp&& __v)
879{
880 return optional<typename decay<_Tp>::type>(_VSTD::forward<_Tp>(__v));
881}
882
Marshall Clowe61fba32014-12-09 14:49:17 +0000883_LIBCPP_END_NAMESPACE_LFTS
Marshall Clowdfdac032013-11-15 22:42:10 +0000884
885_LIBCPP_BEGIN_NAMESPACE_STD
886
Howard Hinnante7d746d2013-09-02 20:30:37 +0000887template <class _Tp>
Marshall Clowdfdac032013-11-15 22:42:10 +0000888struct _LIBCPP_TYPE_VIS_ONLY hash<std::experimental::optional<_Tp> >
Howard Hinnante7d746d2013-09-02 20:30:37 +0000889{
Marshall Clowdfdac032013-11-15 22:42:10 +0000890 typedef std::experimental::optional<_Tp> argument_type;
Howard Hinnante7d746d2013-09-02 20:30:37 +0000891 typedef size_t result_type;
892
893 _LIBCPP_INLINE_VISIBILITY
894 result_type operator()(const argument_type& __opt) const _NOEXCEPT
895 {
896 return static_cast<bool>(__opt) ? hash<_Tp>()(*__opt) : 0;
897 }
898};
899
900_LIBCPP_END_NAMESPACE_STD
901
902#endif // _LIBCPP_STD_VER > 11
903
Marshall Clowaedfb872014-10-26 20:29:38 +0000904#endif // _LIBCPP_OPTIONAL