File: expected.h

package info (click to toggle)
android-platform-tools 29.0.6-28
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 365,224 kB
  • sloc: cpp: 1,049,638; java: 460,532; ansic: 375,452; asm: 301,257; xml: 134,509; python: 92,731; perl: 62,008; sh: 26,753; makefile: 3,210; javascript: 3,172; yacc: 1,403; lex: 455; awk: 368; ruby: 183; sql: 140
file content (759 lines) | stat: -rw-r--r-- 27,146 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <algorithm>
#include <initializer_list>
#include <type_traits>
#include <utility>
#include <variant>

// android::base::expected is an Android implementation of the std::expected
// proposal.
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0323r7.html
//
// Usage:
// using android::base::expected;
// using android::base::unexpected;
//
// expected<double,std::string> safe_divide(double i, double j) {
//   if (j == 0) return unexpected("divide by zero");
//   else return i / j;
// }
//
// void test() {
//   auto q = safe_divide(10, 0);
//   if (q) { printf("%f\n", q.value()); }
//   else { printf("%s\n", q.error().c_str()); }
// }
//
// When the proposal becomes part of the standard and is implemented by
// libcxx, this will be removed and android::base::expected will be
// type alias to std::expected.
//

namespace android {
namespace base {

// Synopsis
template<class T, class E>
    class expected;

template<class E>
    class unexpected;
template<class E>
  unexpected(E) -> unexpected<E>;

template<class E>
   class bad_expected_access;

template<>
   class bad_expected_access<void>;

struct unexpect_t {
   explicit unexpect_t() = default;
};
inline constexpr unexpect_t unexpect{};

// macros for SFINAE
#define _ENABLE_IF(...) \
  , std::enable_if_t<(__VA_ARGS__)>* = nullptr

// Define NODISCARD_EXPECTED to prevent expected<T,E> from being
// ignored when used as a return value. This is off by default.
#ifdef NODISCARD_EXPECTED
#define _NODISCARD_ [[nodiscard]]
#else
#define _NODISCARD_
#endif

// Class expected
template<class T, class E>
class _NODISCARD_ expected {
 public:
  using value_type = T;
  using error_type = E;
  using unexpected_type = unexpected<E>;

  template<class U>
  using rebind = expected<U, error_type>;

  // constructors
  constexpr expected() = default;
  constexpr expected(const expected& rhs) = default;
  constexpr expected(expected&& rhs) noexcept = default;

  template<class U, class G _ENABLE_IF(
    std::is_constructible_v<T, const U&> &&
    std::is_constructible_v<E, const G&> &&
    !std::is_constructible_v<T, expected<U, G>&> &&
    !std::is_constructible_v<T, expected<U, G>&&> &&
    !std::is_constructible_v<T, const expected<U, G>&> &&
    !std::is_constructible_v<T, const expected<U, G>&&> &&
    !std::is_convertible_v<expected<U, G>&, T> &&
    !std::is_convertible_v<expected<U, G>&&, T> &&
    !std::is_convertible_v<const expected<U, G>&, T> &&
    !std::is_convertible_v<const expected<U, G>&&, T> &&
    !(!std::is_convertible_v<const U&, T> ||
     !std::is_convertible_v<const G&, E>) /* non-explicit */
  )>
  // NOLINTNEXTLINE(google-explicit-constructor)
  constexpr expected(const expected<U, G>& rhs) {
    if (rhs.has_value()) var_ = rhs.value();
    else var_ = unexpected(rhs.error());
  }

  template<class U, class G _ENABLE_IF(
    std::is_constructible_v<T, const U&> &&
    std::is_constructible_v<E, const G&> &&
    !std::is_constructible_v<T, expected<U, G>&> &&
    !std::is_constructible_v<T, expected<U, G>&&> &&
    !std::is_constructible_v<T, const expected<U, G>&> &&
    !std::is_constructible_v<T, const expected<U, G>&&> &&
    !std::is_convertible_v<expected<U, G>&, T> &&
    !std::is_convertible_v<expected<U, G>&&, T> &&
    !std::is_convertible_v<const expected<U, G>&, T> &&
    !std::is_convertible_v<const expected<U, G>&&, T> &&
    (!std::is_convertible_v<const U&, T> ||
     !std::is_convertible_v<const G&, E>) /* explicit */
  )>
  constexpr explicit expected(const expected<U, G>& rhs) {
    if (rhs.has_value()) var_ = rhs.value();
    else var_ = unexpected(rhs.error());
  }

