File: dexpected.h

package info (click to toggle)
dtkcore 5.7.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,728 kB
  • sloc: cpp: 22,021; ansic: 183; python: 68; xml: 58; makefile: 27; sh: 15
file content (1520 lines) | stat: -rw-r--r-- 55,056 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
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
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#ifndef DEXPECTED_H
#define DEXPECTED_H

#include <cassert>
#include <cstdlib>
#include <exception>
#include <initializer_list>
#include <memory>
#include <type_traits>

#include "derror.h"

DCORE_BEGIN_NAMESPACE

#define likely(x) __builtin_expect(static_cast<long int>((x)), 1)
#define unlikely(x) __builtin_expect(reinterpret_cast<long int>((x)), 0)

#if __cpp_exceptions
#define _DEXPECTED_THROW_OR_ABORT(_EXC) (throw(_EXC))
#else
#define _DEXPECTED_THROW_OR_ABORT(_EXC) (std::abort())
#endif

template <typename T, typename E>
class DExpected;

template <typename E>
class DUnexpected;

template <bool v>
using _bool_constant = std::integral_constant<bool, v>;

/*!
 * @brief 原位构造标签
 */
enum class emplace_tag { USE_EMPLACE /**< 使用原位构造 */ };

/*!
 * @brief 从DUnexpected构造标签
 */
enum class dunexpected_tag { DUNEXPECTED /**< 从DUnexpected构造 */ };

template <typename Type>
struct remove_cvref
{
    using type = typename std::remove_cv<typename std::remove_reference<Type>::type>::type;
};

template <typename E>
class bad_result_access;

template <>
class bad_result_access<void> : public std::exception
{
protected:
    bad_result_access() noexcept {}
    bad_result_access(const bad_result_access &) = default;
    bad_result_access(bad_result_access &&) = default;
    bad_result_access &operator=(const bad_result_access &) = default;
    bad_result_access &operator=(bad_result_access &&) = default;
    ~bad_result_access() = default;

public:
    const char *what() const noexcept override { return "bad access to DExpected without value"; }
};

template <typename E>
class bad_result_access : public bad_result_access<void>
{
public:
    explicit bad_result_access(E _e)
        : m_error(std::move(_e))
    {
    }

    E &error() &noexcept { return m_error; }
    const E &error() const &noexcept { return m_error; }

    E error() &&noexcept { return std::move(m_error); }
    const E error() const &&noexcept { return std::move(m_error); }

private:
    E m_error;
};

template <typename Type, typename... Args>
auto construct_at(Type *p, Args &&...args) noexcept(noexcept(::new((void *)0) Type(std::declval<Args>()...)))
    -> decltype(::new((void *)0) Type(std::declval<Args>()...))
{
    return ::new ((void *)p) Type(std::forward<Args>(args)...);
}

namespace __dexpected {
template <typename ObjType>
void destroy_at_obj(ObjType *p)
{
    p->~ObjType();
}

template <typename ArrType>
void destroy_at_arr(ArrType *p)
{
    for (auto &elem : *p)
        destroy_at_obj(std::addressof(elem));
}
}  // namespace __dexpected

template <typename Type, typename std::enable_if<std::is_array<Type>::value, bool>::type = true>
void destroy_at(Type *p)
{
    __dexpected::destroy_at_arr(p);
}

template <typename Type, typename std::enable_if<!std::is_array<Type>::value, bool>::type = true>
void destroy_at(Type *p)
{
    __dexpected::destroy_at_obj(p);
}

namespace __dexpected {
template <typename T>
struct Guard
{
    static_assert(std::is_nothrow_move_constructible<T>::value, "type T must bu nothrow_move_constructible");
    explicit Guard(T &_x)
        : m_guarded(std::addressof(_x))
        , m_tmp(std::move(_x))
    {
        destroy_at(m_guarded);
    }

    ~Guard()
    {
        if (unlikely(m_guarded)) {
            construct_at(m_guarded, std::move(m_tmp));
        }
    }
    Guard(const Guard &) = delete;
    Guard &operator=(const Guard &) = delete;

    T &&release() noexcept { return std::move(m_tmp); }

private:
    T *m_guarded;
    T m_tmp;
};
}  // namespace __dexpected

namespace __dexpected {

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

template <typename T, typename E>
struct _is_dexpected<DExpected<T, E>> : public std::true_type
{
};

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

template <typename T>
struct _is_dunexpected<DUnexpected<T>> : public std::true_type
{
};

template <typename E>
constexpr bool _can_be_dunexpected()
{
    return std::is_object<E>::value and !std::is_array<E>::value and !_is_dunexpected<E>() and !std::is_const<E>::value and
           !std::is_volatile<E>::value;
}

template <typename Tp,
          typename Up,
          typename Vp,
          typename std::enable_if<std::is_nothrow_constructible<Tp, Vp>::value and std::is_nothrow_move_constructible<Tp>::value,
                                  bool>::type = true>
void reinit(Tp *_newVal, Up *_oldVal, Vp &&_arg) noexcept(std::is_nothrow_constructible<Tp, Vp>::value)
{
    destroy_at(_oldVal);
    construct_at(_newVal, std::forward<Vp>(_arg));
}

template <typename Tp,
          typename Up,
          typename Vp,
          typename std::enable_if<std::is_nothrow_constructible<Tp, Vp>::value and !std::is_nothrow_move_constructible<Tp>::value,
                                  bool>::type = true>
void reinit(Tp *_newVal, Up *_oldVal, Vp &&_arg) noexcept(std::is_nothrow_constructible<Tp, Vp>::value)
{
    destroy_at(_oldVal);
    construct_at(_newVal, std::forward<Vp>(_arg));
}

template <typename Tp,
          typename Up,
          typename Vp,
          typename std::enable_if<!std::is_nothrow_constructible<Tp, Vp>::value and std::is_nothrow_move_constructible<Tp>::value,
                                  bool>::type = true>
void reinit(Tp *_newVal, Up *_oldVal, Vp &&_arg) noexcept(std::is_nothrow_constructible<Tp, Vp>::value)
{
    Tp _tmp(std::forward<Vp>(_arg));
    destroy_at(_oldVal);
    construct_at(_newVal, std::move(_tmp));
}

template <
    typename Tp,
    typename Up,
    typename Vp,
    typename std::enable_if<!std::is_nothrow_constructible<Tp, Vp>::value and !std::is_nothrow_move_constructible<Tp>::value,
                            bool>::type = true>
void reinit(Tp *_newVal, Up *_oldVal, Vp &&_arg) noexcept(std::is_nothrow_constructible<Tp, Vp>::value)
{
    __dexpected::Guard<Up> _guard(*_oldVal);
    construct_at(_newVal, std::forward<Vp>(_arg));
    _guard.release();
}

}  // namespace __dexpected

