Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/core/StarVariant.hpp
blob: 049779a947f2bcd7ffea06d8c840cf0c59462af1 (plain)
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
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
#pragma once

#include <type_traits>
#include <utility>
#include <initializer_list>

#include "StarAlgorithm.hpp"
#include "StarMaybe.hpp"

namespace Star {

STAR_EXCEPTION(BadVariantCast, StarException);
STAR_EXCEPTION(BadVariantType, StarException);

typedef uint8_t VariantTypeIndex;
VariantTypeIndex const InvalidVariantType = 255;

namespace detail {
  template <typename T, typename... Args>
  struct HasType;

  template <typename T>
  struct HasType<T> : std::false_type {};

  template <typename T, typename Head, typename... Args>
  struct HasType<T, Head, Args...> {
    static constexpr bool value = std::is_same<T, Head>::value || HasType<T, Args...>::value;
  };

  template <typename... Args>
  struct IsNothrowMoveConstructible;

  template <>
  struct IsNothrowMoveConstructible<> : std::true_type {};

  template <typename Head, typename... Args>
  struct IsNothrowMoveConstructible<Head, Args...> {
    static constexpr bool value = std::is_nothrow_move_constructible<Head>::value && IsNothrowMoveConstructible<Args...>::value;
  };

  template <typename... Args>
  struct IsNothrowMoveAssignable;

  template <>
  struct IsNothrowMoveAssignable<> : std::true_type {};

  template <typename Head, typename... Args>
  struct IsNothrowMoveAssignable<Head, Args...> {
    static constexpr bool value = std::is_nothrow_move_assignable<Head>::value && IsNothrowMoveAssignable<Args...>::value;
  };
}

// Stack based variant type container that can be inhabited by one of a limited
// number of types.
template <typename FirstType, typename... RestTypes>
class Variant {
public:
  template <typename T>
  using ValidateType = typename std::enable_if<detail::HasType<T, FirstType, RestTypes...>::value, void>::type;

  template <typename T, typename = ValidateType<T>>
  static constexpr VariantTypeIndex typeIndexOf();

  // If the first type has a default constructor, constructs an Variant which
  // contains a default constructed value of that type.
  Variant();

  template <typename T, typename = ValidateType<T>>
  Variant(T const& x);
  template <typename T, typename = ValidateType<T>>
  Variant(T&& x);

  template <typename T, typename = ValidateType<T>, typename... Args,
    typename std::enable_if< std::is_constructible<T, Args...>::value, int >::type = 0
  >
  Variant(std::in_place_type_t<T>, Args&&... args) {
    new (&m_buffer) T(std::forward<Args>(args)...);
    m_typeIndex = TypeIndex<T>::value;
  }

  template <typename T, typename U, typename = ValidateType<T>, typename... Args,
    typename std::enable_if< std::is_constructible<T, std::initializer_list<U>&, Args...>::value, int >::type = 0
  >
  Variant(std::in_place_type_t<T>, std::initializer_list<U> il, Args&&... args) {
    new (&m_buffer) T(il, std::forward<Args>(args)...);
    m_typeIndex = TypeIndex<T>::value;
  }

  Variant(Variant const& x);
  Variant(Variant&& x) noexcept(detail::IsNothrowMoveConstructible<FirstType, RestTypes...>::value);

  ~Variant();

  // Implementations of operator= may invalidate the Variant if the copy or
  // move constructor of the assigned value throws.
  Variant& operator=(Variant const& x);
  Variant& operator=(Variant&& x) noexcept(detail::IsNothrowMoveAssignable<FirstType, RestTypes...>::value);
  template <typename T, typename = ValidateType<T>>
  Variant& operator=(T const& x);
  template <typename T, typename = ValidateType<T>>
  Variant& operator=(T&& x);

  // Returns true if this Variant contains the given type.
  template <typename T, typename = ValidateType<T>>
  bool is() const;

  // get throws BadVariantCast on bad casts

  template <typename T, typename = ValidateType<T>>
  T const& get() const;

  template <typename T, typename = ValidateType<T>>
  T& get();