  template<class U, class G _ENABLE_IF(
    std::is_constructible_v<T, const U&> &&
    std::is_constructible_v<E, const G&> &&
    !std::is_constructible_v<T, expected<U, G>&> &&
    !std::is_constructible_v<T, expected<U, G>&&> &&
    !std::is_constructible_v<T, const expected<U, G>&> &&
    !std::is_constructible_v<T, const expected<U, G>&&> &&
    !std::is_convertible_v<expected<U, G>&, T> &&
    !std::is_convertible_v<expected<U, G>&&, T> &&
    !std::is_convertible_v<const expected<U, G>&, T> &&
    !std::is_convertible_v<const expected<U, G>&&, T> &&
    !(!std::is_convertible_v<const U&, T> ||
     !std::is_convertible_v<const G&, E>) /* non-explicit */
  )>
  // NOLINTNEXTLINE(google-explicit-constructor)
  constexpr expected(expected<U, G>&& rhs) {
    if (rhs.has_value()) var_ = std::move(rhs.value());
    else var_ = unexpected(std::move(rhs.error()));
  }

  template<class U, class G _ENABLE_IF(
    std::is_constructible_v<T, const U&> &&
    std::is_constructible_v<E, const G&> &&
    !std::is_constructible_v<T, expected<U, G>&> &&
    !std::is_constructible_v<T, expected<U, G>&&> &&
    !std::is_constructible_v<T, const expected<U, G>&> &&
    !std::is_constructible_v<T, const expected<U, G>&&> &&
    !std::is_convertible_v<expected<U, G>&, T> &&
    !std::is_convertible_v<expected<U, G>&&, T> &&
    !std::is_convertible_v<const expected<U, G>&, T> &&
    !std::is_convertible_v<const expected<U, G>&&, T> &&
    (!std::is_convertible_v<const U&, T> ||
     !std::is_convertible_v<const G&, E>) /* explicit */
  )>
  constexpr explicit expected(expected<U, G>&& rhs) {
    if (rhs.has_value()) var_ = std::move(rhs.value());
    else var_ = unexpected(std::move(rhs.error()));
  }

  template <class U = T _ENABLE_IF(
                std::is_constructible_v<T, U&&> &&
                !std::is_same_v<std::remove_cv_t<std::remove_reference_t<U>>, std::in_place_t> &&
                !std::is_same_v<expected<T, E>, std::remove_cv_t<std::remove_reference_t<U>>> &&
                !std::is_same_v<unexpected<E>, std::remove_cv_t<std::remove_reference_t<U>>> &&
                std::is_convertible_v<U&&, T> /* non-explicit */
                )>
  // NOLINTNEXTLINE(google-explicit-constructor)
  constexpr expected(U&& v) : var_(std::in_place_index<0>, std::forward<U>(v)) {}

  template <class U = T _ENABLE_IF(
                std::is_constructible_v<T, U&&> &&
                !std::is_same_v<std::remove_cv_t<std::remove_reference_t<U>>, std::in_place_t> &&
                !std::is_same_v<expected<T, E>, std::remove_cv_t<std::remove_reference_t<U>>> &&
                !std::is_same_v<unexpected<E>, std::remove_cv_t<std::remove_reference_t<U>>> &&
                !std::is_convertible_v<U&&, T> /* explicit */
                )>
  constexpr explicit expected(U&& v) : var_(std::in_place_index<0>, T(std::forward<U>(v))) {}

  template<class G = E _ENABLE_IF(
    std::is_constructible_v<E, const G&> &&
    std::is_convertible_v<const G&, E> /* non-explicit */
  )>
  // NOLINTNEXTLINE(google-explicit-constructor)
  constexpr expected(const unexpected<G>& e)
  : var_(std::in_place_index<1>, e.value()) {}

  template<class G = E _ENABLE_IF(
    std::is_constructible_v<E, const G&> &&
    !std::is_convertible_v<const G&, E> /* explicit */
  )>
  constexpr explicit expected(const unexpected<G>& e)
  : var_(std::in_place_index<1>, E(e.value())) {}

  template<class G = E _ENABLE_IF(
    std::is_constructible_v<E, G&&> &&
    std::is_convertible_v<G&&, E> /* non-explicit */
  )>
  // NOLINTNEXTLINE(google-explicit-constructor)
  constexpr expected(unexpected<G>&& e)
  : var_(std::in_place_index<1>, std::move(e.value())) {}

