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
45namespace absl {
46ABSL_NAMESPACE_BEGIN
47using std::bad_optional_access;
48using std::optional;
49using std::make_optional;
50using std::nullopt_t;
51using std::nullopt;
52ABSL_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
69namespace absl {
70ABSL_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.
76struct nullopt_t {
77 // It must not be default-constructible to avoid ambiguity for opt = {}.
78 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.
85ABSL_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//
118template <typename T>
119class 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`.
133 constexpr optional() noexcept {}
134
135 // Constructs an `optional` initialized with `nullopt` to hold an empty value.
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>
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>
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>
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>
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>
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>
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>
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>
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 <typename U = T,
286 int&..., // Workaround an internal compiler error in GCC 5 to 10.
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<absl::conjunction<
291 std::is_scalar<T>,
292 std::is_same<T, typename std::decay<U>::type> > >,
293 std::is_constructible<T, U>,
294 std::is_assignable<T&, U> >::value>::type>
295 optional& operator=(U&& v) {
296 this->assign(std::forward<U>(v));
297 return *this;
298 }
299
300 template <
301 typename U,
302 int&..., // Workaround an internal compiler error in GCC 5 to 10.
303 typename = typename std::enable_if<absl::conjunction<
304 absl::negation<std::is_same<T, U> >,
305 std::is_constructible<T, const U&>, std::is_assignable<T&, const U&>,
306 absl::negation<
307 optional_internal::
308 is_constructible_convertible_assignable_from_optional<
309 T, U> > >::value>::type>
310 optional& operator=(const optional<U>& rhs) {
311 if (rhs) {
312 this->assign(*rhs);
313 } else {
314 this->destruct();
315 }
316 return *this;
317 }
318
319 template <typename U,
320 int&..., // Workaround an internal compiler error in GCC 5 to 10.
321 typename = typename std::enable_if<absl::conjunction<
322 absl::negation<std::is_same<T, U> >,
323 std::is_constructible<T, U>, std::is_assignable<T&, U>,
324 absl::negation<
325 optional_internal::
326 is_constructible_convertible_assignable_from_optional<
327 T, U> > >::value>::type>
328 optional& operator=(optional<U>&& rhs) {
329 if (rhs) {
330 this->assign(std::move(*rhs));
331 } else {
332 this->destruct();
333 }
334 return *this;
335 }
336
337 // Modifiers
338
339 // optional::reset()
340 //
341 // Destroys the inner `T` value of an `absl::optional` if one is present.
342 ABSL_ATTRIBUTE_REINITIALIZES void reset() noexcept { this->destruct(); }
343
344 // optional::emplace()
345 //
346 // (Re)constructs the underlying `T` in-place with the given forwarded
347 // arguments.
348 //
349 // Example:
350 //
351 // optional<Foo> opt;
352 // opt.emplace(arg1,arg2,arg3); // Constructs Foo(arg1,arg2,arg3)
353 //
354 // If the optional is non-empty, and the `args` refer to subobjects of the
355 // current object, then behaviour is undefined, because the current object
356 // will be destructed before the new object is constructed with `args`.
357 template <typename... Args,
358 typename = typename std::enable_if<
359 std::is_constructible<T, Args&&...>::value>::type>
360 T& emplace(Args&&... args) {
361 this->destruct();
362 this->construct(std::forward<Args>(args)...);
363 return reference();
364 }
365
366 // Emplace reconstruction overload for an initializer list and the given
367 // forwarded arguments.
368 //
369 // Example:
370 //
371 // struct Foo {
372 // Foo(std::initializer_list<int>);
373 // };
374 //
375 // optional<Foo> opt;
376 // opt.emplace({1,2,3}); // Constructs Foo({1,2,3})
377 template <typename U, typename... Args,
378 typename = typename std::enable_if<std::is_constructible<
379 T, std::initializer_list<U>&, Args&&...>::value>::type>
380 T& emplace(std::initializer_list<U> il, Args&&... args) {
381 this->destruct();
382 this->construct(il, std::forward<Args>(args)...);
383 return reference();
384 }
385
386 // Swaps
387
388 // Swap, standard semantics
389 void swap(optional& rhs) noexcept(
390 std::is_nothrow_move_constructible<T>::value&&
391 type_traits_internal::IsNothrowSwappable<T>::value) {
392 if (*this) {
393 if (rhs) {
394 type_traits_internal::Swap(**this, *rhs);
395 } else {
396 rhs.construct(std::move(**this));
397 this->destruct();
398 }
399 } else {
400 if (rhs) {
401 this->construct(std::move(*rhs));
402 rhs.destruct();
403 } else {
404 // No effect (swap(disengaged, disengaged)).
405 }
406 }
407 }
408
409 // Observers
410
411 // optional::operator->()
412 //
413 // Accesses the underlying `T` value's member `m` of an `optional`. If the
414 // `optional` is empty, behavior is undefined.
415 //
416 // If you need myOpt->foo in constexpr, use (*myOpt).foo instead.
417 const T* operator->() const {
418 ABSL_HARDENING_ASSERT(this->engaged_);
419 return std::addressof(this->data_);
420 }
421 T* operator->() {
422 ABSL_HARDENING_ASSERT(this->engaged_);
423 return std::addressof(this->data_);
424 }
425
426 // optional::operator*()
427 //
428 // Accesses the underlying `T` value of an `optional`. If the `optional` is
429 // empty, behavior is undefined.
430 constexpr const T& operator*() const& {
431 return ABSL_HARDENING_ASSERT(this->engaged_), reference();
432 }
433 T& operator*() & {
434 ABSL_HARDENING_ASSERT(this->engaged_);
435 return reference();
436 }
437 constexpr const T&& operator*() const && {
438 return ABSL_HARDENING_ASSERT(this->engaged_), absl::move(reference());
439 }
440 T&& operator*() && {
441 ABSL_HARDENING_ASSERT(this->engaged_);
442 return std::move(reference());
443 }
444
445 // optional::operator bool()
446 //
447 // Returns false if and only if the `optional` is empty.
448 //
449 // if (opt) {
450 // // do something with *opt or opt->;
451 // } else {
452 // // opt is empty.
453 // }
454 //
455 constexpr explicit operator bool() const noexcept { return this->engaged_; }
456
457 // optional::has_value()
458 //
459 // Determines whether the `optional` contains a value. Returns `false` if and
460 // only if `*this` is empty.
461 constexpr bool has_value() const noexcept { return this->engaged_; }
462
463// Suppress bogus warning on MSVC: MSVC complains call to reference() after
464// throw_bad_optional_access() is unreachable.
465#ifdef _MSC_VER
466#pragma warning(push)
467#pragma warning(disable : 4702)
468#endif // _MSC_VER
469 // optional::value()
470 //
471 // Returns a reference to an `optional`s underlying value. The constness
472 // and lvalue/rvalue-ness of the `optional` is preserved to the view of
473 // the `T` sub-object. Throws `absl::bad_optional_access` when the `optional`
474 // is empty.
475 constexpr const T& value() const & {
476 return static_cast<bool>(*this)
477 ? reference()
478 : (optional_internal::throw_bad_optional_access(), reference());
479 }
480 T& value() & {
481 return static_cast<bool>(*this)
482 ? reference()
483 : (optional_internal::throw_bad_optional_access(), reference());
484 }
485 T&& value() && { // NOLINT(build/c++11)
486 return std::move(
487 static_cast<bool>(*this)
488 ? reference()
489 : (optional_internal::throw_bad_optional_access(), reference()));
490 }
491 constexpr const T&& value() const && { // NOLINT(build/c++11)
492 return absl::move(
493 static_cast<bool>(*this)
494 ? reference()
495 : (optional_internal::throw_bad_optional_access(), reference()));
496 }
497#ifdef _MSC_VER
498#pragma warning(pop)
499#endif // _MSC_VER
500
501 // optional::value_or()
502 //
503 // Returns either the value of `T` or a passed default `v` if the `optional`
504 // is empty.
505 template <typename U>
506 constexpr T value_or(U&& v) const& {
507 static_assert(std::is_copy_constructible<value_type>::value,
508 "optional<T>::value_or: T must be copy constructible");
509 static_assert(std::is_convertible<U&&, value_type>::value,
510 "optional<T>::value_or: U must be convertible to T");
511 return static_cast<bool>(*this)
512 ? **this
513 : static_cast<T>(absl::forward<U>(v));
514 }
515 template <typename U>
516 T value_or(U&& v) && { // NOLINT(build/c++11)
517 static_assert(std::is_move_constructible<value_type>::value,
518 "optional<T>::value_or: T must be move constructible");
519 static_assert(std::is_convertible<U&&, value_type>::value,
520 "optional<T>::value_or: U must be convertible to T");
521 return static_cast<bool>(*this) ? std::move(**this)
522 : static_cast<T>(std::forward<U>(v));
523 }
524
525 private:
526 // Private accessors for internal storage viewed as reference to T.
527 constexpr const T& reference() const { return this->data_; }
528 T& reference() { return this->data_; }
529
530 // T constraint checks. You can't have an optional of nullopt_t, in_place_t
531 // or a reference.
532 static_assert(
533 !std::is_same<nullopt_t, typename std::remove_cv<T>::type>::value,
534 "optional<nullopt_t> is not allowed.");
535 static_assert(
536 !std::is_same<in_place_t, typename std::remove_cv<T>::type>::value,
537 "optional<in_place_t> is not allowed.");
538 static_assert(!std::is_reference<T>::value,
539 "optional<reference> is not allowed.");
540};
541
542// Non-member functions
543
544// swap()
545//
546// Performs a swap between two `absl::optional` objects, using standard
547// semantics.
548template <typename T, typename std::enable_if<
549 std::is_move_constructible<T>::value &&
550 type_traits_internal::IsSwappable<T>::value,
551 bool>::type = false>
552void swap(optional<T>& a, optional<T>& b) noexcept(noexcept(a.swap(b))) {
553 a.swap(b);
554}
555
556// make_optional()
557//
558// Creates a non-empty `optional<T>` where the type of `T` is deduced. An
559// `absl::optional` can also be explicitly instantiated with
560// `make_optional<T>(v)`.
561//
562// Note: `make_optional()` constructions may be declared `constexpr` for
563// trivially copyable types `T`. Non-trivial types require copy elision
564// support in C++17 for `make_optional` to support `constexpr` on such
565// non-trivial types.
566//
567// Example:
568//
569// constexpr absl::optional<int> opt = absl::make_optional(1);
570// static_assert(opt.value() == 1, "");
571template <typename T>
572constexpr optional<typename std::decay<T>::type> make_optional(T&& v) {
573 return optional<typename std::decay<T>::type>(absl::forward<T>(v));
574}
575
576template <typename T, typename... Args>
577constexpr optional<T> make_optional(Args&&... args) {
578 return optional<T>(in_place_t(), absl::forward<Args>(args)...);
579}
580
581template <typename T, typename U, typename... Args>
582constexpr optional<T> make_optional(std::initializer_list<U> il,
583 Args&&... args) {
584 return optional<T>(in_place_t(), il,
585 absl::forward<Args>(args)...);
586}
587
588// Relational operators [optional.relops]
589
590// Empty optionals are considered equal to each other and less than non-empty
591// optionals. Supports relations between optional<T> and optional<U>, between
592// optional<T> and U, and between optional<T> and nullopt.
593//
594// Note: We're careful to support T having non-bool relationals.
595
596// Requires: The expression, e.g. "*x == *y" shall be well-formed and its result
597// shall be convertible to bool.
598// The C++17 (N4606) "Returns:" statements are translated into
599// code in an obvious way here, and the original text retained as function docs.
600// Returns: If bool(x) != bool(y), false; otherwise if bool(x) == false, true;
601// otherwise *x == *y.
602template <typename T, typename U>
603constexpr auto operator==(const optional<T>& x, const optional<U>& y)
604 -> decltype(optional_internal::convertible_to_bool(*x == *y)) {
605 return static_cast<bool>(x) != static_cast<bool>(y)
606 ? false
607 : static_cast<bool>(x) == false ? true
608 : static_cast<bool>(*x == *y);
609}
610
611// Returns: If bool(x) != bool(y), true; otherwise, if bool(x) == false, false;
612// otherwise *x != *y.
613template <typename T, typename U>
614constexpr auto operator!=(const optional<T>& x, const optional<U>& y)
615 -> decltype(optional_internal::convertible_to_bool(*x != *y)) {
616 return static_cast<bool>(x) != static_cast<bool>(y)
617 ? true
618 : static_cast<bool>(x) == false ? false
619 : static_cast<bool>(*x != *y);
620}
621// Returns: If !y, false; otherwise, if !x, true; otherwise *x < *y.
622template <typename T, typename U>
623constexpr auto operator<(const optional<T>& x, const optional<U>& y)
624 -> decltype(optional_internal::convertible_to_bool(*x < *y)) {
625 return !y ? false : !x ? true : static_cast<bool>(*x < *y);
626}
627// Returns: If !x, false; otherwise, if !y, true; otherwise *x > *y.
628template <typename T, typename U>
629constexpr auto operator>(const optional<T>& x, const optional<U>& y)
630 -> decltype(optional_internal::convertible_to_bool(*x > *y)) {
631 return !x ? false : !y ? true : static_cast<bool>(*x > *y);
632}
633// Returns: If !x, true; otherwise, if !y, false; otherwise *x <= *y.
634template <typename T, typename U>
635constexpr auto operator<=(const optional<T>& x, const optional<U>& y)
636 -> decltype(optional_internal::convertible_to_bool(*x <= *y)) {
637 return !x ? true : !y ? false : static_cast<bool>(*x <= *y);
638}
639// Returns: If !y, true; otherwise, if !x, false; otherwise *x >= *y.
640template <typename T, typename U>
641constexpr auto operator>=(const optional<T>& x, const optional<U>& y)
642 -> decltype(optional_internal::convertible_to_bool(*x >= *y)) {
643 return !y ? true : !x ? false : static_cast<bool>(*x >= *y);
644}
645
646// Comparison with nullopt [optional.nullops]
647// The C++17 (N4606) "Returns:" statements are used directly here.
648template <typename T>
649constexpr bool operator==(const optional<T>& x, nullopt_t) noexcept {
650 return !x;
651}
652template <typename T>
653constexpr bool operator==(nullopt_t, const optional<T>& x) noexcept {
654 return !x;
655}
656template <typename T>
657constexpr bool operator!=(const optional<T>& x, nullopt_t) noexcept {
658 return static_cast<bool>(x);
659}
660template <typename T>
661constexpr bool operator!=(nullopt_t, const optional<T>& x) noexcept {
662 return static_cast<bool>(x);
663}
664template <typename T>
665constexpr bool operator<(const optional<T>&, nullopt_t) noexcept {
666 return false;
667}
668template <typename T>
669constexpr bool operator<(nullopt_t, const optional<T>& x) noexcept {
670 return static_cast<bool>(x);
671}
672template <typename T>
673constexpr bool operator<=(const optional<T>& x, nullopt_t) noexcept {
674 return !x;
675}
676template <typename T>
677constexpr bool operator<=(nullopt_t, const optional<T>&) noexcept {
678 return true;
679}
680template <typename T>
681constexpr bool operator>(const optional<T>& x, nullopt_t) noexcept {
682 return static_cast<bool>(x);
683}
684template <typename T>
685constexpr bool operator>(nullopt_t, const optional<T>&) noexcept {
686 return false;
687}
688template <typename T>
689constexpr bool operator>=(const optional<T>&, nullopt_t) noexcept {
690 return true;
691}
692template <typename T>
693constexpr bool operator>=(nullopt_t, const optional<T>& x) noexcept {
694 return !x;
695}
696
697// Comparison with T [optional.comp_with_t]
698
699// Requires: The expression, e.g. "*x == v" shall be well-formed and its result
700// shall be convertible to bool.
701// The C++17 (N4606) "Equivalent to:" statements are used directly here.
702template <typename T, typename U>
703constexpr auto operator==(const optional<T>& x, const U& v)
704 -> decltype(optional_internal::convertible_to_bool(*x == v)) {
705 return static_cast<bool>(x) ? static_cast<bool>(*x == v) : false;
706}
707template <typename T, typename U>
708constexpr auto operator==(const U& v, const optional<T>& x)
709 -> decltype(optional_internal::convertible_to_bool(v == *x)) {
710 return static_cast<bool>(x) ? static_cast<bool>(v == *x) : false;
711}
712template <typename T, typename U>
713constexpr auto operator!=(const optional<T>& x, const U& v)
714 -> decltype(optional_internal::convertible_to_bool(*x != v)) {
715 return static_cast<bool>(x) ? static_cast<bool>(*x != v) : true;
716}
717template <typename T, typename U>
718constexpr auto operator!=(const U& v, const optional<T>& x)
719 -> decltype(optional_internal::convertible_to_bool(v != *x)) {
720 return static_cast<bool>(x) ? static_cast<bool>(v != *x) : true;
721}
722template <typename T, typename U>
723constexpr auto operator<(const optional<T>& x, const U& v)
724 -> decltype(optional_internal::convertible_to_bool(*x < v)) {
725 return static_cast<bool>(x) ? static_cast<bool>(*x < v) : true;
726}
727template <typename T, typename U>
728constexpr auto operator<(const U& v, const optional<T>& x)
729 -> decltype(optional_internal::convertible_to_bool(v < *x)) {
730 return static_cast<bool>(x) ? static_cast<bool>(v < *x) : false;
731}
732template <typename T, typename U>
733constexpr auto operator<=(const optional<T>& x, const U& v)
734 -> decltype(optional_internal::convertible_to_bool(*x <= v)) {
735 return static_cast<bool>(x) ? static_cast<bool>(*x <= v) : true;
736}
737template <typename T, typename U>
738constexpr auto operator<=(const U& v, const optional<T>& x)
739 -> decltype(optional_internal::convertible_to_bool(v <= *x)) {
740 return static_cast<bool>(x) ? static_cast<bool>(v <= *x) : false;
741}
742template <typename T, typename U>
743constexpr auto operator>(const optional<T>& x, const U& v)
744 -> decltype(optional_internal::convertible_to_bool(*x > v)) {
745 return static_cast<bool>(x) ? static_cast<bool>(*x > v) : false;
746}
747template <typename T, typename U>
748constexpr auto operator>(const U& v, const optional<T>& x)
749 -> decltype(optional_internal::convertible_to_bool(v > *x)) {
750 return static_cast<bool>(x) ? static_cast<bool>(v > *x) : true;
751}
752template <typename T, typename U>
753constexpr auto operator>=(const optional<T>& x, const U& v)
754 -> decltype(optional_internal::convertible_to_bool(*x >= v)) {
755 return static_cast<bool>(x) ? static_cast<bool>(*x >= v) : false;
756}
757template <typename T, typename U>
758constexpr auto operator>=(const U& v, const optional<T>& x)
759 -> decltype(optional_internal::convertible_to_bool(v >= *x)) {
760 return static_cast<bool>(x) ? static_cast<bool>(v >= *x) : true;
761}
762
763ABSL_NAMESPACE_END
764} // namespace absl
765
766namespace std {
767
768// std::hash specialization for absl::optional.
769template <typename T>
770struct hash<absl::optional<T> >
771 : absl::optional_internal::optional_hash_base<T> {};
772
773} // namespace std
774
775#undef ABSL_MSVC_CONSTEXPR_BUG_IN_UNION_LIKE_CLASS
776
777#endif // ABSL_USES_STD_OPTIONAL
778
779#endif // ABSL_TYPES_OPTIONAL_H_
780

source code of include/absl/types/optional.h