  template <typename T, typename = ValidateType<T>>
  Maybe<T> maybe() const;

  // ptr() does not throw if this Variant does not hold the given type, instead
  // simply returns nullptr.

  template <typename T, typename = ValidateType<T>>
  T const* ptr() const;

  template <typename T, typename = ValidateType<T>>
  T* ptr();

  // Calls the given function with the type currently being held, and returns
  // the value returned by that function.  Will throw if this Variant has been
  // invalidated.
  template <typename Function>
  decltype(auto) call(Function&& function);
  template <typename Function>
  decltype(auto) call(Function&& function) const;

  // Returns an index for the held type, which can be passed into makeType to
  // make this Variant hold a specific type.  Returns InvalidVariantType if
  // invalidated.
  VariantTypeIndex typeIndex() const;

  // Make this Variant hold a new default constructed type of the given type
  // index.  Can only be used if every alternative type has a default
  // constructor.  Throws if given an out of range type index or
  // InvalidVariantType.
  void makeType(VariantTypeIndex typeIndex);

  // True if this Variant has been invalidated.  If the copy or move
  // constructor of a type throws an exception during assignment, there is no
  // *good* way to ensure that the Variant has a valid type, so it may become
  // invalidated.  It is not possible to directly construct an invalidated
  // Variant.
  bool invalid() const;

  // Requires that every type included in this Variant has operator==
  bool operator==(Variant const& x) const;
  bool operator!=(Variant const& x) const;

  // Requires that every type included in this Variant has operator<
  bool operator<(Variant const& x) const;

  template <typename T, typename = ValidateType<T>>
  bool operator==(T const& x) const;
  template <typename T, typename = ValidateType<T>>
  bool operator!=(T const& x) const;
  template <typename T, typename = ValidateType<T>>
  bool operator<(T const& x) const;

private:
  template <typename MatchType, VariantTypeIndex Index, typename... Rest>
  struct LookupTypeIndex;

  template <typename MatchType, VariantTypeIndex Index>
  struct LookupTypeIndex<MatchType, Index> {
    static VariantTypeIndex const value = InvalidVariantType;
  };

  template <typename MatchType, VariantTypeIndex Index, typename Head, typename... Rest>
  struct LookupTypeIndex<MatchType, Index, Head, Rest...> {
    static VariantTypeIndex const value = std::is_same<MatchType, Head>::value ? Index : LookupTypeIndex<MatchType, Index + 1, Rest...>::value;
  };

  template <typename MatchType>
  struct TypeIndex {
    static VariantTypeIndex const value = LookupTypeIndex<MatchType, 0, FirstType, RestTypes...>::value;
  };

  void destruct();

  template <typename T>
  void assign(T&& x);

  template <typename Function, typename T>
  decltype(auto) doCall(Function&& function);
  template <typename Function, typename T1, typename T2, typename... TL>
  decltype(auto) doCall(Function&& function);

  template <typename Function, typename T>
  decltype(auto) doCall(Function&& function) const;
  template <typename Function, typename T1, typename T2, typename... TL>
  decltype(auto) doCall(Function&& function) const;

  template <typename First>
  void doMakeType(VariantTypeIndex);
  template <typename First, typename Second, typename... Rest>
  void doMakeType(VariantTypeIndex typeIndex);

  typename std::aligned_union<0, FirstType, RestTypes...>::type m_buffer;
  VariantTypeIndex m_typeIndex = InvalidVariantType;
};

// A version of Variant that has always has a default "empty" state, useful
// when there is no good default type for a Variant but it needs to be default
// constructed, and is slightly more convenient than Maybe<Variant<Types...>>.
template <typename... Types>
class MVariant {
public:
  template <typename T>
  using ValidateType = typename std::enable_if<detail::HasType<T, Types...>::value, void>::type;

  template <typename T, typename = ValidateType<T>>
  static constexpr VariantTypeIndex typeIndexOf();

  MVariant();
  MVariant(MVariant const& x);
  MVariant(MVariant&& x);

  template <typename T, typename = ValidateType<T>>
  MVariant(T const& x);
  template <typename T, typename = ValidateType<T>>
  MVariant(T&& x);