  template<class G = E _ENABLE_IF(
    std::is_constructible_v<E, G&&> &&
    !std::is_convertible_v<G&&, E> /* explicit */
  )>
  constexpr explicit expected(unexpected<G>&& e)
  : var_(std::in_place_index<1>, E(std::move(e.value()))) {}

  template<class... Args _ENABLE_IF(
    std::is_constructible_v<T, Args&&...>
  )>
  constexpr explicit expected(std::in_place_t, Args&&... args)
  : var_(std::in_place_index<0>, std::forward<Args>(args)...) {}

  template<class U, class... Args _ENABLE_IF(
    std::is_constructible_v<T, std::initializer_list<U>&, Args...>
  )>
  constexpr explicit expected(std::in_place_t, std::initializer_list<U> il, Args&&... args)
  : var_(std::in_place_index<0>, il, std::forward<Args>(args)...) {}

  template<class... Args _ENABLE_IF(
    std::is_constructible_v<E, Args...>
  )>
  constexpr explicit expected(unexpect_t, Args&&... args)
  : var_(unexpected_type(std::forward<Args>(args)...)) {}

  template<class U, class... Args _ENABLE_IF(
    std::is_constructible_v<E, std::initializer_list<U>&, Args...>
  )>
  constexpr explicit expected(unexpect_t, std::initializer_list<U> il, Args&&... args)
  : var_(unexpected_type(il, std::forward<Args>(args)...)) {}

  // destructor
  ~expected() = default;

  // assignment
  // Note: SFNAIE doesn't work here because assignment operator should be
  // non-template. We could workaround this by defining a templated parent class
  // having the assignment operator. This incomplete implementation however
  // doesn't allow us to copy assign expected<T,E> even when T is non-copy
  // assignable. The copy assignment will fail by the underlying std::variant
  // anyway though the error message won't be clear.
  expected& operator=(const expected& rhs) = default;

  // Note for SFNAIE above applies to here as well
  expected& operator=(expected&& rhs) noexcept(
      std::is_nothrow_move_assignable_v<T>&& std::is_nothrow_move_assignable_v<E>) = default;

  template <class U = T _ENABLE_IF(
                !std::is_void_v<T> &&
                !std::is_same_v<expected<T, E>, std::remove_cv_t<std::remove_reference_t<U>>> &&
                !std::conjunction_v<std::is_scalar<T>, std::is_same<T, std::decay_t<U>>> &&
                std::is_constructible_v<T, U> && std::is_assignable_v<T&, U> &&
                std::is_nothrow_move_constructible_v<E>)>
  expected& operator=(U&& rhs) {
    var_ = T(std::forward<U>(rhs));
    return *this;
  }

  template<class G = E>
  expected& operator=(const unexpected<G>& rhs) {
    var_ = rhs;
    return *this;
  }

  template<class G = E _ENABLE_IF(
    std::is_nothrow_move_constructible_v<G> &&
    std::is_move_assignable_v<G>
  )>
  expected& operator=(unexpected<G>&& rhs) {
    var_ = std::move(rhs);
    return *this;
  }

  // modifiers
  template<class... Args _ENABLE_IF(
    std::is_nothrow_constructible_v<T, Args...>
  )>
  T& emplace(Args&&... args) {
    expected(std::in_place, std::forward<Args>(args)...).swap(*this);
    return value();
  }

  template<class U, class... Args _ENABLE_IF(
    std::is_nothrow_constructible_v<T, std::initializer_list<U>&, Args...>
  )>
  T& emplace(std::initializer_list<U> il, Args&&... args) {
    expected(std::in_place, il, std::forward<Args>(args)...).swap(*this);
    return value();
  }

  // swap
  template<typename U = T, typename = std::enable_if_t<(
    std::is_swappable_v<U> &&
    std::is_swappable_v<E> &&
    (std::is_move_constructible_v<U> ||
     std::is_move_constructible_v<E>))>>
  void swap(expected& rhs) noexcept(
    std::is_nothrow_move_constructible_v<T> &&
    std::is_nothrow_swappable_v<T> &&
    std::is_nothrow_move_constructible_v<E> &&
    std::is_nothrow_swappable_v<E>) {
    var_.swap(rhs.var_);
  }