/*!
 * @brief
 * 类模板Dtk::Core::DUnexpected代表一个Dtk::Core::DExpected中存储的不期待的值
 * @tparam E
 * 不期待的值的类型,该类型不能是非对象类型,数组类型,Dtk::Core::DUnexpected的特化类型或有cv限定符的类型
 */
template <typename E = DError>
class DUnexpected
{
    static_assert(__dexpected::_can_be_dunexpected<E>(), "can't be dunexpected");

public:
    /*!
     * @brief Dtk::Core::DUnexpected的默认拷贝构造函数
     */
    constexpr DUnexpected(const DUnexpected &) = default;

    /*!
     * @brief Dtk::Core::DUnexpected的默认移动构造函数
     */
    constexpr DUnexpected(DUnexpected &&) = default;

    /*!
     * @brief 使用类型E直接初始化一个Dtk::Core::DUnexpected对象
     * @tparam Er 错误类型,默认为E
     * @param[in] _e 一个类型为Er的值
     */
    template <typename Er = E,
              typename std::enable_if<!std::is_same<typename remove_cvref<Er>::type, DUnexpected>::value and
                                          !std::is_same<typename remove_cvref<Er>::type, emplace_tag>::value and
                                          std::is_constructible<E, Er>::value,
                                      bool>::type = true>
    constexpr explicit DUnexpected(Er &&_e) noexcept(std::is_nothrow_constructible<E, Er>::value)
        : m_error(std::forward<Er>(_e))
    {
    }

    /*!
     * @brief
     * 直接从参数构造出一个包含错误类型为E的对象的Dtk::Core::DUnexpected对象
     * @tparam Args 可变参数模板类型,这里是构造类型为E的对象所需要的参数的类型
     * @param[in] args 构造类型为E的对象用到的参数
     * @attention
     * 为了区分是构造E还是Dtk::Core::DUnexpected,需要在第一个参数使用emplace_tag进行标识
     */
    template <typename... Args>
    constexpr explicit DUnexpected(emplace_tag, Args &&...args) noexcept(std::is_nothrow_constructible<E, Args...>::value)
        : m_error(std::forward<Args>(args)...)
    {
        static_assert(std::is_constructible<E, Args...>::value, "can't construct E from args.");
    }

    /*!
     * @brief
     * 从参数和初始化列表构造出一个包含错误类型为E的对象的Dtk::Core::DUnexpected对象
     * @tparam U 初始化列表的模板类型
     * @tparam Args 可变参数模板类型,这里是构造类型为E的对象所需要的参数的类型
     * @param _li 模板类型为U的初始化列表
     * @param[in] args 构造类型为E的对象用到的参数
     * @attention
     * 为了区分是构造E还是Dtk::Core::DUnexpected,需要在第一个参数使用emplace_tag进行标识
     */
    template <typename U, typename... Args>
    constexpr explicit DUnexpected(emplace_tag, std::initializer_list<U> _li, Args &&...args) noexcept(
        std::is_nothrow_constructible<E, std::initializer_list<U> &, Args...>::value)
        : m_error(_li, std::forward<Args>(args)...)
    {
    }

    /*!
     * @brief Dtk::Core::DUnexpected的默认拷贝赋值运算符
     */
    DUnexpected &operator=(const DUnexpected &) = default;

    /*!
     * @brief Dtk::Core::DUnexpected的默认移动赋值运算符
     */
    DUnexpected &operator=(DUnexpected &&) = default;

    /*!
     * @brief 获取Dtk::Core::DUnexpected持有的不期待值
     * @return 不期待值的const左值引用
     */
    constexpr const E &error() const &noexcept { return m_error; }

    /*!
     * @brief 获取Dtk::Core::DUnexpected持有的不期待值
     * @return 不期待值的左值引用
     */
    E &error() &noexcept { return m_error; }

    /*!
     * @brief 获取Dtk::Core::DUnexpected持有的不期待值
     * @attention 获取后原Dtk::Core::DUnexpected不可用
     * @return 不期待值的const右值引用
     */
    constexpr const E &&error() const &&noexcept { return std::move(m_error); }

    /*!
     * @brief 获取Dtk::Core::DUnexpected持有的不期待值
     * @attention 获取后原Dtk::Core::DUnexpected不可用
     * @return 不期待值的右值引用
     */
    E &&error() &&noexcept { return std::move(m_error); }

    /*!
     * @brief 交换两个Dtk::Core::DUnexpected的值
     * @param[in] _other 另一个模板参数为E的Dtk::Core::DUnexpected对象
     */
    void swap(DUnexpected &_other)
    {
        using std::swap;
        swap(m_error, _other.m_error);
    }

    /*!
     * @brief 重载相等运算符
     * @tparam Er 另一个Dtk::Core::DUnexpected的模板类型
     * @param[in] _x 模板参数为E的Dtk::Core::DUnexpected对象
     * @param[in] _y 模板参数为Er的Dtk::Core::DUnexpected对象
     */
    template <typename Er>
    friend constexpr bool operator==(const DUnexpected &_x, const DUnexpected<Er> _y)
    {
        return _x.m_error == _y.error();
    }

    /*!
     * @brief 交换两个Dtk::Core::DUnexpected的值
     */
    friend void swap(DUnexpected &_x, DUnexpected &_y) { _x.swap(_y); }

private:
    E m_error;
};

/*!
 * @brief
 * 模板类Dtk::Core::DExpected提供存储两个值之一的方式。Dtk::Core::DExpected的对象要么保有一个期待的T类型值,要么保有一个不期待的E类型值,不会没有值。
 * @tparam T 期待的类型
 * @tparam E 不期待的类型
 * @note 该类自DtkCore 5.6.3引入
 */
template <typename T, typename E = DError>
class DExpected
{
    template <typename, typename>
    friend class DExpected;
    static_assert(!std::is_reference<T>::value, "type T can't be reference type");
    static_assert(!std::is_function<T>::value, "type T can't be function type");
    static_assert(!std::is_same<typename std::remove_cv<T>::type, dunexpected_tag>::value, "type T can't be dunexpected_tag");
    static_assert(!std::is_same<typename std::remove_cv<T>::type, emplace_tag>::value, "type T can't be emplace_tag");
    static_assert(!__dexpected::_is_dunexpected<typename std::remove_cv<T>::type>::value, "type T can't be DUnexpected");
    static_assert(__dexpected::_can_be_dunexpected<E>(), "type E can't be dunexpected");

    template <typename U, typename G, typename Unex = DUnexpected<E>>
    static constexpr bool __cons_from_DExpected()
    {
        return std::is_constructible<T, DExpected<U, G> &>::value or std::is_constructible<T, DExpected<U, G>>::value or
               std::is_constructible<T, const DExpected<U, G>>::value or
               std::is_constructible<T, const DExpected<U, G> &>::value or std::is_convertible<DExpected<U, G> &, T>::value or
               std::is_convertible<DExpected<U, G>, T>::value or std::is_convertible<const DExpected<U, G> &, T>::value or
               std::is_convertible<const DExpected<U, G>, T>::value or std::is_constructible<Unex, DExpected<U, G> &>::value or
               std::is_constructible<Unex, DExpected<U, G>>::value or
               std::is_constructible<Unex, const DExpected<U, G> &>::value or
               std::is_constructible<Unex, const DExpected<U, G>>::value;
    }

