1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_OPTIONAL_H_
6 #define BASE_OPTIONAL_H_
11 #include "base/logging.h"
12 #include "base/template_util.h"
17 // http://en.cppreference.com/w/cpp/utility/optional/in_place_t
21 // http://en.cppreference.com/w/cpp/utility/optional/nullopt_t
23 constexpr explicit nullopt_t(int) {}
27 // http://en.cppreference.com/w/cpp/utility/optional/in_place
28 constexpr in_place_t in_place = {};
31 // http://en.cppreference.com/w/cpp/utility/optional/nullopt
32 constexpr nullopt_t nullopt(0);
34 // Forward declaration, which is refered by following helpers.
40 template <typename T, bool = std::is_trivially_destructible<T>::value>
41 struct OptionalStorageBase {
42 // Initializing |empty_| here instead of using default member initializing
43 // to avoid errors in g++ 4.8.
44 constexpr OptionalStorageBase() : empty_('\0') {}
46 template <class... Args>
47 constexpr explicit OptionalStorageBase(in_place_t, Args&&... args)
48 : is_populated_(true), value_(std::forward<Args>(args)...) {}
50 // When T is not trivially destructible we must call its
51 // destructor before deallocating its memory.
52 // Note that this hides the (implicitly declared) move constructor, which
53 // would be used for constexpr move constructor in OptionalStorage<T>.
54 // It is needed iff T is trivially move constructible. However, the current
55 // is_trivially_{copy,move}_constructible implementation requires
56 // is_trivially_destructible (which looks a bug, cf:
57 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51452 and
58 // http://cplusplus.github.io/LWG/lwg-active.html#2116), so it is not
59 // necessary for this case at the moment. Please see also the destructor
60 // comment in "is_trivially_destructible = true" specialization below.
61 ~OptionalStorageBase() {
66 template <class... Args>
67 void Init(Args&&... args) {
68 DCHECK(!is_populated_);
69 ::new (&value_) T(std::forward<Args>(args)...);
73 bool is_populated_ = false;
75 // |empty_| exists so that the union will always be initialized, even when
76 // it doesn't contain a value. Union members must be initialized for the
77 // constructor to be 'constexpr'.
84 struct OptionalStorageBase<T, true /* trivially destructible */> {
85 // Initializing |empty_| here instead of using default member initializing
86 // to avoid errors in g++ 4.8.
87 constexpr OptionalStorageBase() : empty_('\0') {}
89 template <class... Args>
90 constexpr explicit OptionalStorageBase(in_place_t, Args&&... args)
91 : is_populated_(true), value_(std::forward<Args>(args)...) {}
93 // When T is trivially destructible (i.e. its destructor does nothing) there
94 // is no need to call it. Implicitly defined destructor is trivial, because
95 // both members (bool and union containing only variants which are trivially
96 // destructible) are trivially destructible.
97 // Explicitly-defaulted destructor is also trivial, but do not use it here,
98 // because it hides the implicit move constructor. It is needed to implement
99 // constexpr move constructor in OptionalStorage iff T is trivially move
100 // constructible. Note that, if T is trivially move constructible, the move
101 // constructor of OptionalStorageBase<T> is also implicitly defined and it is
102 // trivially move constructor. If T is not trivially move constructible,
103 // "not declaring move constructor without destructor declaration" here means
104 // "delete move constructor", which works because any move constructor of
105 // OptionalStorage will not refer to it in that case.
107 template <class... Args>
108 void Init(Args&&... args) {
109 DCHECK(!is_populated_);
110 ::new (&value_) T(std::forward<Args>(args)...);
111 is_populated_ = true;
114 bool is_populated_ = false;
116 // |empty_| exists so that the union will always be initialized, even when
117 // it doesn't contain a value. Union members must be initialized for the
118 // constructor to be 'constexpr'.
124 // Implement conditional constexpr copy and move constructors. These are
125 // constexpr if is_trivially_{copy,move}_constructible<T>::value is true
126 // respectively. If each is true, the corresponding constructor is defined as
127 // "= default;", which generates a constexpr constructor (In this case,
128 // the condition of constexpr-ness is satisfied because the base class also has
129 // compiler generated constexpr {copy,move} constructors). Note that
130 // placement-new is prohibited in constexpr.
131 template <typename T,
132 bool = is_trivially_copy_constructible<T>::value,
133 bool = std::is_trivially_move_constructible<T>::value>
134 struct OptionalStorage : OptionalStorageBase<T> {
135 // This is no trivially {copy,move} constructible case. Other cases are
136 // defined below as specializations.
138 // Accessing the members of template base class requires explicit
140 using OptionalStorageBase<T>::is_populated_;
141 using OptionalStorageBase<T>::value_;
142 using OptionalStorageBase<T>::Init;
144 // Inherit constructors (specifically, the in_place constructor).
145 using OptionalStorageBase<T>::OptionalStorageBase;
147 // User defined constructor deletes the default constructor.
148 // Define it explicitly.
149 OptionalStorage() = default;
151 OptionalStorage(const OptionalStorage& other) {
152 if (other.is_populated_)
156 OptionalStorage(OptionalStorage&& other) noexcept(
157 std::is_nothrow_move_constructible<T>::value) {
158 if (other.is_populated_)
159 Init(std::move(other.value_));
163 template <typename T>
164 struct OptionalStorage<T,
165 true /* trivially copy constructible */,
166 false /* trivially move constructible */>
167 : OptionalStorageBase<T> {
168 using OptionalStorageBase<T>::is_populated_;
169 using OptionalStorageBase<T>::value_;
170 using OptionalStorageBase<T>::Init;
171 using OptionalStorageBase<T>::OptionalStorageBase;
173 OptionalStorage() = default;
174 OptionalStorage(const OptionalStorage& other) = default;
176 OptionalStorage(OptionalStorage&& other) noexcept(
177 std::is_nothrow_move_constructible<T>::value) {
178 if (other.is_populated_)
179 Init(std::move(other.value_));
183 template <typename T>
184 struct OptionalStorage<T,
185 false /* trivially copy constructible */,
186 true /* trivially move constructible */>
187 : OptionalStorageBase<T> {
188 using OptionalStorageBase<T>::is_populated_;
189 using OptionalStorageBase<T>::value_;
190 using OptionalStorageBase<T>::Init;
191 using OptionalStorageBase<T>::OptionalStorageBase;
193 OptionalStorage() = default;
194 OptionalStorage(OptionalStorage&& other) = default;
196 OptionalStorage(const OptionalStorage& other) {
197 if (other.is_populated_)
202 template <typename T>
203 struct OptionalStorage<T,
204 true /* trivially copy constructible */,
205 true /* trivially move constructible */>
206 : OptionalStorageBase<T> {
207 // If both trivially {copy,move} constructible are true, it is not necessary
208 // to use user-defined constructors. So, just inheriting constructors
209 // from the base class works.
210 using OptionalStorageBase<T>::OptionalStorageBase;
213 // Base class to support conditionally usable copy-/move- constructors
214 // and assign operators.
215 template <typename T>
217 // This class provides implementation rather than public API, so everything
218 // should be hidden. Often we use composition, but we cannot in this case
219 // because of C++ language restriction.
221 constexpr OptionalBase() = default;
222 constexpr OptionalBase(const OptionalBase& other) = default;
223 constexpr OptionalBase(OptionalBase&& other) = default;
225 template <class... Args>
226 constexpr explicit OptionalBase(in_place_t, Args&&... args)
227 : storage_(in_place, std::forward<Args>(args)...) {}
229 // Implementation of converting constructors.
230 template <typename U>
231 explicit OptionalBase(const OptionalBase<U>& other) {
232 if (other.storage_.is_populated_)
233 storage_.Init(other.storage_.value_);
236 template <typename U>
237 explicit OptionalBase(OptionalBase<U>&& other) {
238 if (other.storage_.is_populated_)
239 storage_.Init(std::move(other.storage_.value_));
242 ~OptionalBase() = default;
244 OptionalBase& operator=(const OptionalBase& other) {
249 OptionalBase& operator=(OptionalBase&& other) noexcept(
250 std::is_nothrow_move_assignable<T>::value&&
251 std::is_nothrow_move_constructible<T>::value) {
252 MoveAssign(std::move(other));
256 template <typename U>
257 void CopyAssign(const OptionalBase<U>& other) {
258 if (other.storage_.is_populated_)
259 InitOrAssign(other.storage_.value_);
264 template <typename U>
265 void MoveAssign(OptionalBase<U>&& other) {
266 if (other.storage_.is_populated_)
267 InitOrAssign(std::move(other.storage_.value_));
272 template <typename U>
273 void InitOrAssign(U&& value) {
274 if (storage_.is_populated_)
275 storage_.value_ = std::forward<U>(value);
277 storage_.Init(std::forward<U>(value));
280 void FreeIfNeeded() {
281 if (!storage_.is_populated_)
283 storage_.value_.~T();
284 storage_.is_populated_ = false;
287 // For implementing conversion, allow access to other typed OptionalBase
289 template <typename U>
290 friend class OptionalBase;
292 OptionalStorage<T> storage_;
295 // The following {Copy,Move}{Constructible,Assignable} structs are helpers to
296 // implement constructor/assign-operator overloading. Specifically, if T is
297 // is not movable but copyable, Optional<T>'s move constructor should not
298 // participate in overload resolution. This inheritance trick implements that.
299 template <bool is_copy_constructible>
300 struct CopyConstructible {};
303 struct CopyConstructible<false> {
304 constexpr CopyConstructible() = default;
305 constexpr CopyConstructible(const CopyConstructible&) = delete;
306 constexpr CopyConstructible(CopyConstructible&&) = default;
307 CopyConstructible& operator=(const CopyConstructible&) = default;
308 CopyConstructible& operator=(CopyConstructible&&) = default;
311 template <bool is_move_constructible>
312 struct MoveConstructible {};
315 struct MoveConstructible<false> {
316 constexpr MoveConstructible() = default;
317 constexpr MoveConstructible(const MoveConstructible&) = default;
318 constexpr MoveConstructible(MoveConstructible&&) = delete;
319 MoveConstructible& operator=(const MoveConstructible&) = default;
320 MoveConstructible& operator=(MoveConstructible&&) = default;
323 template <bool is_copy_assignable>
324 struct CopyAssignable {};
327 struct CopyAssignable<false> {
328 constexpr CopyAssignable() = default;
329 constexpr CopyAssignable(const CopyAssignable&) = default;
330 constexpr CopyAssignable(CopyAssignable&&) = default;
331 CopyAssignable& operator=(const CopyAssignable&) = delete;
332 CopyAssignable& operator=(CopyAssignable&&) = default;
335 template <bool is_move_assignable>
336 struct MoveAssignable {};
339 struct MoveAssignable<false> {
340 constexpr MoveAssignable() = default;
341 constexpr MoveAssignable(const MoveAssignable&) = default;
342 constexpr MoveAssignable(MoveAssignable&&) = default;
343 MoveAssignable& operator=(const MoveAssignable&) = default;
344 MoveAssignable& operator=(MoveAssignable&&) = delete;
347 // Helper to conditionally enable converting constructors and assign operators.
348 template <typename T, typename U>
349 struct IsConvertibleFromOptional
350 : std::integral_constant<
352 std::is_constructible<T, Optional<U>&>::value ||
353 std::is_constructible<T, const Optional<U>&>::value ||
354 std::is_constructible<T, Optional<U>&&>::value ||
355 std::is_constructible<T, const Optional<U>&&>::value ||
356 std::is_convertible<Optional<U>&, T>::value ||
357 std::is_convertible<const Optional<U>&, T>::value ||
358 std::is_convertible<Optional<U>&&, T>::value ||
359 std::is_convertible<const Optional<U>&&, T>::value> {};
361 template <typename T, typename U>
362 struct IsAssignableFromOptional
363 : std::integral_constant<
365 IsConvertibleFromOptional<T, U>::value ||
366 std::is_assignable<T&, Optional<U>&>::value ||
367 std::is_assignable<T&, const Optional<U>&>::value ||
368 std::is_assignable<T&, Optional<U>&&>::value ||
369 std::is_assignable<T&, const Optional<U>&&>::value> {};
371 // Forward compatibility for C++17.
372 // Introduce one more deeper nested namespace to avoid leaking using std::swap.
373 namespace swappable_impl {
376 struct IsSwappableImpl {
377 // Tests if swap can be called. Check<T&>(0) returns true_type iff swap
378 // is available for T. Otherwise, Check's overload resolution falls back
379 // to Check(...) declared below thanks to SFINAE, so returns false_type.
380 template <typename T>
381 static auto Check(int)
382 -> decltype(swap(std::declval<T>(), std::declval<T>()), std::true_type());
384 template <typename T>
385 static std::false_type Check(...);
387 } // namespace swappable_impl
389 template <typename T>
390 struct IsSwappable : decltype(swappable_impl::IsSwappableImpl::Check<T&>(0)) {};
392 // Forward compatibility for C++20.
393 template <typename T>
394 using RemoveCvRefT = std::remove_cv_t<std::remove_reference_t<T>>;
396 } // namespace internal
398 // On Windows, by default, empty-base class optimization does not work,
399 // which means even if the base class is empty struct, it still consumes one
400 // byte for its body. __declspec(empty_bases) enables the optimization.
402 // https://blogs.msdn.microsoft.com/vcblog/2016/03/30/optimizing-the-layout-of-empty-base-classes-in-vs2015-update-2-3/
404 #define OPTIONAL_DECLSPEC_EMPTY_BASES __declspec(empty_bases)
406 #define OPTIONAL_DECLSPEC_EMPTY_BASES
409 // base::Optional is a Chromium version of the C++17 optional class:
410 // std::optional documentation:
411 // http://en.cppreference.com/w/cpp/utility/optional
412 // Chromium documentation:
413 // https://chromium.googlesource.com/chromium/src/+/master/docs/optional.md
415 // These are the differences between the specification and the implementation:
416 // - Constructors do not use 'constexpr' as it is a C++14 extension.
417 // - 'constexpr' might be missing in some places for reasons specified locally.
418 // - No exceptions are thrown, because they are banned from Chromium.
419 // Marked noexcept for only move constructor and move assign operators.
420 // - All the non-members are in the 'base' namespace instead of 'std'.
422 // Note that T cannot have a constructor T(Optional<T>) etc. Optional<T> checks
423 // T's constructor (specifically via IsConvertibleFromOptional), and in the
424 // check whether T can be constructible from Optional<T>, which is recursive
425 // so it does not work. As of Feb 2018, std::optional C++17 implementation in
426 // both clang and gcc has same limitation. MSVC SFINAE looks to have different
427 // behavior, but anyway it reports an error, too.
428 template <typename T>
429 class OPTIONAL_DECLSPEC_EMPTY_BASES Optional
430 : public internal::OptionalBase<T>,
431 public internal::CopyConstructible<std::is_copy_constructible<T>::value>,
432 public internal::MoveConstructible<std::is_move_constructible<T>::value>,
433 public internal::CopyAssignable<std::is_copy_constructible<T>::value &&
434 std::is_copy_assignable<T>::value>,
435 public internal::MoveAssignable<std::is_move_constructible<T>::value &&
436 std::is_move_assignable<T>::value> {
438 #undef OPTIONAL_DECLSPEC_EMPTY_BASES
439 using value_type = T;
441 // Defer default/copy/move constructor implementation to OptionalBase.
442 constexpr Optional() = default;
443 constexpr Optional(const Optional& other) = default;
444 constexpr Optional(Optional&& other) noexcept(
445 std::is_nothrow_move_constructible<T>::value) = default;
447 constexpr Optional(nullopt_t) {} // NOLINT(runtime/explicit)
449 // Converting copy constructor. "explicit" only if
450 // std::is_convertible<const U&, T>::value is false. It is implemented by
451 // declaring two almost same constructors, but that condition in enable_if_t
452 // is different, so that either one is chosen, thanks to SFINAE.
455 std::enable_if_t<std::is_constructible<T, const U&>::value &&
456 !internal::IsConvertibleFromOptional<T, U>::value &&
457 std::is_convertible<const U&, T>::value,
459 Optional(const Optional<U>& other) : internal::OptionalBase<T>(other) {}
463 std::enable_if_t<std::is_constructible<T, const U&>::value &&
464 !internal::IsConvertibleFromOptional<T, U>::value &&
465 !std::is_convertible<const U&, T>::value,
467 explicit Optional(const Optional<U>& other)
468 : internal::OptionalBase<T>(other) {}
470 // Converting move constructor. Similar to converting copy constructor,
471 // declaring two (explicit and non-explicit) constructors.
474 std::enable_if_t<std::is_constructible<T, U&&>::value &&
475 !internal::IsConvertibleFromOptional<T, U>::value &&
476 std::is_convertible<U&&, T>::value,
478 Optional(Optional<U>&& other) : internal::OptionalBase<T>(std::move(other)) {}
482 std::enable_if_t<std::is_constructible<T, U&&>::value &&
483 !internal::IsConvertibleFromOptional<T, U>::value &&
484 !std::is_convertible<U&&, T>::value,
486 explicit Optional(Optional<U>&& other)
487 : internal::OptionalBase<T>(std::move(other)) {}
489 template <class... Args>
490 constexpr explicit Optional(in_place_t, Args&&... args)
491 : internal::OptionalBase<T>(in_place, std::forward<Args>(args)...) {}
496 class = std::enable_if_t<std::is_constructible<value_type,
497 std::initializer_list<U>&,
499 constexpr explicit Optional(in_place_t,
500 std::initializer_list<U> il,
502 : internal::OptionalBase<T>(in_place, il, std::forward<Args>(args)...) {}
504 // Forward value constructor. Similar to converting constructors,
505 // conditionally explicit.
507 typename U = value_type,
509 std::is_constructible<T, U&&>::value &&
510 !std::is_same<internal::RemoveCvRefT<U>, in_place_t>::value &&
511 !std::is_same<internal::RemoveCvRefT<U>, Optional<T>>::value &&
512 std::is_convertible<U&&, T>::value,
514 constexpr Optional(U&& value)
515 : internal::OptionalBase<T>(in_place, std::forward<U>(value)) {}
518 typename U = value_type,
520 std::is_constructible<T, U&&>::value &&
521 !std::is_same<internal::RemoveCvRefT<U>, in_place_t>::value &&
522 !std::is_same<internal::RemoveCvRefT<U>, Optional<T>>::value &&
523 !std::is_convertible<U&&, T>::value,
525 constexpr explicit Optional(U&& value)
526 : internal::OptionalBase<T>(in_place, std::forward<U>(value)) {}
528 ~Optional() = default;
530 // Defer copy-/move- assign operator implementation to OptionalBase.
531 Optional& operator=(const Optional& other) = default;
532 Optional& operator=(Optional&& other) noexcept(
533 std::is_nothrow_move_assignable<T>::value&&
534 std::is_nothrow_move_constructible<T>::value) = default;
536 Optional& operator=(nullopt_t) {
541 // Perfect-forwarded assignment.
542 template <typename U>
544 !std::is_same<internal::RemoveCvRefT<U>, Optional<T>>::value &&
545 std::is_constructible<T, U>::value &&
546 std::is_assignable<T&, U>::value &&
547 (!std::is_scalar<T>::value ||
548 !std::is_same<std::decay_t<U>, T>::value),
550 operator=(U&& value) {
551 InitOrAssign(std::forward<U>(value));
555 // Copy assign the state of other.
556 template <typename U>
557 std::enable_if_t<!internal::IsAssignableFromOptional<T, U>::value &&
558 std::is_constructible<T, const U&>::value &&
559 std::is_assignable<T&, const U&>::value,
561 operator=(const Optional<U>& other) {
566 // Move assign the state of other.
567 template <typename U>
568 std::enable_if_t<!internal::IsAssignableFromOptional<T, U>::value &&
569 std::is_constructible<T, U>::value &&
570 std::is_assignable<T&, U>::value,
572 operator=(Optional<U>&& other) {
573 MoveAssign(std::move(other));
577 constexpr const T* operator->() const {
578 DCHECK(storage_.is_populated_);
579 return &storage_.value_;
582 constexpr T* operator->() {
583 DCHECK(storage_.is_populated_);
584 return &storage_.value_;
587 constexpr const T& operator*() const & {
588 DCHECK(storage_.is_populated_);
589 return storage_.value_;
592 constexpr T& operator*() & {
593 DCHECK(storage_.is_populated_);
594 return storage_.value_;
597 constexpr const T&& operator*() const && {
598 DCHECK(storage_.is_populated_);
599 return std::move(storage_.value_);
602 constexpr T&& operator*() && {
603 DCHECK(storage_.is_populated_);
604 return std::move(storage_.value_);
607 constexpr explicit operator bool() const { return storage_.is_populated_; }
609 constexpr bool has_value() const { return storage_.is_populated_; }
611 constexpr T& value() & {
612 CHECK(storage_.is_populated_);
613 return storage_.value_;
616 constexpr const T& value() const & {
617 CHECK(storage_.is_populated_);
618 return storage_.value_;
621 constexpr T&& value() && {
622 CHECK(storage_.is_populated_);
623 return std::move(storage_.value_);
626 constexpr const T&& value() const && {
627 CHECK(storage_.is_populated_);
628 return std::move(storage_.value_);
632 constexpr T value_or(U&& default_value) const& {
633 // TODO(mlamouri): add the following assert when possible:
634 // static_assert(std::is_copy_constructible<T>::value,
635 // "T must be copy constructible");
636 static_assert(std::is_convertible<U, T>::value,
637 "U must be convertible to T");
638 return storage_.is_populated_
640 : static_cast<T>(std::forward<U>(default_value));
644 constexpr T value_or(U&& default_value) && {
645 // TODO(mlamouri): add the following assert when possible:
646 // static_assert(std::is_move_constructible<T>::value,
647 // "T must be move constructible");
648 static_assert(std::is_convertible<U, T>::value,
649 "U must be convertible to T");
650 return storage_.is_populated_
651 ? std::move(storage_.value_)
652 : static_cast<T>(std::forward<U>(default_value));
655 void swap(Optional& other) {
656 if (!storage_.is_populated_ && !other.storage_.is_populated_)
659 if (storage_.is_populated_ != other.storage_.is_populated_) {
660 if (storage_.is_populated_) {
661 other.storage_.Init(std::move(storage_.value_));
664 storage_.Init(std::move(other.storage_.value_));
665 other.FreeIfNeeded();
670 DCHECK(storage_.is_populated_ && other.storage_.is_populated_);
672 swap(**this, *other);
675 void reset() { FreeIfNeeded(); }
677 template <class... Args>
678 T& emplace(Args&&... args) {
680 storage_.Init(std::forward<Args>(args)...);
681 return storage_.value_;
684 template <class U, class... Args>
686 std::is_constructible<T, std::initializer_list<U>&, Args&&...>::value,
688 emplace(std::initializer_list<U> il, Args&&... args) {
690 storage_.Init(il, std::forward<Args>(args)...);
691 return storage_.value_;
695 // Accessing template base class's protected member needs explicit
696 // declaration to do so.
697 using internal::OptionalBase<T>::CopyAssign;
698 using internal::OptionalBase<T>::FreeIfNeeded;
699 using internal::OptionalBase<T>::InitOrAssign;
700 using internal::OptionalBase<T>::MoveAssign;
701 using internal::OptionalBase<T>::storage_;
704 // Here after defines comparation operators. The definition follows
705 // http://en.cppreference.com/w/cpp/utility/optional/operator_cmp
706 // while bool() casting is replaced by has_value() to meet the chromium
708 template <class T, class U>
709 constexpr bool operator==(const Optional<T>& lhs, const Optional<U>& rhs) {
710 if (lhs.has_value() != rhs.has_value())
712 if (!lhs.has_value())
717 template <class T, class U>
718 constexpr bool operator!=(const Optional<T>& lhs, const Optional<U>& rhs) {
719 if (lhs.has_value() != rhs.has_value())
721 if (!lhs.has_value())
726 template <class T, class U>
727 constexpr bool operator<(const Optional<T>& lhs, const Optional<U>& rhs) {
728 if (!rhs.has_value())
730 if (!lhs.has_value())
735 template <class T, class U>
736 constexpr bool operator<=(const Optional<T>& lhs, const Optional<U>& rhs) {
737 if (!lhs.has_value())
739 if (!rhs.has_value())
744 template <class T, class U>
745 constexpr bool operator>(const Optional<T>& lhs, const Optional<U>& rhs) {
746 if (!lhs.has_value())
748 if (!rhs.has_value())
753 template <class T, class U>
754 constexpr bool operator>=(const Optional<T>& lhs, const Optional<U>& rhs) {
755 if (!rhs.has_value())
757 if (!lhs.has_value())
763 constexpr bool operator==(const Optional<T>& opt, nullopt_t) {
768 constexpr bool operator==(nullopt_t, const Optional<T>& opt) {
773 constexpr bool operator!=(const Optional<T>& opt, nullopt_t) {
774 return opt.has_value();
778 constexpr bool operator!=(nullopt_t, const Optional<T>& opt) {
779 return opt.has_value();
783 constexpr bool operator<(const Optional<T>& opt, nullopt_t) {
788 constexpr bool operator<(nullopt_t, const Optional<T>& opt) {
789 return opt.has_value();
793 constexpr bool operator<=(const Optional<T>& opt, nullopt_t) {
798 constexpr bool operator<=(nullopt_t, const Optional<T>& opt) {
803 constexpr bool operator>(const Optional<T>& opt, nullopt_t) {
804 return opt.has_value();
808 constexpr bool operator>(nullopt_t, const Optional<T>& opt) {
813 constexpr bool operator>=(const Optional<T>& opt, nullopt_t) {
818 constexpr bool operator>=(nullopt_t, const Optional<T>& opt) {
822 template <class T, class U>
823 constexpr bool operator==(const Optional<T>& opt, const U& value) {
824 return opt.has_value() ? *opt == value : false;
827 template <class T, class U>
828 constexpr bool operator==(const U& value, const Optional<T>& opt) {
829 return opt.has_value() ? value == *opt : false;
832 template <class T, class U>
833 constexpr bool operator!=(const Optional<T>& opt, const U& value) {
834 return opt.has_value() ? *opt != value : true;
837 template <class T, class U>
838 constexpr bool operator!=(const U& value, const Optional<T>& opt) {
839 return opt.has_value() ? value != *opt : true;
842 template <class T, class U>
843 constexpr bool operator<(const Optional<T>& opt, const U& value) {
844 return opt.has_value() ? *opt < value : true;
847 template <class T, class U>
848 constexpr bool operator<(const U& value, const Optional<T>& opt) {
849 return opt.has_value() ? value < *opt : false;
852 template <class T, class U>
853 constexpr bool operator<=(const Optional<T>& opt, const U& value) {
854 return opt.has_value() ? *opt <= value : true;
857 template <class T, class U>
858 constexpr bool operator<=(const U& value, const Optional<T>& opt) {
859 return opt.has_value() ? value <= *opt : false;
862 template <class T, class U>
863 constexpr bool operator>(const Optional<T>& opt, const U& value) {
864 return opt.has_value() ? *opt > value : false;
867 template <class T, class U>
868 constexpr bool operator>(const U& value, const Optional<T>& opt) {
869 return opt.has_value() ? value > *opt : true;
872 template <class T, class U>
873 constexpr bool operator>=(const Optional<T>& opt, const U& value) {
874 return opt.has_value() ? *opt >= value : false;
877 template <class T, class U>
878 constexpr bool operator>=(const U& value, const Optional<T>& opt) {
879 return opt.has_value() ? value >= *opt : true;
883 constexpr Optional<std::decay_t<T>> make_optional(T&& value) {
884 return Optional<std::decay_t<T>>(std::forward<T>(value));
887 template <class T, class... Args>
888 constexpr Optional<T> make_optional(Args&&... args) {
889 return Optional<T>(in_place, std::forward<Args>(args)...);
892 template <class T, class U, class... Args>
893 constexpr Optional<T> make_optional(std::initializer_list<U> il,
895 return Optional<T>(in_place, il, std::forward<Args>(args)...);
898 // Partial specialization for a function template is not allowed. Also, it is
899 // not allowed to add overload function to std namespace, while it is allowed
900 // to specialize the template in std. Thus, swap() (kind of) overloading is
901 // defined in base namespace, instead.
903 std::enable_if_t<std::is_move_constructible<T>::value &&
904 internal::IsSwappable<T>::value>
905 swap(Optional<T>& lhs, Optional<T>& rhs) {
914 struct hash<base::Optional<T>> {
915 size_t operator()(const base::Optional<T>& opt) const {
916 return opt == base::nullopt ? 0 : std::hash<T>()(*opt);
922 #endif // BASE_OPTIONAL_H_