  // observers
  constexpr const T* operator->() const { return std::addressof(value()); }
  constexpr T* operator->() { return std::addressof(value()); }
  constexpr const T& operator*() const& { return value(); }
  constexpr T& operator*() & { return value(); }
  constexpr const T&& operator*() const&& { return std::move(std::get<T>(var_)); }
  constexpr T&& operator*() && { return std::move(std::get<T>(var_)); }

  constexpr explicit operator bool() const noexcept { return has_value(); }
  constexpr bool has_value() const noexcept { return var_.index() == 0; }
  constexpr bool ok() const noexcept { return has_value(); }

  constexpr const T& value() const& { return std::get<T>(var_); }
  constexpr T& value() & { return std::get<T>(var_); }
  constexpr const T&& value() const&& { return std::move(std::get<T>(var_)); }
  constexpr T&& value() && { return std::move(std::get<T>(var_)); }

  constexpr const E& error() const& { return std::get<unexpected_type>(var_).value(); }
  constexpr E& error() & { return std::get<unexpected_type>(var_).value(); }
  constexpr const E&& error() const&& { return std::move(std::get<unexpected_type>(var_)).value(); }
  constexpr E&& error() && { return std::move(std::get<unexpected_type>(var_)).value(); }

  template<class U _ENABLE_IF(
    std::is_copy_constructible_v<T> &&
    std::is_convertible_v<U, T>
  )>
  constexpr T value_or(U&& v) const& {
    if (has_value()) return value();
    else return static_cast<T>(std::forward<U>(v));
  }

  template<class U _ENABLE_IF(
    std::is_move_constructible_v<T> &&
    std::is_convertible_v<U, T>
  )>
  constexpr T value_or(U&& v) && {
    if (has_value()) return std::move(value());
    else return static_cast<T>(std::forward<U>(v));
  }

  // expected equality operators
  template<class T1, class E1, class T2, class E2>
  friend constexpr bool operator==(const expected<T1, E1>& x, const expected<T2, E2>& y);
  template<class T1, class E1, class T2, class E2>
  friend constexpr bool operator!=(const expected<T1, E1>& x, const expected<T2, E2>& y);

  // Comparison with unexpected<E>
  template<class T1, class E1, class E2>
  friend constexpr bool operator==(const expected<T1, E1>&, const unexpected<E2>&);
  template<class T1, class E1, class E2>
  friend constexpr bool operator==(const unexpected<E2>&, const expected<T1, E1>&);
  template<class T1, class E1, class E2>
  friend constexpr bool operator!=(const expected<T1, E1>&, const unexpected<E2>&);
  template<class T1, class E1, class E2>
  friend constexpr bool operator!=(const unexpected<E2>&, const expected<T1, E1>&);

  // Specialized algorithms
  template<class T1, class E1>
  friend void swap(expected<T1, E1>&, expected<T1, E1>&) noexcept;

 private:
  std::variant<value_type, unexpected_type> var_;
};

template<class T1, class E1, class T2, class E2>
constexpr bool operator==(const expected<T1, E1>& x, const expected<T2, E2>& y) {
  if (x.has_value() != y.has_value()) {
    return false;
  } else if (!x.has_value()) {
    return x.error() == y.error();
  } else {
    return *x == *y;
  }
}

template<class T1, class E1, class T2, class E2>
constexpr bool operator!=(const expected<T1, E1>& x, const expected<T2, E2>& y) {
  return !(x == y);
}

// Comparison with unexpected<E>
template<class T1, class E1, class E2>
constexpr bool operator==(const expected<T1, E1>& x, const unexpected<E2>& y) {
  return !x.has_value() && (x.error() == y.value());
}
template<class T1, class E1, class E2>
constexpr bool operator==(const unexpected<E2>& x, const expected<T1, E1>& y) {
  return !y.has_value() && (x.value() == y.error());
}
template<class T1, class E1, class E2>
constexpr bool operator!=(const expected<T1, E1>& x, const unexpected<E2>& y) {
  return x.has_value() || (x.error() != y.value());
}
template<class T1, class E1, class E2>
constexpr bool operator!=(const unexpected<E2>& x, const expected<T1, E1>& y) {
  return y.has_value() || (x.value() != y.error());
}

template<class E>
class _NODISCARD_ expected<void, E> {
 public:
  using value_type = void;
  using error_type = E;
  using unexpected_type = unexpected<E>;