  MVariant(Variant<Types...> const& x);
  MVariant(Variant<Types...>&& x);

  ~MVariant();

  // MVariant::operator= will never invalidate the MVariant, instead it will
  // just become empty.
  MVariant& operator=(MVariant const& x);
  MVariant& operator=(MVariant&& x);

  template <typename T, typename = ValidateType<T>>
  MVariant& operator=(T const& x);
  template <typename T, typename = ValidateType<T>>
  MVariant& operator=(T&& x);

  MVariant& operator=(Variant<Types...> const& x);
  MVariant& operator=(Variant<Types...>&& x);

  // Requires that every type included in this MVariant has operator==
  bool operator==(MVariant const& x) const;
  bool operator!=(MVariant const& x) const;

  // Requires that every type included in this MVariant has operator<
  bool operator<(MVariant const& x) const;

  template <typename T, typename = ValidateType<T>>
  bool operator==(T const& x) const;
  template <typename T, typename = ValidateType<T>>
  bool operator!=(T const& x) const;
  template <typename T, typename = ValidateType<T>>
  bool operator<(T const& x) const;

  // get throws BadVariantCast on bad casts

  template <typename T, typename = ValidateType<T>>
  T const& get() const;

  template <typename T, typename = ValidateType<T>>
  T& get();

  // maybe() and ptr() do not throw if this MVariant does not hold the given
  // type, instead simply returns Nothing / nullptr.

  template <typename T, typename = ValidateType<T>>
  Maybe<T> maybe() const;

  template <typename T, typename = ValidateType<T>>
  T const* ptr() const;

  template <typename T, typename = ValidateType<T>>
  T* ptr();

  template <typename T, typename = ValidateType<T>>
  bool is() const;

  // Takes the given value out and leaves this empty
  template <typename T, typename = ValidateType<T>>
  T take();

  // Returns a Variant of all the allowed types if non-empty, throws
  // BadVariantCast if empty.
  Variant<Types...> value() const;

  // Moves the contents of this MVariant into the given Variant if non-empty,
  // throws BadVariantCast if empty.
  Variant<Types...> takeValue();

  bool empty() const;
  void reset();

  // Equivalent to !empty()
  explicit operator bool() const;

  // If this MVariant holds a type, calls the given function with the type
  // being held.  If nothing is currently held, the function is not called.
  template <typename Function>
  void call(Function&& function);

  template <typename Function>
  void call(Function&& function) const;

  // Returns an index for the held type, which can be passed into makeType to
  // make this MVariant hold a specific type.  Types are always indexed in the
  // order they are specified starting from 1.  A type index of 0 indicates an
  // empty MVariant.
  VariantTypeIndex typeIndex() const;

  // Make this MVariant hold a new default constructed type of the given type
  // index.  Can only be used if every alternative type has a default
  // constructor.
  void makeType(VariantTypeIndex typeIndex);

private:
  struct MVariantEmpty {
    bool operator==(MVariantEmpty const& rhs) const;
    bool operator<(MVariantEmpty const& rhs) const;
  };

  template <typename Function>
  struct RefCaller {
    Function&& function;

    RefCaller(Function&& function);

    void operator()(MVariantEmpty& empty);

    template <typename T>
    void operator()(T& t);
  };

  template <typename Function>
  struct ConstRefCaller {
    Function&& function;

    ConstRefCaller(Function&& function);

    void operator()(MVariantEmpty const& empty);

    template <typename T>
    void operator()(T const& t);
  };