    template <typename U, typename G>
    static constexpr bool __explicit_conv()
    {
        return !std::is_convertible<U, T>::value or !std::is_convertible<G, E>::value;
    }

    static constexpr bool des_value()
    {
        return !std::is_trivially_destructible<T>::value or !std::is_trivially_destructible<E>::value;
    }

    template <typename V>
    void assign_val(V &&_v)
    {
        if (m_has_value) {
            m_value = std::forward<V>(_v);
        } else {
            __dexpected::reinit(std::addressof(m_value), std::addressof(m_error), std::forward<V>(_v));
            m_has_value = true;
        }
    }

    template <typename V>
    void assign_err(V &&_v)
    {
        if (m_has_value) {
            __dexpected::reinit(std::addressof(m_error), std::addressof(m_value), std::forward<V>(_v));
            m_has_value = false;
        } else {
            m_error = std::forward<V>(_v);
        }
    }

    template <typename Ep = E, typename std::enable_if<std::is_nothrow_move_constructible<Ep>::value, bool>::type = true>
    void swap_val_err(DExpected &_other) noexcept(
        std::is_nothrow_move_constructible<Ep>::value and std::is_nothrow_move_constructible<T>::value)
    {
        __dexpected::Guard<E> _guard(_other.m_error);
        construct_at(std::addressof(_other.m_value), std::move(m_value));
        _other.m_has_value = true;
        destroy_at(std::addressof(m_value));
        construct_at(std::addressof(m_error), _guard.release());
        m_has_value = false;
    }

    template <typename Ep = E, typename std::enable_if<!std::is_nothrow_move_constructible<Ep>::value, bool>::type = true>
    void swap_val_err(DExpected &_other) noexcept(
        std::is_nothrow_move_constructible<Ep>::value and std::is_nothrow_move_constructible<T>::value)
    {
        __dexpected::Guard<T> _guard(_other.m_value);
        construct_at(std::addressof(m_error), std::move(_other.m_error));
        m_has_value = false;
        destroy_at(std::addressof(_other.m_error));
        construct_at(std::addressof(_other.m_value), _guard.release());
        _other.m_has_value = true;
    }

public:
    using value_type = T;
    using error_type = E;
    using dunexpected_type = DUnexpected<E>;
    template <typename U>
    using rebind = DExpected<U, error_type>;

    /*!
     * @brief Dtk::Core::DExpected的默认构造函数
     */
    template <typename std::enable_if<std::is_default_constructible<T>::value, bool>::type = true>
    constexpr DExpected() noexcept(std::is_nothrow_default_constructible<T>::value)
        : m_has_value(true)
        , m_value()
    {
    }

    /*!
     * @brief Dtk::Core::DExpected的拷贝构造函数
     */
    template <
        typename std::enable_if<std::is_copy_constructible<T>::value and std::is_copy_constructible<E>::value, bool>::type = true>
    DExpected(const DExpected &_x) noexcept(
        std::is_nothrow_copy_constructible<T>::value and std::is_nothrow_copy_constructible<E>::value)
        : m_has_value(_x.m_has_value)
    {
        if (m_has_value)
            construct_at(std::addressof(m_value), _x.m_value);
        else
            construct_at(std::addressof(m_error), _x.m_error);
    }

    /*!
     * @brief Dtk::Core::DExpected的移动构造函数
     */
    template <
        typename std::enable_if<std::is_move_constructible<T>::value and std::is_move_constructible<E>::value, bool>::type = true>
    DExpected(DExpected &&_x) noexcept(
        std::is_nothrow_move_constructible<T>::value and std::is_nothrow_move_constructible<E>::value)
        : m_has_value(_x.m_has_value)
    {
        if (m_has_value)
            construct_at(std::addressof(m_value), std::move(_x).m_value);
        else
            construct_at(std::addressof(m_error), std::move(_x).m_error);
    }

    /*!
     * @brief Dtk::Core::DExpected的拷贝构造函数
     * @tparam U 另一个Dtk::Core::DExpected的期待类型
     * @tparam G 另一个Dtk::Core::DExpected的不期待类型
     * @param[in] _x 模板类型分别为U和G的Dtk::Core::DExpected对象
     */
    template <
        typename U,
        typename G,
        typename std::enable_if<std::is_constructible<T, const U &>::value and std::is_constructible<E, const G &>::value and
                                    !__cons_from_DExpected<U, G>() and !__explicit_conv<const U &, const G &>(),
                                bool>::type = true>
    DExpected(const DExpected<U, G> &_x) noexcept(
        std::is_nothrow_constructible<T, const U &>::value and std::is_nothrow_constructible<E, const G &>::value)
        : m_has_value(_x.m_has_value)
    {
        if (m_has_value)
            construct_at(std::addressof(m_value), _x.m_value);
        else
            construct_at(std::addressof(m_error), _x.m_error);
    }

    /*!
     * @brief Dtk::Core::DExpected的拷贝构造函数
     * @tparam U 另一个Dtk::Core::DExpected的期待类型
     * @tparam G 另一个Dtk::Core::DExpected的不期待类型
     * @param[in] _x 模板类型分别为U和G的Dtk::Core::DExpected对象
     * @attention 该拷贝构造函数有explicit标识
     */
    template <
        typename U,
        typename G,
        typename std::enable_if<std::is_constructible<T, const U &>::value and std::is_constructible<E, const G &>::value and
                                    !__cons_from_DExpected<U, G>() and __explicit_conv<const U &, const G &>(),
                                bool>::type = true>
    explicit DExpected(const DExpected<U, G> &_x) noexcept(
        std::is_nothrow_constructible<T, const U &>::value and std::is_nothrow_constructible<E, const G &>::value)
        : m_has_value(_x.m_has_value)
    {
        if (m_has_value)
            construct_at(std::addressof(m_value), _x.m_value);
        else
            construct_at(std::addressof(m_error), _x.m_error);
    }

    /*!
     * @brief Dtk::Core::DExpected的移动构造函数
     * @tparam U 另一个Dtk::Core::DExpected的期待类型
     * @tparam G 另一个Dtk::Core::DExpected的不期待类型
     * @param[in] _x 模板类型分别为U和G的Dtk::Core::DExpected对象
     * @attention 构造后另一个Dtk::Core::DExpected不可用
     */
    template <typename U,
              typename G,
              typename std::enable_if<std::is_constructible<T, U>::value and std::is_constructible<E, G>::value and
                                          !__cons_from_DExpected<U, G>() and !__explicit_conv<U, G>(),
                                      bool>::type = true>
    DExpected(DExpected<U, G> &&_x) noexcept(
        std::is_nothrow_constructible<T, U>::value and std::is_nothrow_constructible<E, G>::value)
        : m_has_value(_x.m_has_value)
    {
        if (m_has_value)
            construct_at(std::addressof(m_value), std::move(_x).m_value);
        else
            construct_at(std::addressof(m_error), std::move(_x).m_error);
    }