  // constructors
  constexpr expected() = default;
  constexpr expected(const expected& rhs) = default;
  constexpr expected(expected&& rhs) noexcept = default;

  template<class U, class G _ENABLE_IF(
    std::is_void_v<U> &&
    std::is_convertible_v<const G&, E> /* non-explicit */
  )>
  // NOLINTNEXTLINE(google-explicit-constructor)
  constexpr expected(const expected<U, G>& rhs) {
    if (!rhs.has_value()) var_ = unexpected(rhs.error());
  }

  template<class U, class G _ENABLE_IF(
    std::is_void_v<U> &&
    !std::is_convertible_v<const G&, E> /* explicit */
  )>
  constexpr explicit expected(const expected<U, G>& rhs) {
    if (!rhs.has_value()) var_ = unexpected(rhs.error());
  }

  template<class U, class G _ENABLE_IF(
    std::is_void_v<U> &&
    std::is_convertible_v<const G&&, E> /* non-explicit */
  )>
  // NOLINTNEXTLINE(google-explicit-constructor)
  constexpr expected(expected<U, G>&& rhs) {
    if (!rhs.has_value()) var_ = unexpected(std::move(rhs.error()));
  }

  template<class U, class G _ENABLE_IF(
    std::is_void_v<U> &&
    !std::is_convertible_v<const G&&, E> /* explicit */
  )>
  constexpr explicit expected(expected<U, G>&& rhs) {
    if (!rhs.has_value()) var_ = unexpected(std::move(rhs.error()));
  }

  template<class G = E _ENABLE_IF(
    std::is_constructible_v<E, const G&> &&
    std::is_convertible_v<const G&, E> /* non-explicit */
  )>
  // NOLINTNEXTLINE(google-explicit-constructor)
  constexpr expected(const unexpected<G>& e)
  : var_(std::in_place_index<1>, e.value()) {}

  template<class G = E _ENABLE_IF(
    std::is_constructible_v<E, const G&> &&
    !std::is_convertible_v<const G&, E> /* explicit */
  )>
  constexpr explicit expected(const unexpected<G>& e)
  : var_(std::in_place_index<1>, E(e.value())) {}

  template<class G = E _ENABLE_IF(
    std::is_constructible_v<E, G&&> &&
    std::is_convertible_v<G&&, E> /* non-explicit */
  )>
  // NOLINTNEXTLINE(google-explicit-constructor)
  constexpr expected(unexpected<G>&& e)
  : var_(std::in_place_index<1>, std::move(e.value())) {}

  template<class G = E _ENABLE_IF(
    std::is_constructible_v<E, G&&> &&
    !std::is_convertible_v<G&&, E> /* explicit */
  )>
  constexpr explicit expected(unexpected<G>&& e)
  : var_(std::in_place_index<1>, E(std::move(e.value()))) {}

  template<class... Args _ENABLE_IF(
    sizeof...(Args) == 0
  )>
  constexpr explicit expected(std::in_place_t, Args&&...) {}

  template<class... Args _ENABLE_IF(
    std::is_constructible_v<E, Args...>
  )>
  constexpr explicit expected(unexpect_t, Args&&... args)
  : var_(unexpected_type(std::forward<Args>(args)...)) {}

  template<class U, class... Args _ENABLE_IF(
    std::is_constructible_v<E, std::initializer_list<U>&, Args...>
  )>
  constexpr explicit expected(unexpect_t, std::initializer_list<U> il, Args&&... args)
  : var_(unexpected_type(il, std::forward<Args>(args)...)) {}

  // destructor
  ~expected() = default;

  // assignment
  // Note: SFNAIE doesn't work here because assignment operator should be
  // non-template. We could workaround this by defining a templated parent class
  // having the assignment operator. This incomplete implementation however
  // doesn't allow us to copy assign expected<T,E> even when T is non-copy
  // assignable. The copy assignment will fail by the underlying std::variant
  // anyway though the error message won't be clear.
  expected& operator=(const expected& rhs) = default;

  // Note for SFNAIE above applies to here as well
  expected& operator=(expected&& rhs) noexcept(std::is_nothrow_move_assignable_v<E>) = default;

  template<class G = E>
  expected& operator=(const unexpected<G>& rhs) {
    var_ = rhs;
    return *this;
  }

  template<class G = E _ENABLE_IF(
    std::is_nothrow_move_constructible_v<G> &&
    std::is_move_assignable_v<G>
  )>
  expected& operator=(unexpected<G>&& rhs) {
    var_ = std::move(rhs);
    return *this;
  }

