1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // -----------------------------------------------------------------------------
16 // optional.h
17 // -----------------------------------------------------------------------------
18 //
19 // This header file defines the `absl::optional` type for holding a value which
20 // may or may not be present. This type is useful for providing value semantics
21 // for operations that may either wish to return or hold "something-or-nothing".
22 //
23 // Example:
24 //
25 // // A common way to signal operation failure is to provide an output
26 // // parameter and a bool return type:
27 // bool AcquireResource(const Input&, Resource * out);
28 //
29 // // Providing an absl::optional return type provides a cleaner API:
30 // absl::optional<Resource> AcquireResource(const Input&);
31 //
32 // `absl::optional` is a C++11 compatible version of the C++17 `std::optional`
33 // abstraction and is designed to be a drop-in replacement for code compliant
34 // with C++17.
35 #ifndef ABSL_TYPES_OPTIONAL_H_
36 #define ABSL_TYPES_OPTIONAL_H_
37
38 #include "absl/base/config.h" // TODO(calabrese) IWYU removal?
39 #include "absl/utility/utility.h"
40
41 #ifdef ABSL_USES_STD_OPTIONAL
42
43 #include <optional> // IWYU pragma: export
44
45 namespace absl {
46 ABSL_NAMESPACE_BEGIN
47 using std::bad_optional_access;
48 using std::optional;
49 using std::make_optional;
50 using std::nullopt_t;
51 using std::nullopt;
52 ABSL_NAMESPACE_END
53 } // namespace absl
54
55 #else // ABSL_USES_STD_OPTIONAL
56
57 #include <cassert>
58 #include <functional>
59 #include <initializer_list>
60 #include <type_traits>
61 #include <utility>
62
63 #include "absl/base/attributes.h"
64 #include "absl/base/internal/inline_variable.h"
65 #include "absl/meta/type_traits.h"
66 #include "absl/types/bad_optional_access.h"
67 #include "absl/types/internal/optional.h"
68
69 namespace absl {
70 ABSL_NAMESPACE_BEGIN
71
72 // nullopt_t
73 //
74 // Class type for `absl::nullopt` used to indicate an `absl::optional<T>` type
75 // that does not contain a value.
76 struct nullopt_t {
77 // It must not be default-constructible to avoid ambiguity for opt = {}.
nullopt_tnullopt_t78 explicit constexpr nullopt_t(optional_internal::init_t) noexcept {}
79 };
80
81 // nullopt
82 //
83 // A tag constant of type `absl::nullopt_t` used to indicate an empty
84 // `absl::optional` in certain functions, such as construction or assignment.
85 ABSL_INTERNAL_INLINE_CONSTEXPR(nullopt_t, nullopt,
86 nullopt_t(optional_internal::init_t()));
87
88 // -----------------------------------------------------------------------------
89 // absl::optional
90 // -----------------------------------------------------------------------------
91 //
92 // A value of type `absl::optional<T>` holds either a value of `T` or an
93 // "empty" value. When it holds a value of `T`, it stores it as a direct
94 // sub-object, so `sizeof(optional<T>)` is approximately
95 // `sizeof(T) + sizeof(bool)`.
96 //
97 // This implementation is based on the specification in the latest draft of the
98 // C++17 `std::optional` specification as of May 2017, section 20.6.
99 //
100 // Differences between `absl::optional<T>` and `std::optional<T>` include:
101 //
102 // * `constexpr` is not used for non-const member functions.
103 // (dependency on some differences between C++11 and C++14.)
104 // * `absl::nullopt` and `absl::in_place` are not declared `constexpr`. We
105 // need the inline variable support in C++17 for external linkage.
106 // * Throws `absl::bad_optional_access` instead of
107 // `std::bad_optional_access`.
108 // * `make_optional()` cannot be declared `constexpr` due to the absence of
109 // guaranteed copy elision.
110 // * The move constructor's `noexcept` specification is stronger, i.e. if the
111 // default allocator is non-throwing (via setting
112 // `ABSL_ALLOCATOR_NOTHROW`), it evaluates to `noexcept(true)`, because
113 // we assume
114 // a) move constructors should only throw due to allocation failure and
115 // b) if T's move constructor allocates, it uses the same allocation
116 // function as the default allocator.
117 //
118 template <typename T>
119 class optional : private optional_internal::optional_data<T>,
120 private optional_internal::optional_ctor_base<
121 optional_internal::ctor_copy_traits<T>::traits>,
122 private optional_internal::optional_assign_base<
123 optional_internal::assign_copy_traits<T>::traits> {
124 using data_base = optional_internal::optional_data<T>;
125
126 public:
127 typedef T value_type;
128
129 // Constructors
130
131 // Constructs an `optional` holding an empty value, NOT a default constructed
132 // `T`.
optional()133 constexpr optional() noexcept {}
134
135 // Constructs an `optional` initialized with `nullopt` to hold an empty value.
optional(nullopt_t)136 constexpr optional(nullopt_t) noexcept {} // NOLINT(runtime/explicit)
137
138 // Copy constructor, standard semantics
139 optional(const optional&) = default;
140
141 // Move constructor, standard semantics
142 optional(optional&&) = default;
143
144 // Constructs a non-empty `optional` direct-initialized value of type `T` from
145 // the arguments `std::forward<Args>(args)...` within the `optional`.
146 // (The `in_place_t` is a tag used to indicate that the contained object
147 // should be constructed in-place.)
148 template <typename InPlaceT, typename... Args,
149 absl::enable_if_t<absl::conjunction<
150 std::is_same<InPlaceT, in_place_t>,
151 std::is_constructible<T, Args&&...> >::value>* = nullptr>
optional(InPlaceT,Args &&...args)152 constexpr explicit optional(InPlaceT, Args&&... args)
153 : data_base(in_place_t(), absl::forward<Args>(args)...) {}
154
155 // Constructs a non-empty `optional` direct-initialized value of type `T` from
156 // the arguments of an initializer_list and `std::forward<Args>(args)...`.
157 // (The `in_place_t` is a tag used to indicate that the contained object
158 // should be constructed in-place.)
159 template <typename U, typename... Args,
160 typename = typename std::enable_if<std::is_constructible<
161 T, std::initializer_list<U>&, Args&&...>::value>::type>
optional(in_place_t,std::initializer_list<U> il,Args &&...args)162 constexpr explicit optional(in_place_t, std::initializer_list<U> il,
163 Args&&... args)
164 : data_base(in_place_t(), il, absl::forward<Args>(args)...) {
165 }
166
167 // Value constructor (implicit)
168 template <
169 typename U = T,
170 typename std::enable_if<
171 absl::conjunction<absl::negation<std::is_same<
172 in_place_t, typename std::decay<U>::type> >,
173 absl::negation<std::is_same<
174 optional<T>, typename std::decay<U>::type> >,
175 std::is_convertible<U&&, T>,
176 std::is_constructible<T, U&&> >::value,
177 bool>::type = false>
optional(U && v)178 constexpr optional(U&& v) : data_base(in_place_t(), absl::forward<U>(v)) {}
179
180 // Value constructor (explicit)
181 template <
182 typename U = T,
183 typename std::enable_if<
184 absl::conjunction<absl::negation<std::is_same<
185 in_place_t, typename std::decay<U>::type>>,
186 absl::negation<std::is_same<
187 optional<T>, typename std::decay<U>::type>>,
188 absl::negation<std::is_convertible<U&&, T>>,
189 std::is_constructible<T, U&&>>::value,
190 bool>::type = false>
optional(U && v)191 explicit constexpr optional(U&& v)
192 : data_base(in_place_t(), absl::forward<U>(v)) {}
193
194 // Converting copy constructor (implicit)
195 template <typename U,
196 typename std::enable_if<
197 absl::conjunction<
198 absl::negation<std::is_same<T, U> >,
199 std::is_constructible<T, const U&>,
200 absl::negation<
201 optional_internal::
202 is_constructible_convertible_from_optional<T, U> >,
203 std::is_convertible<const U&, T> >::value,
204 bool>::type = false>
optional(const optional<U> & rhs)205 optional(const optional<U>& rhs) {
206 if (rhs) {
207 this->construct(*rhs);
208 }
209 }
210
211 // Converting copy constructor (explicit)
212 template <typename U,
213 typename std::enable_if<
214 absl::conjunction<
215 absl::negation<std::is_same<T, U>>,
216 std::is_constructible<T, const U&>,
217 absl::negation<
218 optional_internal::
219 is_constructible_convertible_from_optional<T, U>>,
220 absl::negation<std::is_convertible<const U&, T>>>::value,
221 bool>::type = false>
optional(const optional<U> & rhs)222 explicit optional(const optional<U>& rhs) {
223 if (rhs) {
224 this->construct(*rhs);
225 }
226 }
227
228 // Converting move constructor (implicit)
229 template <typename U,
230 typename std::enable_if<
231 absl::conjunction<
232 absl::negation<std::is_same<T, U> >,
233 std::is_constructible<T, U&&>,
234 absl::negation<
235 optional_internal::
236 is_constructible_convertible_from_optional<T, U> >,
237 std::is_convertible<U&&, T> >::value,
238 bool>::type = false>
optional(optional<U> && rhs)239 optional(optional<U>&& rhs) {
240 if (rhs) {
241 this->construct(std::move(*rhs));
242 }
243 }
244
245 // Converting move constructor (explicit)
246 template <
247 typename U,
248 typename std::enable_if<
249 absl::conjunction<
250 absl::negation<std::is_same<T, U>>, std::is_constructible<T, U&&>,
251 absl::negation<
252 optional_internal::is_constructible_convertible_from_optional<
253 T, U>>,
254 absl::negation<std::is_convertible<U&&, T>>>::value,
255 bool>::type = false>
optional(optional<U> && rhs)256 explicit optional(optional<U>&& rhs) {
257 if (rhs) {
258 this->construct(std::move(*rhs));
259 }
260 }
261
262 // Destructor. Trivial if `T` is trivially destructible.
263 ~optional() = default;
264
265 // Assignment Operators
266
267 // Assignment from `nullopt`
268 //
269 // Example:
270 //
271 // struct S { int value; };
272 // optional<S> opt = absl::nullopt; // Could also use opt = { };
273 optional& operator=(nullopt_t) noexcept {
274 this->destruct();
275 return *this;
276 }
277
278 // Copy assignment operator, standard semantics
279 optional& operator=(const optional& src) = default;
280
281 // Move assignment operator, standard semantics
282 optional& operator=(optional&& src) = default;
283
284 // Value assignment operators
285 template <
286 typename U = T,
287 typename = typename std::enable_if<absl::conjunction<
288 absl::negation<
289 std::is_same<optional<T>, typename std::decay<U>::type>>,
290 absl::negation<
291 absl::conjunction<std::is_scalar<T>,
292 std::is_same<T, typename std::decay<U>::type>>>,
293 std::is_constructible<T, U>, std::is_assignable<T&, U>>::value>::type>
294 optional& operator=(U&& v) {
295 this->assign(std::forward<U>(v));
296 return *this;
297 }
298
299 template <
300 typename U,
301 typename = typename std::enable_if<absl::conjunction<
302 absl::negation<std::is_same<T, U>>,
303 std::is_constructible<T, const U&>, std::is_assignable<T&, const U&>,
304 absl::negation<
305 optional_internal::
306 is_constructible_convertible_assignable_from_optional<
307 T, U>>>::value>::type>
308 optional& operator=(const optional<U>& rhs) {
309 if (rhs) {
310 this->assign(*rhs);
311 } else {
312 this->destruct();
313 }
314 return *this;
315 }
316
317 template <typename U,
318 typename = typename std::enable_if<absl::conjunction<
319 absl::negation<std::is_same<T, U>>, std::is_constructible<T, U>,
320 std::is_assignable<T&, U>,
321 absl::negation<
322 optional_internal::
323 is_constructible_convertible_assignable_from_optional<
324 T, U>>>::value>::type>
325 optional& operator=(optional<U>&& rhs) {
326 if (rhs) {
327 this->assign(std::move(*rhs));
328 } else {
329 this->destruct();
330 }
331 return *this;
332 }
333
334 // Modifiers
335
336 // optional::reset()
337 //
338 // Destroys the inner `T` value of an `absl::optional` if one is present.
reset()339 ABSL_ATTRIBUTE_REINITIALIZES void reset() noexcept { this->destruct(); }
340
341 // optional::emplace()
342 //
343 // (Re)constructs the underlying `T` in-place with the given forwarded
344 // arguments.
345 //
346 // Example:
347 //
348 // optional<Foo> opt;
349 // opt.emplace(arg1,arg2,arg3); // Constructs Foo(arg1,arg2,arg3)
350 //
351 // If the optional is non-empty, and the `args` refer to subobjects of the
352 // current object, then behaviour is undefined, because the current object
353 // will be destructed before the new object is constructed with `args`.
354 template <typename... Args,
355 typename = typename std::enable_if<
356 std::is_constructible<T, Args&&...>::value>::type>
emplace(Args &&...args)357 T& emplace(Args&&... args) {
358 this->destruct();
359 this->construct(std::forward<Args>(args)...);
360 return reference();
361 }
362
363 // Emplace reconstruction overload for an initializer list and the given
364 // forwarded arguments.
365 //
366 // Example:
367 //
368 // struct Foo {
369 // Foo(std::initializer_list<int>);
370 // };
371 //
372 // optional<Foo> opt;
373 // opt.emplace({1,2,3}); // Constructs Foo({1,2,3})
374 template <typename U, typename... Args,
375 typename = typename std::enable_if<std::is_constructible<
376 T, std::initializer_list<U>&, Args&&...>::value>::type>
emplace(std::initializer_list<U> il,Args &&...args)377 T& emplace(std::initializer_list<U> il, Args&&... args) {
378 this->destruct();
379 this->construct(il, std::forward<Args>(args)...);
380 return reference();
381 }
382
383 // Swaps
384
385 // Swap, standard semantics
swap(optional & rhs)386 void swap(optional& rhs) noexcept(
387 std::is_nothrow_move_constructible<T>::value&&
388 type_traits_internal::IsNothrowSwappable<T>::value) {
389 if (*this) {
390 if (rhs) {
391 type_traits_internal::Swap(**this, *rhs);
392 } else {
393 rhs.construct(std::move(**this));
394 this->destruct();
395 }
396 } else {
397 if (rhs) {
398 this->construct(std::move(*rhs));
399 rhs.destruct();
400 } else {
401 // No effect (swap(disengaged, disengaged)).
402 }
403 }
404 }
405
406 // Observers
407
408 // optional::operator->()
409 //
410 // Accesses the underlying `T` value's member `m` of an `optional`. If the
411 // `optional` is empty, behavior is undefined.
412 //
413 // If you need myOpt->foo in constexpr, use (*myOpt).foo instead.
414 const T* operator->() const {
415 ABSL_HARDENING_ASSERT(this->engaged_);
416 return std::addressof(this->data_);
417 }
418 T* operator->() {
419 ABSL_HARDENING_ASSERT(this->engaged_);
420 return std::addressof(this->data_);
421 }
422
423 // optional::operator*()
424 //
425 // Accesses the underlying `T` value of an `optional`. If the `optional` is
426 // empty, behavior is undefined.
427 constexpr const T& operator*() const& {
428 return ABSL_HARDENING_ASSERT(this->engaged_), reference();
429 }
430 T& operator*() & {
431 ABSL_HARDENING_ASSERT(this->engaged_);
432 return reference();
433 }
434 constexpr const T&& operator*() const && {
435 return ABSL_HARDENING_ASSERT(this->engaged_), absl::move(reference());
436 }
437 T&& operator*() && {
438 ABSL_HARDENING_ASSERT(this->engaged_);
439 return std::move(reference());
440 }
441
442 // optional::operator bool()
443 //
444 // Returns false if and only if the `optional` is empty.
445 //
446 // if (opt) {
447 // // do something with *opt or opt->;
448 // } else {
449 // // opt is empty.
450 // }
451 //
452 constexpr explicit operator bool() const noexcept { return this->engaged_; }
453
454 // optional::has_value()
455 //
456 // Determines whether the `optional` contains a value. Returns `false` if and
457 // only if `*this` is empty.
has_value()458 constexpr bool has_value() const noexcept { return this->engaged_; }
459
460 // Suppress bogus warning on MSVC: MSVC complains call to reference() after
461 // throw_bad_optional_access() is unreachable.
462 #ifdef _MSC_VER
463 #pragma warning(push)
464 #pragma warning(disable : 4702)
465 #endif // _MSC_VER
466 // optional::value()
467 //
468 // Returns a reference to an `optional`s underlying value. The constness
469 // and lvalue/rvalue-ness of the `optional` is preserved to the view of
470 // the `T` sub-object. Throws `absl::bad_optional_access` when the `optional`
471 // is empty.
value()472 constexpr const T& value() const & {
473 return static_cast<bool>(*this)
474 ? reference()
475 : (optional_internal::throw_bad_optional_access(), reference());
476 }
value()477 T& value() & {
478 return static_cast<bool>(*this)
479 ? reference()
480 : (optional_internal::throw_bad_optional_access(), reference());
481 }
value()482 T&& value() && { // NOLINT(build/c++11)
483 return std::move(
484 static_cast<bool>(*this)
485 ? reference()
486 : (optional_internal::throw_bad_optional_access(), reference()));
487 }
value()488 constexpr const T&& value() const && { // NOLINT(build/c++11)
489 return absl::move(
490 static_cast<bool>(*this)
491 ? reference()
492 : (optional_internal::throw_bad_optional_access(), reference()));
493 }
494 #ifdef _MSC_VER
495 #pragma warning(pop)
496 #endif // _MSC_VER
497
498 // optional::value_or()
499 //
500 // Returns either the value of `T` or a passed default `v` if the `optional`
501 // is empty.
502 template <typename U>
value_or(U && v)503 constexpr T value_or(U&& v) const& {
504 static_assert(std::is_copy_constructible<value_type>::value,
505 "optional<T>::value_or: T must be copy constructible");
506 static_assert(std::is_convertible<U&&, value_type>::value,
507 "optional<T>::value_or: U must be convertible to T");
508 return static_cast<bool>(*this)
509 ? **this
510 : static_cast<T>(absl::forward<U>(v));
511 }
512 template <typename U>
value_or(U && v)513 T value_or(U&& v) && { // NOLINT(build/c++11)
514 static_assert(std::is_move_constructible<value_type>::value,
515 "optional<T>::value_or: T must be move constructible");
516 static_assert(std::is_convertible<U&&, value_type>::value,
517 "optional<T>::value_or: U must be convertible to T");
518 return static_cast<bool>(*this) ? std::move(**this)
519 : static_cast<T>(std::forward<U>(v));
520 }
521
522 private:
523 // Private accessors for internal storage viewed as reference to T.
reference()524 constexpr const T& reference() const { return this->data_; }
reference()525 T& reference() { return this->data_; }
526
527 // T constraint checks. You can't have an optional of nullopt_t, in_place_t
528 // or a reference.
529 static_assert(
530 !std::is_same<nullopt_t, typename std::remove_cv<T>::type>::value,
531 "optional<nullopt_t> is not allowed.");
532 static_assert(
533 !std::is_same<in_place_t, typename std::remove_cv<T>::type>::value,
534 "optional<in_place_t> is not allowed.");
535 static_assert(!std::is_reference<T>::value,
536 "optional<reference> is not allowed.");
537 };
538
539 // Non-member functions
540
541 // swap()
542 //
543 // Performs a swap between two `absl::optional` objects, using standard
544 // semantics.
545 template <typename T, typename std::enable_if<
546 std::is_move_constructible<T>::value &&
547 type_traits_internal::IsSwappable<T>::value,
548 bool>::type = false>
swap(optional<T> & a,optional<T> & b)549 void swap(optional<T>& a, optional<T>& b) noexcept(noexcept(a.swap(b))) {
550 a.swap(b);
551 }
552
553 // make_optional()
554 //
555 // Creates a non-empty `optional<T>` where the type of `T` is deduced. An
556 // `absl::optional` can also be explicitly instantiated with
557 // `make_optional<T>(v)`.
558 //
559 // Note: `make_optional()` constructions may be declared `constexpr` for
560 // trivially copyable types `T`. Non-trivial types require copy elision
561 // support in C++17 for `make_optional` to support `constexpr` on such
562 // non-trivial types.
563 //
564 // Example:
565 //
566 // constexpr absl::optional<int> opt = absl::make_optional(1);
567 // static_assert(opt.value() == 1, "");
568 template <typename T>
make_optional(T && v)569 constexpr optional<typename std::decay<T>::type> make_optional(T&& v) {
570 return optional<typename std::decay<T>::type>(absl::forward<T>(v));
571 }
572
573 template <typename T, typename... Args>
make_optional(Args &&...args)574 constexpr optional<T> make_optional(Args&&... args) {
575 return optional<T>(in_place_t(), absl::forward<Args>(args)...);
576 }
577
578 template <typename T, typename U, typename... Args>
make_optional(std::initializer_list<U> il,Args &&...args)579 constexpr optional<T> make_optional(std::initializer_list<U> il,
580 Args&&... args) {
581 return optional<T>(in_place_t(), il,
582 absl::forward<Args>(args)...);
583 }
584
585 // Relational operators [optional.relops]
586
587 // Empty optionals are considered equal to each other and less than non-empty
588 // optionals. Supports relations between optional<T> and optional<U>, between
589 // optional<T> and U, and between optional<T> and nullopt.
590 //
591 // Note: We're careful to support T having non-bool relationals.
592
593 // Requires: The expression, e.g. "*x == *y" shall be well-formed and its result
594 // shall be convertible to bool.
595 // The C++17 (N4606) "Returns:" statements are translated into
596 // code in an obvious way here, and the original text retained as function docs.
597 // Returns: If bool(x) != bool(y), false; otherwise if bool(x) == false, true;
598 // otherwise *x == *y.
599 template <typename T, typename U>
600 constexpr auto operator==(const optional<T>& x, const optional<U>& y)
601 -> decltype(optional_internal::convertible_to_bool(*x == *y)) {
602 return static_cast<bool>(x) != static_cast<bool>(y)
603 ? false
604 : static_cast<bool>(x) == false ? true
605 : static_cast<bool>(*x == *y);
606 }
607
608 // Returns: If bool(x) != bool(y), true; otherwise, if bool(x) == false, false;
609 // otherwise *x != *y.
610 template <typename T, typename U>
611 constexpr auto operator!=(const optional<T>& x, const optional<U>& y)
612 -> decltype(optional_internal::convertible_to_bool(*x != *y)) {
613 return static_cast<bool>(x) != static_cast<bool>(y)
614 ? true
615 : static_cast<bool>(x) == false ? false
616 : static_cast<bool>(*x != *y);
617 }
618 // Returns: If !y, false; otherwise, if !x, true; otherwise *x < *y.
619 template <typename T, typename U>
620 constexpr auto operator<(const optional<T>& x, const optional<U>& y)
621 -> decltype(optional_internal::convertible_to_bool(*x < *y)) {
622 return !y ? false : !x ? true : static_cast<bool>(*x < *y);
623 }
624 // Returns: If !x, false; otherwise, if !y, true; otherwise *x > *y.
625 template <typename T, typename U>
626 constexpr auto operator>(const optional<T>& x, const optional<U>& y)
627 -> decltype(optional_internal::convertible_to_bool(*x > *y)) {
628 return !x ? false : !y ? true : static_cast<bool>(*x > *y);
629 }
630 // Returns: If !x, true; otherwise, if !y, false; otherwise *x <= *y.
631 template <typename T, typename U>
632 constexpr auto operator<=(const optional<T>& x, const optional<U>& y)
633 -> decltype(optional_internal::convertible_to_bool(*x <= *y)) {
634 return !x ? true : !y ? false : static_cast<bool>(*x <= *y);
635 }
636 // Returns: If !y, true; otherwise, if !x, false; otherwise *x >= *y.
637 template <typename T, typename U>
638 constexpr auto operator>=(const optional<T>& x, const optional<U>& y)
639 -> decltype(optional_internal::convertible_to_bool(*x >= *y)) {
640 return !y ? true : !x ? false : static_cast<bool>(*x >= *y);
641 }
642
643 // Comparison with nullopt [optional.nullops]
644 // The C++17 (N4606) "Returns:" statements are used directly here.
645 template <typename T>
646 constexpr bool operator==(const optional<T>& x, nullopt_t) noexcept {
647 return !x;
648 }
649 template <typename T>
650 constexpr bool operator==(nullopt_t, const optional<T>& x) noexcept {
651 return !x;
652 }
653 template <typename T>
654 constexpr bool operator!=(const optional<T>& x, nullopt_t) noexcept {
655 return static_cast<bool>(x);
656 }
657 template <typename T>
658 constexpr bool operator!=(nullopt_t, const optional<T>& x) noexcept {
659 return static_cast<bool>(x);
660 }
661 template <typename T>
662 constexpr bool operator<(const optional<T>&, nullopt_t) noexcept {
663 return false;
664 }
665 template <typename T>
666 constexpr bool operator<(nullopt_t, const optional<T>& x) noexcept {
667 return static_cast<bool>(x);
668 }
669 template <typename T>
670 constexpr bool operator<=(const optional<T>& x, nullopt_t) noexcept {
671 return !x;
672 }
673 template <typename T>
674 constexpr bool operator<=(nullopt_t, const optional<T>&) noexcept {
675 return true;
676 }
677 template <typename T>
678 constexpr bool operator>(const optional<T>& x, nullopt_t) noexcept {
679 return static_cast<bool>(x);
680 }
681 template <typename T>
682 constexpr bool operator>(nullopt_t, const optional<T>&) noexcept {
683 return false;
684 }
685 template <typename T>
686 constexpr bool operator>=(const optional<T>&, nullopt_t) noexcept {
687 return true;
688 }
689 template <typename T>
690 constexpr bool operator>=(nullopt_t, const optional<T>& x) noexcept {
691 return !x;
692 }
693
694 // Comparison with T [optional.comp_with_t]
695
696 // Requires: The expression, e.g. "*x == v" shall be well-formed and its result
697 // shall be convertible to bool.
698 // The C++17 (N4606) "Equivalent to:" statements are used directly here.
699 template <typename T, typename U>
700 constexpr auto operator==(const optional<T>& x, const U& v)
701 -> decltype(optional_internal::convertible_to_bool(*x == v)) {
702 return static_cast<bool>(x) ? static_cast<bool>(*x == v) : false;
703 }
704 template <typename T, typename U>
705 constexpr auto operator==(const U& v, const optional<T>& x)
706 -> decltype(optional_internal::convertible_to_bool(v == *x)) {
707 return static_cast<bool>(x) ? static_cast<bool>(v == *x) : false;
708 }
709 template <typename T, typename U>
710 constexpr auto operator!=(const optional<T>& x, const U& v)
711 -> decltype(optional_internal::convertible_to_bool(*x != v)) {
712 return static_cast<bool>(x) ? static_cast<bool>(*x != v) : true;
713 }
714 template <typename T, typename U>
715 constexpr auto operator!=(const U& v, const optional<T>& x)
716 -> decltype(optional_internal::convertible_to_bool(v != *x)) {
717 return static_cast<bool>(x) ? static_cast<bool>(v != *x) : true;
718 }
719 template <typename T, typename U>
720 constexpr auto operator<(const optional<T>& x, const U& v)
721 -> decltype(optional_internal::convertible_to_bool(*x < v)) {
722 return static_cast<bool>(x) ? static_cast<bool>(*x < v) : true;
723 }
724 template <typename T, typename U>
725 constexpr auto operator<(const U& v, const optional<T>& x)
726 -> decltype(optional_internal::convertible_to_bool(v < *x)) {
727 return static_cast<bool>(x) ? static_cast<bool>(v < *x) : false;
728 }
729 template <typename T, typename U>
730 constexpr auto operator<=(const optional<T>& x, const U& v)
731 -> decltype(optional_internal::convertible_to_bool(*x <= v)) {
732 return static_cast<bool>(x) ? static_cast<bool>(*x <= v) : true;
733 }
734 template <typename T, typename U>
735 constexpr auto operator<=(const U& v, const optional<T>& x)
736 -> decltype(optional_internal::convertible_to_bool(v <= *x)) {
737 return static_cast<bool>(x) ? static_cast<bool>(v <= *x) : false;
738 }
739 template <typename T, typename U>
740 constexpr auto operator>(const optional<T>& x, const U& v)
741 -> decltype(optional_internal::convertible_to_bool(*x > v)) {
742 return static_cast<bool>(x) ? static_cast<bool>(*x > v) : false;
743 }
744 template <typename T, typename U>
745 constexpr auto operator>(const U& v, const optional<T>& x)
746 -> decltype(optional_internal::convertible_to_bool(v > *x)) {
747 return static_cast<bool>(x) ? static_cast<bool>(v > *x) : true;
748 }
749 template <typename T, typename U>
750 constexpr auto operator>=(const optional<T>& x, const U& v)
751 -> decltype(optional_internal::convertible_to_bool(*x >= v)) {
752 return static_cast<bool>(x) ? static_cast<bool>(*x >= v) : false;
753 }
754 template <typename T, typename U>
755 constexpr auto operator>=(const U& v, const optional<T>& x)
756 -> decltype(optional_internal::convertible_to_bool(v >= *x)) {
757 return static_cast<bool>(x) ? static_cast<bool>(v >= *x) : true;
758 }
759
760 ABSL_NAMESPACE_END
761 } // namespace absl
762
763 namespace std {
764
765 // std::hash specialization for absl::optional.
766 template <typename T>
767 struct hash<absl::optional<T> >
768 : absl::optional_internal::optional_hash_base<T> {};
769
770 } // namespace std
771
772 #undef ABSL_MSVC_CONSTEXPR_BUG_IN_UNION_LIKE_CLASS
773
774 #endif // ABSL_USES_STD_OPTIONAL
775
776 #endif // ABSL_TYPES_OPTIONAL_H_
777