    /*!
     * @brief Dtk::Core::DExpected的移动构造函数
     * @tparam U 另一个Dtk::Core::DExpected的期待类型
     * @tparam G 另一个Dtk::Core::DExpected的不期待类型
     * @param[in] _x 模板类型分别为U和G的Dtk::Core::DExpected对象
     * @attention 构造后另一个Dtk::Core::DExpected不可用,该函数有explicit标识
     */
    template <typename U,
              typename G,
              typename std::enable_if<std::is_constructible<T, U>::value and std::is_constructible<E, G>::value and
                                          !__cons_from_DExpected<U, G>() and __explicit_conv<U, G>(),
                                      bool>::type = true>
    explicit DExpected(DExpected<U, G> &&_x) noexcept(
        std::is_nothrow_constructible<T, U>::value and std::is_nothrow_constructible<E, G>::value)
        : m_has_value(_x.m_has_value)
    {
        if (m_has_value)
            construct_at(std::addressof(m_value), std::move(_x).m_value);
        else
            construct_at(std::addressof(m_error), std::move(_x).m_error);
    }

    /*!
     * @brief
     * Dtk::Core::DExpected的移动构造函数,直接从期待类型构造出Dtk::Core::DExpected对象
     * @tparam U Dtk::Core::DExpected的期待类型,默认为类型T
     * @param[in] _v 期待类型为U的对象
     * @attention 构造后原对象不可用,该函数有explicit标识
     */
    template <typename U = T,
              typename std::enable_if<!std::is_same<typename remove_cvref<U>::type, DExpected>::value and
                                          !std::is_same<typename remove_cvref<U>::type, emplace_tag>::value and
                                          !__dexpected::_is_dunexpected<typename remove_cvref<U>::type>::value and
                                          std::is_constructible<T, U>::value and !std::is_convertible<U, T>::value,
                                      bool>::type = true>
    constexpr explicit DExpected(U &&_v) noexcept(std::is_nothrow_constructible<T, U>::value)
        : m_has_value(true)
        , m_value(std::forward<U>(_v))

    {
    }

    /*!
     * @brief
     * Dtk::Core::DExpected的移动构造函数,直接从期待类型构造出Dtk::Core::DExpected对象
     * @tparam U Dtk::Core::DExpected的期待类型,默认为类型T
     * @param[in] _v 期待类型为U的对象
     * @attention 构造后原对象不可用
     */
    template <typename U = T,
              typename std::enable_if<!std::is_same<typename remove_cvref<U>::type, DExpected>::value and
                                          !std::is_same<typename remove_cvref<U>::type, emplace_tag>::value and
                                          !__dexpected::_is_dunexpected<typename remove_cvref<U>::type>::value and
                                          std::is_constructible<T, U>::value and std::is_convertible<U, T>::value,
                                      bool>::type = true>
    constexpr DExpected(U &&_v) noexcept(std::is_nothrow_constructible<T, U>::value)
        : m_has_value(true)
        , m_value(std::forward<U>(_v))
    {
    }

    /*!
     * @brief
     * Dtk::Core::DExpected的拷贝构造函数,从Dtk::Core::DUnexpected构造出Dtk::Core::DExpected对象
     * @tparam G Dtk::Core::DExpected的期待类型,默认为类型E
     * @param[in] _u 期待类型为G的Dtk::Core::DUnexpected对象
     * @attention 该函数有explicit标识
     */
    template <typename G = E,
              typename std::enable_if<std::is_constructible<E, const G &>::value and !std::is_convertible<const G &, E>::value,
                                      bool>::type = true>
    constexpr explicit DExpected(const DUnexpected<G> &_u) noexcept(std::is_nothrow_constructible<E, const G &>::value)
        : m_has_value(false)
        , m_error(_u.error())
    {
    }

    /*!
     * @brief
     * Dtk::Core::DExpected的拷贝构造函数,从Dtk::Core::DUnexpected构造出Dtk::Core::DExpected对象
     * @tparam G Dtk::Core::DExpected的期待类型,默认为类型E
     * @param[in] _u 期待类型为G的Dtk::Core::DUnexpected对象
     */
    template <typename G = E,
              typename std::enable_if<std::is_constructible<E, const G &>::value and std::is_convertible<const G &, E>::value,
                                      bool>::type = true>
    constexpr DExpected(const DUnexpected<G> &_u) noexcept(std::is_nothrow_constructible<E, const G &>::value)
        : m_has_value(false)
        , m_error(_u.error())
    {
    }

    /*!
     * @brief
     * Dtk::Core::DExpected的移动构造函数,从Dtk::Core::DUnexpected构造出Dtk::Core::DExpected对象
     * @tparam G Dtk::Core::DExpected的期待类型,默认为类型E
     * @param[in] _u 期待类型为G的Dtk::Core::DUnexpected对象
     * @attention 构造后原对象不可用,该函数有explicit标识
     */
    template <
        typename G = E,
        typename std::enable_if<std::is_constructible<E, G>::value and !std::is_convertible<G, E>::value, bool>::type = true>
    constexpr explicit DExpected(DUnexpected<G> &&_u) noexcept(std::is_nothrow_constructible<E, G>::value)
        : m_has_value(false)
        , m_error(std::move(_u).error())
    {
    }

    /*!
     * @brief
     * Dtk::Core::DExpected的移动构造函数,从Dtk::Core::DUnexpected构造出Dtk::Core::DExpected对象
     * @tparam G Dtk::Core::DExpected的期待类型,默认为类型E
     * @param[in] _u 期待类型为G的Dtk::Core::DUnexpected对象
     * @attention 构造后原对象不可用
     */
    template <typename G = E,
              typename std::enable_if<std::is_constructible<E, G>::value and std::is_convertible<G, E>::value, bool>::type = true>
    constexpr DExpected(DUnexpected<G> &&_u) noexcept(std::is_nothrow_constructible<E, G>::value)
        : m_has_value(false)
        , m_error(std::move(_u).error())
    {
    }

    /*!
     * @brief Dtk::Core::DExpected的转发构造函数,从参数直接构造出期待值
     * @tparam Args 构造期待类型T所用到的参数的类型
     * @param[in] args 构造期待类型T所用到的参数
     * @attention
     * 为了区分是构造T还是Dtk::Core::DExpected,需要在第一个参数使用emplace_tag进行标识
     */
    template <typename... Args>
    constexpr explicit DExpected(emplace_tag, Args &&...args) noexcept(std::is_nothrow_constructible<T, Args...>::value)
        : m_has_value(true)
        , m_value(std::forward<Args>(args)...)