  // modifiers
  void emplace() {
    var_ = std::monostate();
  }

  // swap
  template<typename = std::enable_if_t<
    std::is_swappable_v<E>>
  >
  void swap(expected& rhs) noexcept(std::is_nothrow_move_constructible_v<E>) {
    var_.swap(rhs.var_);
  }

  // observers
  constexpr explicit operator bool() const noexcept { return has_value(); }
  constexpr bool has_value() const noexcept { return var_.index() == 0; }
  constexpr bool ok() const noexcept { return has_value(); }

  constexpr void value() const& { if (!has_value()) std::get<0>(var_); }

  constexpr const E& error() const& { return std::get<unexpected_type>(var_).value(); }
  constexpr E& error() & { return std::get<unexpected_type>(var_).value(); }
  constexpr const E&& error() const&& { return std::move(std::get<unexpected_type>(var_)).value(); }
  constexpr E&& error() && { return std::move(std::get<unexpected_type>(var_)).value(); }

  // expected equality operators
  template<class E1, class E2>
  friend constexpr bool operator==(const expected<void, E1>& x, const expected<void, E2>& y);

  // Specialized algorithms
  template<class T1, class E1>
  friend void swap(expected<T1, E1>&, expected<T1, E1>&) noexcept;

 private:
  std::variant<std::monostate, unexpected_type> var_;
};

template<class E1, class E2>
constexpr bool operator==(const expected<void, E1>& x, const expected<void, E2>& y) {
  if (x.has_value() != y.has_value()) {
    return false;
  } else if (!x.has_value()) {
    return x.error() == y.error();
  } else {
    return true;
  }
}

template<class T1, class E1, class E2>
constexpr bool operator==(const expected<T1, E1>& x, const expected<void, E2>& y) {
  if (x.has_value() != y.has_value()) {
    return false;
  } else if (!x.has_value()) {
    return x.error() == y.error();
  } else {
    return false;
  }
}

template<class E1, class T2, class E2>
constexpr bool operator==(const expected<void, E1>& x, const expected<T2, E2>& y) {
  if (x.has_value() != y.has_value()) {
    return false;
  } else if (!x.has_value()) {
    return x.error() == y.error();
  } else {
    return false;
  }
}

template<class E>
class unexpected {
 public:
  // constructors
  constexpr unexpected(const unexpected&) = default;
  constexpr unexpected(unexpected&&) noexcept(std::is_nothrow_move_constructible_v<E>) = default;

  template <class Err = E _ENABLE_IF(
                std::is_constructible_v<E, Err> &&
                !std::is_same_v<std::remove_cv_t<std::remove_reference_t<E>>, std::in_place_t> &&
                !std::is_same_v<std::remove_cv_t<std::remove_reference_t<E>>, unexpected>)>
  // NOLINTNEXTLINE(google-explicit-constructor)
  constexpr unexpected(Err&& e) : val_(std::forward<Err>(e)) {}

  template<class U, class... Args _ENABLE_IF(
    std::is_constructible_v<E, std::initializer_list<U>&, Args...>
  )>
  constexpr explicit unexpected(std::in_place_t, std::initializer_list<U> il, Args&&... args)
  : val_(il, std::forward<Args>(args)...) {}

  template<class Err _ENABLE_IF(
    std::is_constructible_v<E, Err> &&
    !std::is_constructible_v<E, unexpected<Err>&> &&
    !std::is_constructible_v<E, unexpected<Err>> &&
    !std::is_constructible_v<E, const unexpected<Err>&> &&
    !std::is_constructible_v<E, const unexpected<Err>> &&
    !std::is_convertible_v<unexpected<Err>&, E> &&
    !std::is_convertible_v<unexpected<Err>, E> &&
    !std::is_convertible_v<const unexpected<Err>&, E> &&
    !std::is_convertible_v<const unexpected<Err>, E> &&
    std::is_convertible_v<Err, E> /* non-explicit */
  )>
  // NOLINTNEXTLINE(google-explicit-constructor)
  constexpr unexpected(const unexpected<Err>& rhs)
  : val_(rhs.value()) {}