  Variant<MVariantEmpty, Types...> m_variant;
};

template <typename FirstType, typename... RestTypes>
template <typename T, typename>
constexpr VariantTypeIndex Variant<FirstType, RestTypes...>::typeIndexOf() {
  return TypeIndex<T>::value;
}

template <typename FirstType, typename... RestTypes>
Variant<FirstType, RestTypes...>::Variant()
  : Variant(FirstType()) {}

template <typename FirstType, typename... RestTypes>
template <typename T, typename>
Variant<FirstType, RestTypes...>::Variant(T const& x) {
  assign(x);
}

template <typename FirstType, typename... RestTypes>
template <typename T, typename>
Variant<FirstType, RestTypes...>::Variant(T&& x) {
  assign(std::forward<T>(x));
}

template <typename FirstType, typename... RestTypes>
Variant<FirstType, RestTypes...>::Variant(Variant const& x) {
  x.call([&](auto const& t) {
      assign(t);
    });
}

template <typename FirstType, typename... RestTypes>
Variant<FirstType, RestTypes...>::Variant(Variant&& x)
  noexcept(detail::IsNothrowMoveConstructible<FirstType, RestTypes...>::value) {
  x.call([&](auto& t) {
      assign(std::move(t));
    });
}

template <typename FirstType, typename... RestTypes>
Variant<FirstType, RestTypes...>::~Variant() {
  destruct();
}

template <typename FirstType, typename... RestTypes>
Variant<FirstType, RestTypes...>& Variant<FirstType, RestTypes...>::operator=(Variant const& x) {
  if (&x == this)
    return *this;

  x.call([&](auto const& t) {
      assign(t);
    });

  return *this;
}

template <typename FirstType, typename... RestTypes>
Variant<FirstType, RestTypes...>& Variant<FirstType, RestTypes...>::operator=(Variant&& x)
  noexcept(detail::IsNothrowMoveAssignable<FirstType, RestTypes...>::value) {
  if (&x == this)
    return *this;

  x.call([&](auto& t) {
      assign(std::move(t));
    });

  return *this;
}

template <typename FirstType, typename... RestTypes>
template <typename T, typename>
Variant<FirstType, RestTypes...>& Variant<FirstType, RestTypes...>::operator=(T const& x) {
  assign(x);
  return *this;
}

template <typename FirstType, typename... RestTypes>
template <typename T, typename>
Variant<FirstType, RestTypes...>& Variant<FirstType, RestTypes...>::operator=(T&& x) {
  assign(std::forward<T>(x));
  return *this;
}

template <typename FirstType, typename... RestTypes>
template <typename T, typename>
T const& Variant<FirstType, RestTypes...>::get() const {
  if (!is<T>())
    throw BadVariantCast();
  return *(T*)(&m_buffer);
}

template <typename FirstType, typename... RestTypes>
template <typename T, typename>
T& Variant<FirstType, RestTypes...>::get() {
  if (!is<T>())
    throw BadVariantCast();
  return *(T*)(&m_buffer);
}

template <typename FirstType, typename... RestTypes>
template <typename T, typename>
Maybe<T> Variant<FirstType, RestTypes...>::maybe() const {
  if (!is<T>())
    return {};
  return *(T*)(&m_buffer);
}

template <typename FirstType, typename... RestTypes>
template <typename T, typename>
T const* Variant<FirstType, RestTypes...>::ptr() const {
  if (!is<T>())
    return nullptr;
  return (T*)(&m_buffer);
}

template <typename FirstType, typename... RestTypes>
template <typename T, typename>
T* Variant<FirstType, RestTypes...>::ptr() {
  if (!is<T>())
    return nullptr;
  return (T*)(&m_buffer);
}

template <typename FirstType, typename... RestTypes>
template <typename T, typename>
bool Variant<FirstType, RestTypes...>::is() const {
  return m_typeIndex == TypeIndex<T>::value;
}

template <typename FirstType, typename... RestTypes>
template <typename Function>
decltype(auto) Variant<FirstType, RestTypes...>::call(Function&& function) {
  return doCall<Function, FirstType, RestTypes...>(std::forward<Function>(function));
}

template <typename FirstType, typename... RestTypes>
template <typename Function>
decltype(auto) Variant<FirstType, RestTypes...>::call(Function&& function) const {
  return doCall<Function, FirstType, RestTypes...>(std::forward<Function>(function));
}

template <typename FirstType, typename... RestTypes>
VariantTypeIndex Variant<FirstType, RestTypes...>::typeIndex() const {
  return m_typeIndex;
}

template <typename FirstType, typename... RestTypes>
void Variant<FirstType, RestTypes...>::makeType(VariantTypeIndex typeIndex) {
  return doMakeType<FirstType, RestTypes...>(typeIndex);
}

template <typename FirstType, typename... RestTypes>
bool Variant<FirstType, RestTypes...>::invalid() const {
  return m_typeIndex == InvalidVariantType;
}

template <typename FirstType, typename... RestTypes>
bool Variant<FirstType, RestTypes...>::operator==(Variant const& x) const {
  if (this == &x) {
    return true;
  } else if (typeIndex() != x.typeIndex()) {
    return false;
  } else {
    return call([&x](auto const& t) {
        typedef typename std::decay<decltype(t)>::type T;
        return t == x.template get<T>();
      });
  }
}

template <typename FirstType, typename... RestTypes>
bool Variant<FirstType, RestTypes...>::operator!=(Variant const& x) const {
  return !operator==(x);
}

template <typename FirstType, typename... RestTypes>
bool Variant<FirstType, RestTypes...>::operator<(Variant const& x) const {
  if (this == &x) {
    return false;
  } else {
    auto sti = typeIndex();
    auto xti = x.typeIndex();
    if (sti != xti) {
      return sti < xti;
    } else {
      return call([&x](auto const& t) {
          typedef typename std::decay<decltype(t)>::type T;
          return t < x.template get<T>();
        });
    }
  }
}

template <typename FirstType, typename... RestTypes>
template <typename T, typename>
bool Variant<FirstType, RestTypes...>::operator==(T const& x) const {
  if (auto p = ptr<T>())
    return *p == x;
  return false;
}

template <typename FirstType, typename... RestTypes>
template <typename T, typename>
bool Variant<FirstType, RestTypes...>::operator!=(T const& x) const {
  return !operator==(x);
}

template <typename FirstType, typename... RestTypes>
template <typename T, typename>
bool Variant<FirstType, RestTypes...>::operator<(T const& x) const {
  if (auto p = ptr<T>())
    return *p == x;
  return m_typeIndex < TypeIndex<T>::value;
}

template <typename FirstType, typename... RestTypes>
void Variant<FirstType, RestTypes...>::destruct() {
  if (m_typeIndex != InvalidVariantType) {
    try {
      call([](auto& t) {
          typedef typename std::decay<decltype(t)>::type T;
          t.~T();
        });
      m_typeIndex = InvalidVariantType;
    } catch (...) {
      m_typeIndex = InvalidVariantType;
      throw;
    }
  }
}

template <typename FirstType, typename... RestTypes>
template <typename T>
void Variant<FirstType, RestTypes...>::assign(T&& x) {
  typedef typename std::decay<T>::type AssignType;
  if (auto p = ptr<AssignType>()) {
    *p = std::forward<T>(x);
  } else {
    destruct();
    new (&m_buffer) AssignType(std::forward<T>(x));
    m_typeIndex = TypeIndex<AssignType>::value;
  }
}

template <typename FirstType, typename... RestTypes>
template <typename Function, typename T>
decltype(auto) Variant<FirstType, RestTypes...>::doCall(Function&& function) {
  if (T* p = ptr<T>())
    return function(*p);
  else
    throw BadVariantType();
}

template <typename FirstType, typename... RestTypes>
template <typename Function, typename T1, typename T2, typename... TL>
decltype(auto) Variant<FirstType, RestTypes...>::doCall(Function&& function) {
  if (T1* p = ptr<T1>())
    return function(*p);
  else
    return doCall<Function, T2, TL...>(std::forward<Function>(function));
}

template <typename FirstType, typename... RestTypes>
template <typename Function, typename T>
decltype(auto) Variant<FirstType, RestTypes...>::doCall(Function&& function) const {
  if (T const* p = ptr<T>())
    return function(*p);
  else
    throw BadVariantType();
}

template <typename FirstType, typename... RestTypes>
template <typename Function, typename T1, typename T2, typename... TL>
decltype(auto) Variant<FirstType, RestTypes...>::doCall(Function&& function) const {
  if (T1 const* p = ptr<T1>())
    return function(*p);
  else
    return doCall<Function, T2, TL...>(std::forward<Function>(function));
}

template <typename FirstType, typename... RestTypes>
template <typename First>
void Variant<FirstType, RestTypes...>::doMakeType(VariantTypeIndex typeIndex) {
  if (typeIndex == 0)
    *this = First();
  else
    throw BadVariantType();
}

template <typename FirstType, typename... RestTypes>
template <typename First, typename Second, typename... Rest>
void Variant<FirstType, RestTypes...>::doMakeType(VariantTypeIndex typeIndex) {
  if (typeIndex == 0)
    *this = First();
  else
    return doMakeType<Second, Rest...>(typeIndex - 1);
}

template <typename... Types>
template <typename T, typename>
constexpr VariantTypeIndex MVariant<Types...>::typeIndexOf() {
  return Variant<MVariantEmpty, Types...>::template typeIndexOf<T>();
}

template <typename... Types>
MVariant<Types...>::MVariant() {}

template <typename... Types>
MVariant<Types...>::MVariant(MVariant const& x)
  : m_variant(x.m_variant) {}

template <typename... Types>
MVariant<Types...>::MVariant(MVariant&& x) {
  m_variant = std::move(x.m_variant);
  x.m_variant = MVariantEmpty();
}

template <typename... Types>
MVariant<Types...>::MVariant(Variant<Types...> const& x) {
  operator=(x);
}

template <typename... Types>
MVariant<Types...>::MVariant(Variant<Types...>&& x) {
  operator=(std::move(x));
}

template <typename... Types>
template <typename T, typename>
MVariant<Types...>::MVariant(T const& x)
  : m_variant(x) {}

template <typename... Types>
template <typename T, typename>
MVariant<Types...>::MVariant(T&& x)
  : m_variant(std::forward<T>(x)) {}

template <typename... Types>
MVariant<Types...>::~MVariant() {}

template <typename... Types>
MVariant<Types...>& MVariant<Types...>::operator=(MVariant const& x) {
  try {
    m_variant = x.m_variant;
  } catch (...) {
    if (m_variant.invalid())
      m_variant = MVariantEmpty();
    throw;
  }
  return *this;
}

template <typename... Types>
MVariant<Types...>& MVariant<Types...>::operator=(MVariant&& x) {
  try {
    m_variant = std::move(x.m_variant);
  } catch (...) {
    if (m_variant.invalid())
      m_variant = MVariantEmpty();
    throw;
  }
  return *this;
}

template <typename... Types>
template <typename T, typename>
MVariant<Types...>& MVariant<Types...>::operator=(T const& x) {
  try {
    m_variant = x;
  } catch (...) {
    if (m_variant.invalid())
      m_variant = MVariantEmpty();
    throw;
  }
  return *this;
}

template <typename... Types>
template <typename T, typename>
MVariant<Types...>& MVariant<Types...>::operator=(T&& x) {
  try {
    m_variant = std::forward<T>(x);
  } catch (...) {
    if (m_variant.invalid())
      m_variant = MVariantEmpty();
    throw;
  }
  return *this;
}

template <typename... Types>
MVariant<Types...>& MVariant<Types...>::operator=(Variant<Types...> const& x) {
  x.call([this](auto const& t) {
      *this = t;
    });
  return *this;
}

template <typename... Types>
MVariant<Types...>& MVariant<Types...>::operator=(Variant<Types...>&& x) {
  x.call([this](auto& t) {
      *this = std::move(t);
    });
  return *this;
}

template <typename... Types>
bool MVariant<Types...>::operator==(MVariant const& x) const {
  return m_variant == x.m_variant;
}

template <typename... Types>
bool MVariant<Types...>::operator!=(MVariant const& x) const {
  return m_variant != x.m_variant;
}

template <typename... Types>
bool MVariant<Types...>::operator<(MVariant const& x) const {
  return m_variant < x.m_variant;
}

template <typename... Types>
template <typename T, typename>
bool MVariant<Types...>::operator==(T const& x) const {
  return m_variant == x;
}

template <typename... Types>
template <typename T, typename>
bool MVariant<Types...>::operator!=(T const& x) const {
  return m_variant != x;
}

template <typename... Types>
template <typename T, typename>
bool MVariant<Types...>::operator<(T const& x) const {
  return m_variant < x;
}

template <typename... Types>
template <typename T, typename>
T const& MVariant<Types...>::get() const {
  return m_variant.template get<T>();
}

template <typename... Types>
template <typename T, typename>
T& MVariant<Types...>::get() {
  return m_variant.template get<T>();
}

template <typename... Types>
template <typename T, typename>
Maybe<T> MVariant<Types...>::maybe() const {
  return m_variant.template maybe<T>();
}

template <typename... Types>
template <typename T, typename>
T const* MVariant<Types...>::ptr() const {
  return m_variant.template ptr<T>();
}

template <typename... Types>
template <typename T, typename>
T* MVariant<Types...>::ptr() {
  return m_variant.template ptr<T>();
}

template <typename... Types>
template <typename T, typename>
bool MVariant<Types...>::is() const {
  return m_variant.template is<T>();
}

template <typename... Types>
template <typename T, typename>
T MVariant<Types...>::take() {
  T t = std::move(m_variant.template get<T>());
  m_variant = MVariantEmpty();
  return t;
}

template <typename... Types>
Variant<Types...> MVariant<Types...>::value() const {
  if (empty())
    throw BadVariantCast();

  Variant<Types...> r;
  call([&r](auto const& v) {
      r = v;
    });
  return r;
}

template <typename... Types>
Variant<Types...> MVariant<Types...>::takeValue() {
  if (empty())
    throw BadVariantCast();

  Variant<Types...> r;
  call([&r](auto& v) {
      r = std::move(v);
    });
  m_variant = MVariantEmpty();
  return r;
}

template <typename... Types>
bool MVariant<Types...>::empty() const {
  return m_variant.template is<MVariantEmpty>();
}

template <typename... Types>
void MVariant<Types...>::reset() {
  m_variant = MVariantEmpty();
}

template <typename... Types>
MVariant<Types...>::operator bool() const {
  return !empty();
}

template <typename... Types>
template <typename Function>
void MVariant<Types...>::call(Function&& function) {
  m_variant.call(RefCaller<Function>(std::forward<Function>(function)));
}

template <typename... Types>
template <typename Function>
void MVariant<Types...>::call(Function&& function) const {
  m_variant.call(ConstRefCaller<Function>(std::forward<Function>(function)));
}

template <typename... Types>
VariantTypeIndex MVariant<Types...>::typeIndex() const {
  return m_variant.typeIndex();
}

template <typename... Types>
void MVariant<Types...>::makeType(VariantTypeIndex typeIndex) {
  m_variant.makeType(typeIndex);
}

template <typename... Types>
bool MVariant<Types...>::MVariantEmpty::operator==(MVariantEmpty const&) const {
  return true;
}

template <typename... Types>
bool MVariant<Types...>::MVariantEmpty::operator<(MVariantEmpty const&) const {
  return false;
}

template <typename... Types>
template <typename Function>
MVariant<Types...>::RefCaller<Function>::RefCaller(Function&& function)
  : function(std::forward<Function>(function)) {}

template <typename... Types>
template <typename Function>
void MVariant<Types...>::RefCaller<Function>::operator()(MVariantEmpty&) {}

template <typename... Types>
template <typename Function>
template <typename T>
void MVariant<Types...>::RefCaller<Function>::operator()(T& t) {
  function(t);
}

template <typename... Types>
template <typename Function>
MVariant<Types...>::ConstRefCaller<Function>::ConstRefCaller(Function&& function)
  : function(std::forward<Function>(function)) {}

template <typename... Types>
template <typename Function>
void MVariant<Types...>::ConstRefCaller<Function>::operator()(MVariantEmpty const&) {}

template <typename... Types>
template <typename Function>
template <typename T>
void MVariant<Types...>::ConstRefCaller<Function>::operator()(T const& t) {
  function(t);
}

}

template <typename FirstType, typename... RestTypes>
struct fmt::formatter<Star::Variant<FirstType, RestTypes...>> : ostream_formatter {};