    {
        static_assert(std::is_constructible<T, Args...>::value, "can't construct T from args.");
    }

    /*!
     * @brief Dtk::Core::DExpected的转发构造函数,从参数直接构造出期待值
     * @tparam U 初始化列表的模板参数
     * @tparam Args 构造期待类型T所用到的参数的类型
     * @param[in] _li  构造期待类型T所用到的初始化列表
     * @param[in] args 构造期待类型T所用到的参数
     * @attention
     * 为了区分是构造T还是Dtk::Core::DExpected,需要在第一个参数使用emplace_tag进行标识
     */
    template <typename U, typename... Args>
    constexpr explicit DExpected(emplace_tag, std::initializer_list<U> _li, Args &&...args) noexcept(
        std::is_nothrow_constructible<T, std::initializer_list<U> &, Args...>::value)
        : m_has_value(true)
        , m_value(_li, std::forward<Args>(args)...)
    {
        static_assert(std::is_constructible<T, std::initializer_list<U> &, Args...>::value, "can't construct T from args.");
    }

    /*!
     * @brief Dtk::Core::DExpected的转发构造函数,从参数直接构造出不期待值
     * @tparam Args 构造不期待类型E所用到的参数的类型
     * @param[in] args 构造不期待类型E所用到的参数
     * @attention
     * 为了区分是构造E还是Dtk::Core::DExpected,需要在第一个参数使用dunexpected_tag进行标识
     */
    template <typename... Args>
    constexpr explicit DExpected(dunexpected_tag, Args &&...args) noexcept(std::is_nothrow_constructible<E, Args...>::value)
        : m_has_value(false)
        , m_error(std::forward<Args>(args)...)

    {
        static_assert(std::is_constructible<E, Args...>::value, "can't construct E from args.");
    }

    /*!
     * @brief Dtk::Core::DExpected的转发构造函数,从参数直接构造出不期待值
     * @tparam U 初始化列表的模板参数
     * @tparam Args 构造不期待类型E所用到的参数的类型
     * @param[in] _li  构造不期待类型E所用到的初始化列表
     * @param[in] args 构造不期待类型E所用到的参数
     * @attention
     * 为了区分是构造E还是Dtk::Core::DExpected,需要在第一个参数使用dunexpected_tag进行标识
     */
    template <typename U, typename... Args>
    constexpr explicit DExpected(dunexpected_tag, std::initializer_list<U> _li, Args &&...args) noexcept(
        std::is_nothrow_constructible<E, std::initializer_list<U> &, Args...>::value)
        : m_has_value(false)
        , m_error(_li, std::forward<Args>(args)...)
    {
        static_assert(std::is_constructible<E, std::initializer_list<U> &, Args...>::value, "can't construct E from args.");
    }

    /*!
     * @brief Dtk::Core::DExpected的析构函数
     */
    ~DExpected()
    {
        if (des_value()) {
            if (m_has_value) {
                destroy_at(std::addressof(m_value));
            } else {
                destroy_at(std::addressof(m_error));
            }
        }
    }

    /*!
     * @brief Dtk::Core::DExpected的默认拷贝赋值运算符
     */
    DExpected &operator=(const DExpected &) = delete;

    /*!
     * @brief Dtk::Core::DExpected的拷贝赋值运算符
     * @param[in] _x 同类型的Dtk::Core::DExpected对象
     */
    template <typename std::enable_if<std::is_copy_assignable<T>::value and std::is_copy_constructible<T>::value and
                                          std::is_copy_assignable<E>::value and std::is_copy_constructible<E>::value and
                                          (std::is_nothrow_move_constructible<T>::value or
                                           std::is_nothrow_move_constructible<E>::value),
                                      bool>::type = true>
    DExpected &operator=(const DExpected &_x) noexcept(
        std::is_nothrow_copy_constructible<T>::value and std::is_nothrow_copy_constructible<E>::value
            and std::is_nothrow_copy_assignable<T>::value and std::is_nothrow_copy_assignable<E>::value)
    {
        if (_x.m_has_value)
            this->assign_val(_x.m_value);
        else
            this->assign_err(_x.m_error);
        return *this;
    }

    /*!
     * @brief Dtk::Core::DExpected的移动赋值运算符
     * @param[in] _x 同类型的Dtk::Core::DExpected对象
     * @attention 赋值后原对象不可用
     */
    template <typename std::enable_if<std::is_move_assignable<T>::value and std::is_move_constructible<T>::value and
                                          std::is_move_assignable<E>::value and std::is_move_constructible<E>::value and
                                          (std::is_nothrow_move_constructible<T>::value or
                                           std::is_nothrow_move_constructible<E>::value),
                                      bool>::type = true>
    DExpected &operator=(DExpected &&_x) noexcept(
        std::is_nothrow_move_constructible<T>::value and std::is_nothrow_move_constructible<E>::value
            and std::is_nothrow_move_assignable<T>::value and std::is_nothrow_move_assignable<E>::value)
    {
        if (_x.m_has_value)
            assign_val(std::move(_x.m_value));
        else
            assign_err(std::move(_x.m_error));
        return *this;
    }

    /*!
     * @brief Dtk::Core::DExpected的转发赋值运算符
     * @tparam U 期待类型,默认为T
     * @param[in] _v 期待类型U的对象
     */
    template <
        typename U = T,
        typename std::enable_if<!std::is_same<DExpected, typename remove_cvref<U>::type>::value and
                                    !__dexpected::_is_dunexpected<typename remove_cvref<U>::type>::value and
                                    std::is_constructible<T, U>::value and std::is_assignable<T &, U>::value and
                                    (std::is_nothrow_constructible<T, U>::value or std::is_nothrow_move_constructible<T>::value or
                                     std::is_nothrow_move_constructible<E>::value),
                                bool>::type = true>
    DExpected &operator=(U &&_v)
    {
        assign_val(std::forward<U>(_v));
        return *this;
    }

    /*!
     * @brief Dtk::Core::DExpected的拷贝赋值运算符
     * @tparam G 不期待类型
     * @param[in] _e 模板类型为G的Dtk::Core::DUnexpected对象
     */
    template <typename G,
              typename std::enable_if<std::is_constructible<E, const G &>::value and std::is_assignable<E &, const G &>::value and
                                          (std::is_nothrow_constructible<E, const G &>::value or
                                           std::is_nothrow_move_constructible<T>::value or std::is_move_constructible<E>::value),
                                      bool>::type = true>
    DExpected &operator=(const DUnexpected<G> &_e)
    {
        assign_err(_e.error());
        return *this;
    }