  template<class Err _ENABLE_IF(
    std::is_constructible_v<E, Err> &&
    !std::is_constructible_v<E, unexpected<Err>&> &&
    !std::is_constructible_v<E, unexpected<Err>> &&
    !std::is_constructible_v<E, const unexpected<Err>&> &&
    !std::is_constructible_v<E, const unexpected<Err>> &&
    !std::is_convertible_v<unexpected<Err>&, E> &&
    !std::is_convertible_v<unexpected<Err>, E> &&
    !std::is_convertible_v<const unexpected<Err>&, E> &&
    !std::is_convertible_v<const unexpected<Err>, E> &&
    !std::is_convertible_v<Err, E> /* explicit */
  )>
  constexpr explicit unexpected(const unexpected<Err>& rhs)
  : val_(E(rhs.value())) {}

  template<class Err _ENABLE_IF(
    std::is_constructible_v<E, Err> &&
    !std::is_constructible_v<E, unexpected<Err>&> &&
    !std::is_constructible_v<E, unexpected<Err>> &&
    !std::is_constructible_v<E, const unexpected<Err>&> &&
    !std::is_constructible_v<E, const unexpected<Err>> &&
    !std::is_convertible_v<unexpected<Err>&, E> &&
    !std::is_convertible_v<unexpected<Err>, E> &&
    !std::is_convertible_v<const unexpected<Err>&, E> &&
    !std::is_convertible_v<const unexpected<Err>, E> &&
    std::is_convertible_v<Err, E> /* non-explicit */
  )>
  // NOLINTNEXTLINE(google-explicit-constructor)
  constexpr unexpected(unexpected<Err>&& rhs)
  : val_(std::move(rhs.value())) {}

  template<class Err _ENABLE_IF(
    std::is_constructible_v<E, Err> &&
    !std::is_constructible_v<E, unexpected<Err>&> &&
    !std::is_constructible_v<E, unexpected<Err>> &&
    !std::is_constructible_v<E, const unexpected<Err>&> &&
    !std::is_constructible_v<E, const unexpected<Err>> &&
    !std::is_convertible_v<unexpected<Err>&, E> &&
    !std::is_convertible_v<unexpected<Err>, E> &&
    !std::is_convertible_v<const unexpected<Err>&, E> &&
    !std::is_convertible_v<const unexpected<Err>, E> &&
    !std::is_convertible_v<Err, E> /* explicit */
  )>
  constexpr explicit unexpected(unexpected<Err>&& rhs)
  : val_(E(std::move(rhs.value()))) {}

  // assignment
  constexpr unexpected& operator=(const unexpected&) = default;
  constexpr unexpected& operator=(unexpected&&) noexcept(std::is_nothrow_move_assignable_v<E>) =
      default;
  template<class Err = E>
  constexpr unexpected& operator=(const unexpected<Err>& rhs) {
    val_ = rhs.value();
    return *this;
  }
  template<class Err = E>
  constexpr unexpected& operator=(unexpected<Err>&& rhs) {
    val_ = std::forward<E>(rhs.value());
    return *this;
  }

  // observer
  constexpr const E& value() const& noexcept { return val_; }
  constexpr E& value() & noexcept { return val_; }
  constexpr const E&& value() const&& noexcept { return std::move(val_); }
  constexpr E&& value() && noexcept { return std::move(val_); }

  void swap(unexpected& other) noexcept(std::is_nothrow_swappable_v<E>) {
    std::swap(val_, other.val_);
  }

  template<class E1, class E2>
  friend constexpr bool
  operator==(const unexpected<E1>& e1, const unexpected<E2>& e2);
  template<class E1, class E2>
  friend constexpr bool
  operator!=(const unexpected<E1>& e1, const unexpected<E2>& e2);

  template<class E1>
  friend void swap(unexpected<E1>& x, unexpected<E1>& y) noexcept(noexcept(x.swap(y)));

 private:
  E val_;
};

template<class E1, class E2>
constexpr bool
operator==(const unexpected<E1>& e1, const unexpected<E2>& e2) {
  return e1.value() == e2.value();
}

template<class E1, class E2>
constexpr bool
operator!=(const unexpected<E1>& e1, const unexpected<E2>& e2) {
  return e1.value() != e2.value();
}

template<class E1>
void swap(unexpected<E1>& x, unexpected<E1>& y) noexcept(noexcept(x.swap(y))) {
  x.swap(y);
}

// TODO: bad_expected_access class

#undef _ENABLE_IF
#undef _NODISCARD_

}  // namespace base
}  // namespace android