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/nullability.h"
65#include "absl/base/internal/inline_variable.h"
66#include "absl/meta/type_traits.h"
67#include "absl/types/bad_optional_access.h"
68#include "absl/types/internal/optional.h"
69
70namespace absl {
71ABSL_NAMESPACE_BEGIN
72
73// nullopt_t
74//
75// Class type for `absl::nullopt` used to indicate an `absl::optional<T>` type
76// that does not contain a value.
77struct nullopt_t {
78 // It must not be default-constructible to avoid ambiguity for opt = {}.
79 explicit constexpr nullopt_t(optional_internal::init_t) noexcept {}
80};
81
82// nullopt
83//
84// A tag constant of type `absl::nullopt_t` used to indicate an empty
85// `absl::optional` in certain functions, such as construction or assignment.
86ABSL_INTERNAL_INLINE_CONSTEXPR(nullopt_t, nullopt,
87 nullopt_t(optional_internal::init_t()));
88
89// -----------------------------------------------------------------------------
90// absl::optional
91// -----------------------------------------------------------------------------
92//
93// A value of type `absl::optional<T>` holds either a value of `T` or an
94// "empty" value. When it holds a value of `T`, it stores it as a direct
95// sub-object, so `sizeof(optional<T>)` is approximately
96// `sizeof(T) + sizeof(bool)`.
97//
98// This implementation is based on the specification in the latest draft of the
99// C++17 `std::optional` specification as of May 2017, section 20.6.
100//
101// Differences between `absl::optional<T>` and `std::optional<T>` include:
102//
103// * `constexpr` is not used for non-const member functions.
104// (dependency on some differences between C++11 and C++14.)
105// * `absl::nullopt` and `absl::in_place` are not declared `constexpr`. We
106// need the inline variable support in C++17 for external linkage.
107// * Throws `absl::bad_optional_access` instead of
108// `std::bad_optional_access`.
109// * `make_optional()` cannot be declared `constexpr` due to the absence of
110// guaranteed copy elision.
111// * The move constructor's `noexcept` specification is stronger, i.e. if the
112// default allocator is non-throwing (via setting
113// `ABSL_ALLOCATOR_NOTHROW`), it evaluates to `noexcept(true)`, because
114// we assume
115// a) move constructors should only throw due to allocation failure and
116// b) if T's move constructor allocates, it uses the same allocation
117// function as the default allocator.
118//
119template <typename T>
120class optional : private optional_internal::optional_data<T>,
121 private optional_internal::optional_ctor_base<
122 optional_internal::ctor_copy_traits<T>::traits>,
123 private optional_internal::optional_assign_base<
124 optional_internal::assign_copy_traits<T>::traits> {
125 using data_base = optional_internal::optional_data<T>;
126
127 public:
128 typedef T value_type;
129
130 // Constructors
131
132 // Constructs an `optional` holding an empty value, NOT a default constructed
133 // `T`.
134 constexpr optional() noexcept = default;
135
136 // Constructs an `optional` initialized with `nullopt` to hold an empty value.
137 constexpr optional(nullopt_t) noexcept {} // NOLINT(runtime/explicit)
138
139 // Copy constructor, standard semantics
140 optional(const optional&) = default;
141
142 // Move constructor, standard semantics
143 optional(optional&&) = default;
144
145 // Constructs a non-empty `optional` direct-initialized value of type `T` from
146 // the arguments `std::forward<Args>(args)...` within the `optional`.
147 // (The `in_place_t` is a tag used to indicate that the contained object
148 // should be constructed in-place.)
149 template <typename InPlaceT, typename... Args,
150 absl::enable_if_t<absl::conjunction<
151 std::is_same<InPlaceT, in_place_t>,
152 std::is_constructible<T, Args&&...> >::value>* = nullptr>
153 constexpr explicit optional(InPlaceT, Args&&... args)
154 : data_base(in_place_t(), std::forward<Args>(args)...) {}
155
156 // Constructs a non-empty `optional` direct-initialized value of type `T` from
157 // the arguments of an initializer_list and `std::forward<Args>(args)...`.
158 // (The `in_place_t` is a tag used to indicate that the contained object
159 // should be constructed in-place.)
160 template <typename U, typename... Args,
161 typename = typename std::enable_if<std::is_constructible<
162 T, std::initializer_list<U>&, Args&&...>::value>::type>
163 constexpr explicit optional(in_place_t, std::initializer_list<U> il,
164 Args&&... args)
165 : data_base(in_place_t(), il, std::forward<Args>(args)...) {}
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(), std::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(), std::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) ABSL_ATTRIBUTE_LIFETIME_BOUND {
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,
381 Args&&... args) ABSL_ATTRIBUTE_LIFETIME_BOUND {
382 this->destruct();
383 this->construct(il, std::forward<Args>(args)...);
384 return reference();
385 }
386
387 // Swaps
388
389 // Swap, standard semantics
390 void swap(optional& rhs) noexcept(
391 std::is_nothrow_move_constructible<T>::value&&
392 type_traits_internal::IsNothrowSwappable<T>::value) {
393 if (*this) {
394 if (rhs) {
395 type_traits_internal::Swap(**this, *rhs);
396 } else {
397 rhs.construct(std::move(**this));
398 this->destruct();
399 }
400 } else {
401 if (rhs) {
402 this->construct(std::move(*rhs));
403 rhs.destruct();
404 } else {
405 // No effect (swap(disengaged, disengaged)).
406 }
407 }
408 }
409
410 // Observers
411
412 // optional::operator->()
413 //
414 // Accesses the underlying `T` value's member `m` of an `optional`. If the
415 // `optional` is empty, behavior is undefined.
416 //
417 // If you need myOpt->foo in constexpr, use (*myOpt).foo instead.
418 absl::Nonnull<const T*> operator->() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
419 ABSL_HARDENING_ASSERT(this->engaged_);
420 return std::addressof(this->data_);
421 }
422 absl::Nonnull<T*> operator->() ABSL_ATTRIBUTE_LIFETIME_BOUND {
423 ABSL_HARDENING_ASSERT(this->engaged_);
424 return std::addressof(this->data_);
425 }
426
427 // optional::operator*()
428 //
429 // Accesses the underlying `T` value of an `optional`. If the `optional` is
430 // empty, behavior is undefined.
431 constexpr const T& operator*() const& ABSL_ATTRIBUTE_LIFETIME_BOUND {
432 ABSL_HARDENING_ASSERT(this->engaged_);
433 return reference();
434 }
435 T& operator*() & ABSL_ATTRIBUTE_LIFETIME_BOUND {
436 ABSL_HARDENING_ASSERT(this->engaged_);
437 return reference();
438 }
439 constexpr const T&& operator*() const&& ABSL_ATTRIBUTE_LIFETIME_BOUND {
440 ABSL_HARDENING_ASSERT(this->engaged_);
441 return std::move(reference());
442 }
443 T&& operator*() && ABSL_ATTRIBUTE_LIFETIME_BOUND {
444 ABSL_HARDENING_ASSERT(this->engaged_);
445 return std::move(reference());
446 }
447
448 // optional::operator bool()
449 //
450 // Returns false if and only if the `optional` is empty.
451 //
452 // if (opt) {
453 // // do something with *opt or opt->;
454 // } else {
455 // // opt is empty.
456 // }
457 //
458 constexpr explicit operator bool() const noexcept { return this->engaged_; }
459
460 // optional::has_value()
461 //
462 // Determines whether the `optional` contains a value. Returns `false` if and
463 // only if `*this` is empty.
464 constexpr bool has_value() const noexcept { return this->engaged_; }
465
466// Suppress bogus warning on MSVC: MSVC complains call to reference() after
467// throw_bad_optional_access() is unreachable.
468#ifdef _MSC_VER
469#pragma warning(push)
470#pragma warning(disable : 4702)
471#endif // _MSC_VER
472 // optional::value()
473 //
474 // Returns a reference to an `optional`s underlying value. The constness
475 // and lvalue/rvalue-ness of the `optional` is preserved to the view of
476 // the `T` sub-object. Throws `absl::bad_optional_access` when the `optional`
477 // is empty.
478 constexpr const T& value() const& ABSL_ATTRIBUTE_LIFETIME_BOUND {
479 return static_cast<bool>(*this)
480 ? reference()
481 : (optional_internal::throw_bad_optional_access(), reference());
482 }
483 T& value() & ABSL_ATTRIBUTE_LIFETIME_BOUND {
484 return static_cast<bool>(*this)
485 ? reference()
486 : (optional_internal::throw_bad_optional_access(), reference());
487 }
488 T&& value() && ABSL_ATTRIBUTE_LIFETIME_BOUND { // NOLINT(build/c++11)
489 return std::move(
490 static_cast<bool>(*this)
491 ? reference()
492 : (optional_internal::throw_bad_optional_access(), reference()));
493 }
494 constexpr const T&& value()
495 const&& ABSL_ATTRIBUTE_LIFETIME_BOUND { // NOLINT(build/c++11)
496 return std::move(
497 static_cast<bool>(*this)
498 ? reference()
499 : (optional_internal::throw_bad_optional_access(), reference()));
500 }
501#ifdef _MSC_VER
502#pragma warning(pop)
503#endif // _MSC_VER
504
505 // optional::value_or()
506 //
507 // Returns either the value of `T` or a passed default `v` if the `optional`
508 // is empty.
509 template <typename U>
510 constexpr T value_or(U&& v) const& {
511 static_assert(std::is_copy_constructible<value_type>::value,
512 "optional<T>::value_or: T must be copy constructible");
513 static_assert(std::is_convertible<U&&, value_type>::value,
514 "optional<T>::value_or: U must be convertible to T");
515 return static_cast<bool>(*this) ? **this
516 : static_cast<T>(std::forward<U>(v));
517 }
518 template <typename U>
519 T value_or(U&& v) && { // NOLINT(build/c++11)
520 static_assert(std::is_move_constructible<value_type>::value,
521 "optional<T>::value_or: T must be move constructible");
522 static_assert(std::is_convertible<U&&, value_type>::value,
523 "optional<T>::value_or: U must be convertible to T");
524 return static_cast<bool>(*this) ? std::move(**this)
525 : static_cast<T>(std::forward<U>(v));
526 }
527
528 private:
529 // Private accessors for internal storage viewed as reference to T.
530 constexpr const T& reference() const { return this->data_; }
531 T& reference() { return this->data_; }
532
533 // T constraint checks. You can't have an optional of nullopt_t, in_place_t
534 // or a reference.
535 static_assert(
536 !std::is_same<nullopt_t, typename std::remove_cv<T>::type>::value,
537 "optional<nullopt_t> is not allowed.");
538 static_assert(
539 !std::is_same<in_place_t, typename std::remove_cv<T>::type>::value,
540 "optional<in_place_t> is not allowed.");
541 static_assert(!std::is_reference<T>::value,
542 "optional<reference> is not allowed.");
543};
544
545// Non-member functions
546
547// swap()
548//
549// Performs a swap between two `absl::optional` objects, using standard
550// semantics.
551template <typename T, typename std::enable_if<
552 std::is_move_constructible<T>::value &&
553 type_traits_internal::IsSwappable<T>::value,
554 bool>::type = false>
555void swap(optional<T>& a, optional<T>& b) noexcept(noexcept(a.swap(b))) {
556 a.swap(b);
557}
558
559// make_optional()
560//
561// Creates a non-empty `optional<T>` where the type of `T` is deduced. An
562// `absl::optional` can also be explicitly instantiated with
563// `make_optional<T>(v)`.
564//
565// Note: `make_optional()` constructions may be declared `constexpr` for
566// trivially copyable types `T`. Non-trivial types require copy elision
567// support in C++17 for `make_optional` to support `constexpr` on such
568// non-trivial types.
569//
570// Example:
571//
572// constexpr absl::optional<int> opt = absl::make_optional(1);
573// static_assert(opt.value() == 1, "");
574template <typename T>
575constexpr optional<typename std::decay<T>::type> make_optional(T&& v) {
576 return optional<typename std::decay<T>::type>(std::forward<T>(v));
577}
578
579template <typename T, typename... Args>
580constexpr optional<T> make_optional(Args&&... args) {
581 return optional<T>(in_place_t(), std::forward<Args>(args)...);
582}
583
584template <typename T, typename U, typename... Args>
585constexpr optional<T> make_optional(std::initializer_list<U> il,
586 Args&&... args) {
587 return optional<T>(in_place_t(), il, std::forward<Args>(args)...);
588}
589
590// Relational operators [optional.relops]
591
592// Empty optionals are considered equal to each other and less than non-empty
593// optionals. Supports relations between optional<T> and optional<U>, between
594// optional<T> and U, and between optional<T> and nullopt.
595//
596// Note: We're careful to support T having non-bool relationals.
597
598// Requires: The expression, e.g. "*x == *y" shall be well-formed and its result
599// shall be convertible to bool.
600// The C++17 (N4606) "Returns:" statements are translated into
601// code in an obvious way here, and the original text retained as function docs.
602// Returns: If bool(x) != bool(y), false; otherwise if bool(x) == false, true;
603// otherwise *x == *y.
604template <typename T, typename U>
605constexpr auto operator==(const optional<T>& x, const optional<U>& y)
606 -> decltype(optional_internal::convertible_to_bool(*x == *y)) {
607 return static_cast<bool>(x) != static_cast<bool>(y)
608 ? false
609 : static_cast<bool>(x) == false ? true
610 : static_cast<bool>(*x == *y);
611}
612
613// Returns: If bool(x) != bool(y), true; otherwise, if bool(x) == false, false;
614// otherwise *x != *y.
615template <typename T, typename U>
616constexpr auto operator!=(const optional<T>& x, const optional<U>& y)
617 -> decltype(optional_internal::convertible_to_bool(*x != *y)) {
618 return static_cast<bool>(x) != static_cast<bool>(y)
619 ? true
620 : static_cast<bool>(x) == false ? false
621 : static_cast<bool>(*x != *y);
622}
623// Returns: If !y, false; otherwise, if !x, true; otherwise *x < *y.
624template <typename T, typename U>
625constexpr auto operator<(const optional<T>& x, const optional<U>& y)
626 -> decltype(optional_internal::convertible_to_bool(*x < *y)) {
627 return !y ? false : !x ? true : static_cast<bool>(*x < *y);
628}
629// Returns: If !x, false; otherwise, if !y, true; otherwise *x > *y.
630template <typename T, typename U>
631constexpr auto operator>(const optional<T>& x, const optional<U>& y)
632 -> decltype(optional_internal::convertible_to_bool(*x > *y)) {
633 return !x ? false : !y ? true : static_cast<bool>(*x > *y);
634}
635// Returns: If !x, true; otherwise, if !y, false; otherwise *x <= *y.
636template <typename T, typename U>
637constexpr auto operator<=(const optional<T>& x, const optional<U>& y)
638 -> decltype(optional_internal::convertible_to_bool(*x <= *y)) {
639 return !x ? true : !y ? false : static_cast<bool>(*x <= *y);
640}
641// Returns: If !y, true; otherwise, if !x, false; otherwise *x >= *y.
642template <typename T, typename U>
643constexpr auto operator>=(const optional<T>& x, const optional<U>& y)
644 -> decltype(optional_internal::convertible_to_bool(*x >= *y)) {
645 return !y ? true : !x ? false : static_cast<bool>(*x >= *y);
646}
647
648// Comparison with nullopt [optional.nullops]
649// The C++17 (N4606) "Returns:" statements are used directly here.
650template <typename T>
651constexpr bool operator==(const optional<T>& x, nullopt_t) noexcept {
652 return !x;
653}
654template <typename T>
655constexpr bool operator==(nullopt_t, const optional<T>& x) noexcept {
656 return !x;
657}
658template <typename T>
659constexpr bool operator!=(const optional<T>& x, nullopt_t) noexcept {
660 return static_cast<bool>(x);
661}
662template <typename T>
663constexpr bool operator!=(nullopt_t, const optional<T>& x) noexcept {
664 return static_cast<bool>(x);
665}
666template <typename T>
667constexpr bool operator<(const optional<T>&, nullopt_t) noexcept {
668 return false;
669}
670template <typename T>
671constexpr bool operator<(nullopt_t, const optional<T>& x) noexcept {
672 return static_cast<bool>(x);
673}
674template <typename T>
675constexpr bool operator<=(const optional<T>& x, nullopt_t) noexcept {
676 return !x;
677}
678template <typename T>
679constexpr bool operator<=(nullopt_t, const optional<T>&) noexcept {
680 return true;
681}
682template <typename T>
683constexpr bool operator>(const optional<T>& x, nullopt_t) noexcept {
684 return static_cast<bool>(x);
685}
686template <typename T>
687constexpr bool operator>(nullopt_t, const optional<T>&) noexcept {
688 return false;
689}
690template <typename T>
691constexpr bool operator>=(const optional<T>&, nullopt_t) noexcept {
692 return true;
693}
694template <typename T>
695constexpr bool operator>=(nullopt_t, const optional<T>& x) noexcept {
696 return !x;
697}
698
699// Comparison with T [optional.comp_with_t]
700
701// Requires: The expression, e.g. "*x == v" shall be well-formed and its result
702// shall be convertible to bool.
703// The C++17 (N4606) "Equivalent to:" statements are used directly here.
704template <typename T, typename U>
705constexpr auto operator==(const optional<T>& x, const U& v)
706 -> decltype(optional_internal::convertible_to_bool(*x == v)) {
707 return static_cast<bool>(x) ? static_cast<bool>(*x == v) : false;
708}
709template <typename T, typename U>
710constexpr auto operator==(const U& v, const optional<T>& x)
711 -> decltype(optional_internal::convertible_to_bool(v == *x)) {
712 return static_cast<bool>(x) ? static_cast<bool>(v == *x) : false;
713}
714template <typename T, typename U>
715constexpr auto operator!=(const optional<T>& x, const U& v)
716 -> decltype(optional_internal::convertible_to_bool(*x != v)) {
717 return static_cast<bool>(x) ? static_cast<bool>(*x != v) : true;
718}
719template <typename T, typename U>
720constexpr auto operator!=(const U& v, const optional<T>& x)
721 -> decltype(optional_internal::convertible_to_bool(v != *x)) {
722 return static_cast<bool>(x) ? static_cast<bool>(v != *x) : true;
723}
724template <typename T, typename U>
725constexpr auto operator<(const optional<T>& x, const U& v)
726 -> decltype(optional_internal::convertible_to_bool(*x < v)) {
727 return static_cast<bool>(x) ? static_cast<bool>(*x < v) : true;
728}
729template <typename T, typename U>
730constexpr auto operator<(const U& v, const optional<T>& x)
731 -> decltype(optional_internal::convertible_to_bool(v < *x)) {
732 return static_cast<bool>(x) ? static_cast<bool>(v < *x) : false;
733}
734template <typename T, typename U>
735constexpr auto operator<=(const optional<T>& x, const U& v)
736 -> decltype(optional_internal::convertible_to_bool(*x <= v)) {
737 return static_cast<bool>(x) ? static_cast<bool>(*x <= v) : true;
738}
739template <typename T, typename U>
740constexpr auto operator<=(const U& v, const optional<T>& x)
741 -> decltype(optional_internal::convertible_to_bool(v <= *x)) {
742 return static_cast<bool>(x) ? static_cast<bool>(v <= *x) : false;
743}
744template <typename T, typename U>
745constexpr auto operator>(const optional<T>& x, const U& v)
746 -> decltype(optional_internal::convertible_to_bool(*x > v)) {
747 return static_cast<bool>(x) ? static_cast<bool>(*x > v) : false;
748}
749template <typename T, typename U>
750constexpr auto operator>(const U& v, const optional<T>& x)
751 -> decltype(optional_internal::convertible_to_bool(v > *x)) {
752 return static_cast<bool>(x) ? static_cast<bool>(v > *x) : true;
753}
754template <typename T, typename U>
755constexpr auto operator>=(const optional<T>& x, const U& v)
756 -> decltype(optional_internal::convertible_to_bool(*x >= v)) {
757 return static_cast<bool>(x) ? static_cast<bool>(*x >= v) : false;
758}
759template <typename T, typename U>
760constexpr auto operator>=(const U& v, const optional<T>& x)
761 -> decltype(optional_internal::convertible_to_bool(v >= *x)) {
762 return static_cast<bool>(x) ? static_cast<bool>(v >= *x) : true;
763}
764
765ABSL_NAMESPACE_END
766} // namespace absl
767
768namespace std {
769
770// std::hash specialization for absl::optional.
771template <typename T>
772struct hash<absl::optional<T> >
773 : absl::optional_internal::optional_hash_base<T> {};
774
775} // namespace std
776
777#undef ABSL_MSVC_CONSTEXPR_BUG_IN_UNION_LIKE_CLASS
778
779#endif // ABSL_USES_STD_OPTIONAL
780
781#endif // ABSL_TYPES_OPTIONAL_H_
782