    /*!
     * @brief Dtk::Core::DExpected的移动赋值运算符
     * @tparam G 不期待类型
     * @param[in] _e 模板类型为G的Dtk::Core::DUnexpected对象
     * @attention 赋值后原对象不可用
     */
    template <typename G,
              typename std::enable_if<std::is_constructible<E, G>::value and std::is_assignable<E &, G>::value and
                                          (std::is_nothrow_constructible<E, G>::value or
                                           std::is_nothrow_move_constructible<T>::value or std::is_move_constructible<E>::value),
                                      bool>::type = true>
    DExpected &operator=(DUnexpected<G> &&_e)
    {
        assign_err(std::move(_e).error());
        return *this;
    }

    /*!
     * @brief 从参数直接转发构造期待值
     * @tparam Args 构造期待值所用到的参数的类型
     * @param[in] args 构造期待值所用到的参数
     * @return 返回构造好的期待值的引用
     */
    template <typename... Args>
    T &emplace(Args &&...args) noexcept
    {
        static_assert(std::is_nothrow_constructible<T, Args...>::value, "type T should have nothrow_constructible");
        if (m_has_value)
            destroy_at(std::addressof(m_value));
        else {
            destroy_at(std::addressof(m_error));
            m_has_value = true;
        }
        construct_at(std::addressof(m_value), std::forward<Args>(args)...);
        return m_value;
    }

    /*!
     * @brief 从参数直接转发构造期待值
     * @tparam U 初始化列表的模板参数
     * @tparam Args 构造期待值所用到的参数的类型
     * @param[in] args 构造期待值所用到的参数
     * @param[in] li 构造期待值所用到的参数化列表
     * @return 返回构造好的期待值的引用
     */
    template <typename U, typename... Args>
    T &emplace(std::initializer_list<U> li, Args &&...args) noexcept
    {
        static_assert(std::is_nothrow_constructible<T, std::initializer_list<U> &, Args...>::value,
                      "type T should have a noexcept constructor");
        if (m_has_value)
            destroy_at(std::addressof(m_value));
        else {
            destroy_at(std::addressof(m_error));
        }
        construct_at(std::addressof(m_value), li, std::forward<Args>(args)...);
        return m_value;
    }

    // TODO:需要swap吗?
    /*!
     * @brief 交换两个Dtk::Core::DExpected的值
     * @param[in] _x 另一个Dtk::Core::DExpected对象
     */
    template <typename std::enable_if<std::is_move_constructible<T>::value and std::is_move_constructible<E>::value and
                                          (std::is_nothrow_move_constructible<T>::value or
                                           std::is_nothrow_move_constructible<E>::value),
                                      bool>::type = true>
    void
    swap(DExpected &_x) noexcept(std::is_nothrow_move_constructible<T>::value and std::is_nothrow_move_constructible<E>::value)
    {
        if (m_has_value) {
            if (_x.m_has_value) {
                using std::swap;
                swap(m_value, _x.m_value);
            } else {
                this->swap_val_err(_x);
            }
        } else {
            if (_x.m_has_value)
                _x.swap_val_err(*this);
            else {
                using std::swap;
                swap(m_error, _x.m_error);
            }
        }
    }

    /*!
     * @brief 重载箭头运算符
     * @return 一个指向期待值的const指针
     */
    const T *operator->() const noexcept
    {
        assert(m_has_value);
        return std::addressof(m_value);
    }

    /*!
     * @brief 重载箭头运算符
     * @return 一个指向期待值的指针
     */
    T *operator->() noexcept
    {
        assert(m_has_value);
        return std::addressof(m_value);
    }

    /*!
     * @brief 重载解引用运算符
     * @return 一个期待值的const左值引用
     */
    const T &operator*() const &noexcept
    {
        assert(m_has_value);
        return m_value;
    }

    /*!
     * @brief 重载解引用运算符
     * @return 一个期待值的左值引用
     */
    T &operator*() &noexcept
    {
        assert(m_has_value);
        return m_value;
    }

    /*!
     * @brief 重载解引用运算符
     * @return 一个期待值的const右值引用
     */
    const T &&operator*() const &&noexcept
    {
        assert(m_has_value);
        return std::move(m_value);
    }

    /*!
     * @brief 重载解引用运算符
     * @return 一个期待值的右值引用
     */
    T &&operator*() &&noexcept
    {
        assert(m_has_value);
        return std::move(m_value);
    }

    /*!
     * @brief bool转换函数
     * @return 表示Dtk::Core::DExpected是否有值的bool值
     */
    constexpr explicit operator bool() const noexcept { return m_has_value; }

    /*!
     * @brief 判断Dtk::Core::DExpected是否有值
     * @return 表示是否有值的bool值
     */
    constexpr bool hasValue() const noexcept { return m_has_value; }

    /*!
     * @brief 获取Dtk::Core::DExpected的期待值
     * @return 期待值的const左值引用
     */
    const T &value() const &
    {
        if (likely(m_has_value)) {
            return m_value;
        }
        _DEXPECTED_THROW_OR_ABORT(bad_result_access<E>(m_error));
    }

    /*!
     * @brief 获取Dtk::Core::DExpected的期待值
     * @return 期待值的左值引用
     */
    T &value() &
    {
        if (likely(m_has_value)) {
            return m_value;
        }
        _DEXPECTED_THROW_OR_ABORT(bad_result_access<E>(m_error));
    }

    /*!
     * @brief 获取Dtk::Core::DExpected的期待值
     * @return 期待值的const右值引用
     * @attention 调用后期待值不可用
     */
    const T &&value() const &&
    {
        if (likely(m_has_value)) {
            return std::move(m_value);
        }
        _DEXPECTED_THROW_OR_ABORT(bad_result_access<E>(m_error));
    }

    /*!
     * @brief 获取Dtk::Core::DExpected的期待值
     * @return 期待值的右值引用
     * @attention 调用后期待值不可用
     */
    T &&value() &&
    {
        if (likely(m_has_value)) {
            return std::move(m_value);
        }
        _DEXPECTED_THROW_OR_ABORT(bad_result_access<E>(m_error));
    }

    /*!
     * @brief 获取Dtk::Core::DExpected的不期待值
     * @return 不期待值的const左值引用
     */
    const E &error() const &noexcept
    {
        assert(!m_has_value);
        return m_error;
    }

    /*!
     * @brief 获取Dtk::Core::DExpected的不期待值
     * @return 不期待值的左值引用
     */
    E &error() &noexcept
    {
        assert(!m_has_value);
        return m_error;
    }

    /*!
     * @brief 获取Dtk::Core::DExpected的不期待值
     * @return 不期待值的const右值引用
     * @attention 调用后不期待值不可用
     */
    const E &&error() const &&noexcept
    {
        assert(!m_has_value);
        return std::move(m_error);
    }

    /*!
     * @brief 获取Dtk::Core::DExpected的不期待值
     * @return 不期待值的右值引用
     * @attention 调用后不期待值不可用
     */
    E &&error() &&noexcept
    {
        assert(!m_has_value);
        return std::move(m_error);
    }

    // TODO:因为无法确定U转T时是否会抛出异常,所以都按抛出异常来
    /*!
     * @brief 如果有期待值返回期待值,否则返回传入的默认值
     * @tparam U 期待值的类型
     * @param[in] _v 默认的期待值
     * @return 期待值
     */
    template <typename U>
    T value_or(U &&_v) const &
    {
        static_assert(std::is_copy_constructible<T>::value, "type T should have an copy constructor.");
        static_assert(std::is_convertible<U, T>::value, "type U must can be converted to T.");
        if (m_has_value)
            return m_value;
        return static_cast<T>(std::forward<U>(_v));
    }

    /*!
     * @brief 如果有期待值返回期待值,否则返回传入的默认值
     * @tparam U 期待值的类型
     * @param[in] _v 默认的期待值
     * @return 期待值
     * @attention 如果由期待值,调用后原期待值不可用,同时类型U要可以转换成类型T
     */
    template <typename U>
    T value_or(U &&_v) &&
    {
        static_assert(std::is_move_constructible<T>::value, "type T must bu copy_constructible.");
        static_assert(std::is_convertible<U, T>::value, "type U must can be converted to T.");
        if (m_has_value)
            return std::move(m_value);
        return static_cast<T>(std::forward<U>(_v));
    }

    /*!
     *@brief 重载相等运算符
     */
    template <typename U, typename E2, typename std::enable_if<!std::is_void<U>::value, bool>::type = true>
    friend bool
    operator==(const DExpected &_x,
               const DExpected<U, E2> &_y) noexcept(noexcept(bool(*_x == *_y)) and noexcept(bool(_x.error() == _y.error())))
    {
        if (_x.hasValue())
            return _y.hasValue() and bool(*_x == *_y);
        else
            return !_y.hasValue() and bool(_x.error() == _x.error());
    }

    /*!
     *@brief 重载相等运算符
     */
    template <typename U>
    friend constexpr bool operator==(const DExpected &_x, const U &_v) noexcept(noexcept(bool(*_x == _v)))
    {
        return _x.hasValue() && bool(*_x == _v);
    }

    /*!
     *@brief 重载相等运算符
     */
    template <typename E2>
    friend constexpr bool operator==(const DExpected &_x,
                                     const DUnexpected<E2> &_e) noexcept(noexcept(bool(_x.error() == _e.error())))
    {
        return !_x.hasValue() && bool(_x.error() == _e.error());
    }

    /*!
     *@brief 交换两个Dtk::Core::DExpected中的值
     */
    friend void swap(DExpected &_x, DExpected &_y) noexcept(noexcept(_x.swap(_y))) { _x.swap(_y); }

private:
    bool m_has_value;
    union
    {
        T m_value;
        E m_error;
    };
};

/*!
 * @brief 对于Dtk::Core::DExpected的void偏特化,其他函数参考原模板类
 * @tparam E 不期待值的类型
 */
template <typename E>
class DExpected<void, E>
{
    static_assert(__dexpected::_can_be_dunexpected<E>(), "type E can't be DUnexpected.");
    static constexpr bool des_value() { return !std::is_trivially_destructible<E>::value; }

    template <typename, typename>
    friend class DExpected;

    template <typename U, typename G, typename Unex = DUnexpected<E>>
    static constexpr bool __cons_from_DExpected()
    {
        return std::is_constructible<Unex, DExpected<U, G> &>::value and std::is_constructible<Unex, DExpected<U, G>>::value and
               std::is_constructible<Unex, const DExpected<U, G> &>::value and
               std::is_constructible<Unex, const DExpected<U, G>>::value;
    }

    template <typename V>
    void assign_err(V &&_v)
    {
        if (m_has_value) {
            construct_at(std::addressof(m_error), std::forward<V>(_v));
            m_has_value = false;
        } else {
            m_error = std::forward<V>(_v);
        }
    }

public:
    using value_type = void;
    using error_type = E;
    using dunexpected_type = DUnexpected<E>;
    template <typename U>
    using rebind = DExpected<U, error_type>;

    constexpr DExpected() noexcept
        : m_has_value(true)
        , m_void()
    {
    }

    template <typename std::enable_if<std::is_copy_constructible<E>::value, bool>::type = true>
    DExpected(const DExpected &_x) noexcept(std::is_nothrow_copy_constructible<E>::value)
        : m_has_value(_x.m_has_value)
        , m_void()
    {
        if (!m_has_value)
            construct_at(std::addressof(m_error), _x.m_error);
    }

    template <typename std::enable_if<std::is_move_constructible<E>::value, bool>::type = true>
    DExpected(DExpected &&_x) noexcept(std::is_nothrow_move_constructible<E>::value)
        : m_has_value(_x.m_has_value)
        , m_void()
    {
        if (!m_has_value)
            construct_at(std::addressof(m_error), std::move(_x).m_error);
    }

    template <typename U,
              typename G,
              typename std::enable_if<std::is_void<U>::value and std::is_constructible<E, const G &>::value and
                                          !__cons_from_DExpected<U, G>() and !std::is_convertible<const G &, E>::value,
                                      bool>::type = true>
    explicit DExpected(const DExpected<U, G> &_x) noexcept(std::is_nothrow_constructible<E, const G &>::value)
        : m_has_value(_x.m_has_value)
        , m_void()
    {
        if (!m_has_value)
            construct_at(std::addressof(m_error), _x.m_error);
    }

    template <typename U,
              typename G,
              typename std::enable_if<std::is_void<U>::value and std::is_constructible<E, const G &>::value and
                                          !__cons_from_DExpected<U, G>() and std::is_convertible<const G &, E>::value,
                                      bool>::type = true>
    DExpected(const DExpected<U, G> &_x) noexcept(std::is_nothrow_constructible<E, const G &>::value)
        : m_has_value(_x.m_has_value)
        , m_void()
    {
        if (!m_has_value)
            construct_at(std::addressof(m_error), _x.m_error);
    }

    template <typename U,
              typename G,
              typename std::enable_if<std::is_void<U>::value and std::is_constructible<E, G>::value and
                                          __cons_from_DExpected<U, G>() and !std::is_convertible<G, E>::value,
                                      bool>::type = true>
    explicit DExpected(DExpected<U, G> &&_x) noexcept(std::is_nothrow_constructible<E, G>::value)
        : m_has_value(_x.m_has_value)
        , m_void()
    {
        if (!m_has_value)
            construct_at(std::addressof(m_error), std::move(_x).m_error);
    }

    template <typename U,
              typename G,
              typename std::enable_if<std::is_void<U>::value and std::is_constructible<E, G>::value and
                                          __cons_from_DExpected<U, G>() and std::is_convertible<G, E>::value,
                                      bool>::type = true>
    DExpected(DExpected<U, G> &&_x) noexcept(std::is_nothrow_constructible<E, G>::value)
        : m_has_value(_x.m_has_value)
        , m_void()
    {
        if (!m_has_value)
            construct_at(std::addressof(m_error), std::move(_x).m_error);
    }

    template <typename G = E,
              typename std::enable_if<std::is_constructible<E, const G &>::value and !std::is_convertible<const G &, E>::value,
                                      bool>::type = true>
    constexpr explicit DExpected(const DUnexpected<G> &_u) noexcept(std::is_nothrow_constructible<E, const G &>::value)
        : m_has_value(false)
        , m_error(_u.error())
    {
    }

    template <typename G = E,
              typename std::enable_if<std::is_constructible<E, const G &>::value and std::is_convertible<const G &, E>::value,
                                      bool>::type = true>
    constexpr DExpected(const DUnexpected<G> &_u) noexcept(std::is_nothrow_constructible<E, const G &>::value)
        : m_has_value(false)
        , m_error(_u.error())
    {
    }

    template <
        typename G = E,
        typename std::enable_if<std::is_constructible<E, G>::value and !std::is_convertible<G, E>::value, bool>::type = true>
    constexpr explicit DExpected(DUnexpected<G> &&_u) noexcept(std::is_nothrow_constructible<E, G>::value)
        : m_has_value(false)
        , m_error(std::move(_u).error())
    {
    }

    template <typename G = E,
              typename std::enable_if<std::is_constructible<E, G>::value and std::is_convertible<G, E>::value, bool>::type = true>
    constexpr DExpected(DUnexpected<G> &&_u) noexcept(std::is_nothrow_constructible<E, G>::value)
        : m_has_value(false)
        , m_error(std::move(_u).error())
    {
    }

    template <typename... Args>
    constexpr explicit DExpected(emplace_tag) noexcept
        : DExpected()
    {
    }

    template <typename... Args>
    constexpr explicit DExpected(dunexpected_tag, Args &&...args) noexcept(std::is_nothrow_constructible<E, Args...>::value)
        : m_has_value(false)
        , m_error(std::forward<Args>(args)...)
    {
        static_assert(std::is_constructible<E, Args...>::value, "type E can't construct from args");
    }

    template <typename U, typename... Args>
    constexpr explicit DExpected(dunexpected_tag,
                                 std::initializer_list<U> _li,
                                 Args &&...args) noexcept(std::is_nothrow_constructible<E, Args...>::value)
        : m_has_value(false)
        , m_error(_li, std::forward<Args>(args)...)
    {
        static_assert(std::is_constructible<E, std::initializer_list<U> &, Args...>::value, "type E can't construct from args");
    }

    ~DExpected()
    {
        if (des_value() and !m_has_value) {
            destroy_at(std::addressof(m_error));
        }
    }

    DExpected &operator=(const DExpected &) = delete;

    template <
        typename std::enable_if<std::is_copy_constructible<E>::value and std::is_copy_assignable<E>::value, bool>::type = true>
    DExpected &operator=(const DExpected &_x) noexcept(
        std::is_nothrow_copy_constructible<E>::value and std::is_nothrow_copy_assignable<E>::value)
    {
        if (_x.m_has_value)
            emplace();
        else
            assign_err(_x.m_error);
        return *this;
    }

    template <
        typename std::enable_if<std::is_move_constructible<E>::value and std::is_move_assignable<E>::value, bool>::type = true>
    DExpected &
    operator=(DExpected &&_x) noexcept(std::is_nothrow_move_constructible<E>::value and std::is_nothrow_move_assignable<E>::value)
    {
        if (_x.m_has_value)
            emplace();
        else
            assign_err(std::move(_x.m_error));
        return *this;
    }

    template <typename G,
              typename std::enable_if<std::is_constructible<E, const G &>::value and std::is_assignable<E &, const G &>::value,
                                      bool>::type = true>
    DExpected &operator=(const DUnexpected<G> &_e)
    {
        assign_err(_e.error());
        return *this;
    }

    template <
        typename G,
        typename std::enable_if<std::is_constructible<E, G>::value and std::is_assignable<E &, G>::value, bool>::type = true>
    DExpected &operator=(DUnexpected<G> &&_e)
    {
        assign_err(std::move(_e.error()));
        return *this;
    }

    void emplace() noexcept
    {
        if (!m_has_value) {
            destroy_at(std::addressof(m_error));
            m_has_value = true;
        }
    }

    template <typename std::enable_if<std::is_move_constructible<E>::value, bool>::type = true>
    void swap(DExpected &_x) noexcept(std::is_nothrow_move_constructible<E>::value)
    {
        if (m_has_value) {
            if (!_x.m_has_value) {
                construct_at(std::addressof(m_error), std::move(_x.m_error));
                destroy_at(std::addressof(_x.m_error));
                m_has_value = false;
                _x.m_has_value = true;
            }
        } else {
            if (_x.m_has_value) {
                construct_at(std::addressof(_x.m_error), std::move(m_error));
                destroy_at(std::addressof(m_error));
                m_has_value = true;
                _x.m_has_value = false;
            } else {
                using std::swap;
                swap(m_error, _x.m_error);
            }
        }
    }

    constexpr explicit operator bool() const noexcept { return m_has_value; }

    constexpr bool hasValue() const noexcept { return m_has_value; }

    void operator*() const noexcept { assert(m_has_value); }

    void value() const &
    {
        if (likely(m_has_value))
            return;
        _DEXPECTED_THROW_OR_ABORT(bad_result_access<E>(m_error));
    }

    void value() &&
    {
        if (likely(m_has_value))
            return;
        _DEXPECTED_THROW_OR_ABORT(bad_result_access<E>(std::move(m_error)));
    }

    const E &error() const &noexcept
    {
        assert(!m_has_value);
        return m_error;
    }

    E &error() &noexcept
    {
        assert(!m_has_value);
        return m_error;
    }

    const E &&error() const &&noexcept
    {
        assert(!m_has_value);
        return std::move(m_error);
    }

    E &&error() &&noexcept
    {
        assert(!m_has_value);
        return std::move(m_error);
    }

    template <typename U, typename E2, typename std::enable_if<std::is_void<U>::value, bool>::type = true>
    friend bool operator==(const DExpected &_x, const DExpected<U, E2> &_y) noexcept(noexcept(bool(_x.error() == _y.error())))
    {
        if (_x.hasValue())
            return _y.hasValue();
        else
            return !_y.hasValue() and bool(_x.error() == _y.error());
    }

    template <typename E2>
    friend bool operator==(const DExpected &_x, const DUnexpected<E2> &_e) noexcept(noexcept(bool(_x.error() == _e.error())))
    {
        return !_x.hasValue() && bool(_x.error() == _e.error());
    }

    // TODO:可能没有swap
    friend void swap(DExpected &_x, DExpected &_y) noexcept(noexcept(_x.swap(_y))) { _x.swap(_y); }

private:
    bool m_has_value;
    union
    {
        struct
        {
        } m_void;
        E m_error;
    };
};

DCORE_END_NAMESPACE

#endif