-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathresult.hpp
5994 lines (5288 loc) · 210 KB
/
result.hpp
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
////////////////////////////////////////////////////////////////////////////////
/// \file result.hpp
///
/// \brief This header contains the 'result' monadic type for indicating
/// possible error conditions
////////////////////////////////////////////////////////////////////////////////
/*
The MIT License (MIT)
Copyright (c) 2017-2021 Matthew Rodusek All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef RESULT_RESULT_HPP
#define RESULT_RESULT_HPP
#include <cstddef> // std::size_t
#include <type_traits> // std::enable_if, std::is_constructible, etc
#include <new> // placement-new
#include <memory> // std::address_of
#include <functional> // std::reference_wrapper, std::invoke
#include <utility> // std::in_place_t, std::forward
#include <initializer_list> // std::initializer_list
#include <string> // std::string (for exception message)
#if defined(RESULT_EXCEPTIONS_DISABLED)
# include <cstdio> // std::fprintf, stderr
#else
# include <stdexcept> // std::logic_error
#endif
#if __cplusplus >= 201402L
# define RESULT_CPP14_CONSTEXPR constexpr
#else
# define RESULT_CPP14_CONSTEXPR
#endif
#if __cplusplus >= 201703L
# define RESULT_CPP17_INLINE inline
#else
# define RESULT_CPP17_INLINE
#endif
#if defined(__clang__) && defined(_MSC_VER)
# define RESULT_INLINE_VISIBILITY __attribute__((visibility("hidden")))
#elif defined(__clang__) || defined(__GNUC__)
# define RESULT_INLINE_VISIBILITY __attribute__((visibility("hidden"), always_inline))
#elif defined(_MSC_VER)
# define RESULT_INLINE_VISIBILITY __forceinline
#else
# define RESULT_INLINE_VISIBILITY
#endif
// [[clang::warn_unused_result]] is more full-featured than gcc's variant, since
// it supports being applied to class objects.
#if __cplusplus >= 201703L
# define RESULT_NODISCARD [[nodiscard]]
# define RESULT_WARN_UNUSED [[nodiscard]]
#elif defined(__clang__) && ((__clang_major__ > 3) || ((__clang_major__ == 3) && (__clang_minor__ >= 9)))
# define RESULT_NODISCARD [[clang::warn_unused_result]]
# define RESULT_WARN_UNUSED [[clang::warn_unused_result]]
#elif defined(__GNUC__)
# define RESULT_NODISCARD
# define RESULT_WARN_UNUSED [[gnu::warn_unused_result]]
#else
# define RESULT_WARN_UNUSED
# define RESULT_NODISCARD
#endif
#if defined(RESULT_NAMESPACE)
# define RESULT_NAMESPACE_INTERNAL RESULT_NAMESPACE
#else
# define RESULT_NAMESPACE_INTERNAL cpp
#endif
#define RESULT_NS_IMPL RESULT_NAMESPACE_INTERNAL::bitwizeshift
// clang's `-Wdocumentation-unknown-command` flag is bugged and does not
// understand `\copydoc` tags, despite this being a valid doxygen tag.
#if defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
#endif
namespace RESULT_NAMESPACE_INTERNAL {
inline namespace bitwizeshift {
//===========================================================================
// utilities : constexpr forward
//===========================================================================
// std::forward is not constexpr until C++14
namespace detail {
#if __cplusplus >= 201402L
using std::forward;
#else
template <typename T>
inline RESULT_INLINE_VISIBILITY constexpr
auto forward(typename std::remove_reference<T>::type& t)
noexcept -> T&&
{
return static_cast<T&&>(t);
}
template <typename T>
inline RESULT_INLINE_VISIBILITY constexpr
auto forward(typename std::remove_reference<T>::type&& t)
noexcept -> T&&
{
return static_cast<T&&>(t);
}
#endif
} // namespace detail
//===========================================================================
// utilities : invoke / invoke_result
//===========================================================================
// std::invoke was introduced in C++17
namespace detail {
#if __cplusplus >= 201703L
using std::invoke;
using std::invoke_result;
using std::invoke_result_t;
#else
template<typename T>
struct is_reference_wrapper : std::false_type{};
template<typename U>
struct is_reference_wrapper<std::reference_wrapper<U>> : std::true_type{};
//-------------------------------------------------------------------------
template <typename Base, typename T, typename Derived, typename... Args,
typename = typename std::enable_if<
std::is_function<T>::value &&
std::is_base_of<Base, typename std::decay<Derived>::type>::value
>::type>
inline RESULT_INLINE_VISIBILITY constexpr
auto invoke(T Base::*pmf, Derived&& ref, Args&&... args)
noexcept(noexcept((::RESULT_NS_IMPL::detail::forward<Derived>(ref).*pmf)(::RESULT_NS_IMPL::detail::forward<Args>(args)...)))
-> decltype((::RESULT_NS_IMPL::detail::forward<Derived>(ref).*pmf)(::RESULT_NS_IMPL::detail::forward<Args>(args)...))
{
return (RESULT_NS_IMPL::detail::forward<Derived>(ref).*pmf)(RESULT_NS_IMPL::detail::forward<Args>(args)...);
}
template <typename Base, typename T, typename RefWrap, typename... Args,
typename = typename std::enable_if<
std::is_function<T>::value &&
is_reference_wrapper<typename std::decay<RefWrap>::type>::value
>::type>
inline RESULT_INLINE_VISIBILITY constexpr
auto invoke(T Base::*pmf, RefWrap&& ref, Args&&... args)
noexcept(noexcept((ref.get().*pmf)(std::forward<Args>(args)...)))
-> decltype((ref.get().*pmf)(RESULT_NS_IMPL::detail::forward<Args>(args)...))
{
return (ref.get().*pmf)(RESULT_NS_IMPL::detail::forward<Args>(args)...);
}
template <typename Base, typename T, typename Pointer, typename... Args,
typename = typename std::enable_if<
std::is_function<T>::value &&
!is_reference_wrapper<typename std::decay<Pointer>::type>::value &&
!std::is_base_of<Base, typename std::decay<Pointer>::type>::value
>::type>
inline RESULT_INLINE_VISIBILITY constexpr
auto invoke(T Base::*pmf, Pointer&& ptr, Args&&... args)
noexcept(noexcept(((*std::forward<Pointer>(ptr)).*pmf)(std::forward<Args>(args)...)))
-> decltype(((*RESULT_NS_IMPL::detail::forward<Pointer>(ptr)).*pmf)(RESULT_NS_IMPL::detail::forward<Args>(args)...))
{
return ((*RESULT_NS_IMPL::detail::forward<Pointer>(ptr)).*pmf)(RESULT_NS_IMPL::detail::forward<Args>(args)...);
}
template <typename Base, typename T, typename Derived,
typename = typename std::enable_if<
!std::is_function<T>::value &&
std::is_base_of<Base, typename std::decay<Derived>::type>::value
>::type>
inline RESULT_INLINE_VISIBILITY constexpr
auto invoke(T Base::*pmd, Derived&& ref)
noexcept(noexcept(std::forward<Derived>(ref).*pmd))
-> decltype(RESULT_NS_IMPL::detail::forward<Derived>(ref).*pmd)
{
return RESULT_NS_IMPL::detail::forward<Derived>(ref).*pmd;
}
template <typename Base, typename T, typename RefWrap,
typename = typename std::enable_if<
!std::is_function<T>::value &&
is_reference_wrapper<typename std::decay<RefWrap>::type>::value
>::type>
inline RESULT_INLINE_VISIBILITY constexpr
auto invoke(T Base::*pmd, RefWrap&& ref)
noexcept(noexcept(ref.get().*pmd))
-> decltype(ref.get().*pmd)
{
return ref.get().*pmd;
}
template <typename Base, typename T, typename Pointer,
typename = typename std::enable_if<
!std::is_function<T>::value &&
!is_reference_wrapper<typename std::decay<Pointer>::type>::value &&
!std::is_base_of<Base, typename std::decay<Pointer>::type>::value
>::type>
inline RESULT_INLINE_VISIBILITY constexpr
auto invoke(T Base::*pmd, Pointer&& ptr)
noexcept(noexcept((*std::forward<Pointer>(ptr)).*pmd))
-> decltype((*RESULT_NS_IMPL::detail::forward<Pointer>(ptr)).*pmd)
{
return (*RESULT_NS_IMPL::detail::forward<Pointer>(ptr)).*pmd;
}
template <typename F, typename... Args,
typename = typename std::enable_if<!std::is_member_pointer<typename std::decay<F>::type>::value>::type>
inline RESULT_INLINE_VISIBILITY constexpr
auto invoke(F&& f, Args&&... args)
noexcept(noexcept(std::forward<F>(f)(std::forward<Args>(args)...)))
-> decltype(RESULT_NS_IMPL::detail::forward<F>(f)(RESULT_NS_IMPL::detail::forward<Args>(args)...))
{
return RESULT_NS_IMPL::detail::forward<F>(f)(RESULT_NS_IMPL::detail::forward<Args>(args)...);
}
template<typename Fn, typename...Args>
struct is_invocable
{
template <typename Fn2, typename...Args2>
static auto test( Fn2&&, Args2&&... )
-> decltype(invoke(std::declval<Fn2>(), std::declval<Args2>()...), std::true_type{});
static auto test(...)
-> std::false_type;
using type = decltype(test(std::declval<Fn>(), std::declval<Args>()...));
static constexpr bool value = type::value;
};
template <bool B, typename Fn, typename...Args>
struct invoke_result_impl {
using type = decltype(RESULT_NS_IMPL::detail::invoke(std::declval<Fn>(), std::declval<Args>()...));
};
template <typename Fn, typename...Args>
struct invoke_result_impl<false, Fn, Args...>{};
template <typename Fn, typename...Args>
struct invoke_result
: invoke_result_impl<is_invocable<Fn,Args...>::value, Fn, Args...>{};
template <typename Fn, typename...Args>
using invoke_result_t = typename invoke_result<Fn, Args...>::type;
#endif
}
//===========================================================================
// struct : in_place_t
//===========================================================================
#if __cplusplus >= 201703L
using std::in_place_t;
using std::in_place;
#else
/// \brief A structure for representing in-place construction
struct in_place_t
{
explicit in_place_t() = default;
};
RESULT_CPP17_INLINE constexpr auto in_place = in_place_t{};
#endif
//===========================================================================
// struct : in_place_t
//===========================================================================
/// \brief A structure for representing in-place construction of an error type
struct in_place_error_t
{
explicit in_place_error_t() = default;
};
RESULT_CPP17_INLINE constexpr auto in_place_error = in_place_error_t{};
//===========================================================================
// forward-declarations
//===========================================================================
template <typename>
class failure;
template <typename, typename>
class result;
template <typename>
class bad_result_access;
//===========================================================================
// traits
//===========================================================================
template <typename T>
struct is_failure : std::false_type{};
template <typename E>
struct is_failure<failure<E>> : std::true_type{};
template <typename T>
struct is_result : std::false_type{};
template <typename T, typename E>
struct is_result<result<T,E>> : std::true_type{};
//===========================================================================
// trait : detail::wrapped_result_type
//===========================================================================
namespace detail {
template <typename T>
using wrapped_result_type = typename std::conditional<
std::is_lvalue_reference<T>::value,
std::reference_wrapper<
typename std::remove_reference<T>::type
>,
typename std::remove_const<T>::type
>::type;
} // namespace detail
#if !defined(RESULT_DISABLE_EXCEPTIONS)
//===========================================================================
// class : bad_result_access<E>
//===========================================================================
/////////////////////////////////////////////////////////////////////////////
/// \brief An exception thrown when result::value is accessed without
/// a contained value
/////////////////////////////////////////////////////////////////////////////
template <typename E>
class bad_result_access : public std::logic_error
{
//-------------------------------------------------------------------------
// Constructor / Assignment
//-------------------------------------------------------------------------
public:
/// \brief Constructs this exception using the underlying error type for
/// the error type
///
/// \param error the underlying error
template <typename E2,
typename = typename std::enable_if<std::is_constructible<E,E2>::value>::type>
explicit bad_result_access(E2&& error);
/// \{
/// \brief Constructs this exception using the underlying error type for
/// the error and a message
///
/// \param what_arg the message for the failure
/// \param error the underlying error
template <typename E2,
typename = typename std::enable_if<std::is_constructible<E,E2>::value>::type>
bad_result_access(const char* what_arg, E2&& error);
template <typename E2,
typename = typename std::enable_if<std::is_constructible<E,E2>::value>::type>
bad_result_access(const std::string& what_arg, E2&& error);
/// \}
bad_result_access(const bad_result_access& other) = default;
bad_result_access(bad_result_access&& other) = default;
//-------------------------------------------------------------------------
auto operator=(const bad_result_access& other) -> bad_result_access& = default;
auto operator=(bad_result_access&& other) -> bad_result_access& = default;
/// \{
/// \brief Gets the underlying error
///
/// \return the error
auto error() & noexcept -> E&;
auto error() && noexcept -> E&&;
auto error() const & noexcept -> const E&;
auto error() const && noexcept -> const E&&;
/// \}
//-------------------------------------------------------------------------
// Private Members
//-------------------------------------------------------------------------
private:
E m_error;
};
#endif
namespace detail {
template <typename E, typename E2>
using failure_is_value_convertible = std::integral_constant<bool,(
std::is_constructible<E, E2&&>::value &&
!std::is_same<typename std::decay<E2>::type, in_place_t>::value &&
!is_failure<typename std::decay<E2>::type>::value &&
!is_result<typename std::decay<E2>::type>::value
)>;
template <typename E, typename E2>
using failure_is_explicit_value_convertible = std::integral_constant<bool,(
failure_is_value_convertible<E, E2>::value &&
!std::is_convertible<E2, E>::value
)>;
template <typename E, typename E2>
using failure_is_implicit_value_convertible = std::integral_constant<bool,(
failure_is_value_convertible<E, E2>::value &&
std::is_convertible<E2, E>::value
)>;
template <typename E, typename E2>
using failure_is_value_assignable = std::integral_constant<bool,(
!is_result<typename std::decay<E2>::type>::value &&
!is_failure<typename std::decay<E2>::type>::value &&
std::is_assignable<wrapped_result_type<E>&,E2>::value
)>;
} // namespace detail
//===========================================================================
// class : failure_type
//===========================================================================
//////////////////////////////////////////////////////////////////////////////
/// \brief A semantic type used for distinguishing failure values in an
/// API that returns result types
///
/// \tparam E the error type
//////////////////////////////////////////////////////////////////////////////
template <typename E>
class failure
{
static_assert(
!is_result<typename std::decay<E>::type>::value,
"A (possibly CV-qualified) result 'E' type is ill-formed."
);
static_assert(
!is_failure<typename std::decay<E>::type>::value,
"A (possibly CV-qualified) failure 'E' type is ill-formed."
);
static_assert(
!std::is_void<typename std::decay<E>::type>::value,
"A (possibly CV-qualified) 'void' 'E' type is ill-formed."
);
static_assert(
!std::is_rvalue_reference<E>::value,
"rvalue references for 'E' type is ill-formed. "
"Only lvalue references are valid."
);
//-------------------------------------------------------------------------
// Public Member Types
//-------------------------------------------------------------------------
public:
using error_type = E;
//-------------------------------------------------------------------------
// Constructors / Assignment
//-------------------------------------------------------------------------
public:
/// \brief Constructs a failure via default construction
failure() = default;
/// \brief Constructs a failure by delegating construction to the
/// underlying constructor
///
/// \param args the arguments to forward to E's constructor
template <typename...Args,
typename = typename std::enable_if<std::is_constructible<E,Args...>::value>::type>
constexpr failure(in_place_t, Args&&...args)
noexcept(std::is_nothrow_constructible<E, Args...>::value);
/// \brief Constructs a failure by delegating construction to the
/// underlying constructor
///
/// \param ilist the initializer list
/// \param args the arguments to forward to E's constructor
template <typename U, typename...Args,
typename = typename std::enable_if<std::is_constructible<E,std::initializer_list<U>,Args...>::value>::type>
constexpr failure(in_place_t, std::initializer_list<U> ilist, Args&&...args)
noexcept(std::is_nothrow_constructible<E, std::initializer_list<U>, Args...>::value);
/// \{
/// \brief Constructs a failure from the given error
///
/// \param error the error to create a failure from
template <typename E2,
typename std::enable_if<detail::failure_is_implicit_value_convertible<E,E2>::value,int>::type = 0>
constexpr failure(E2&& error)
noexcept(std::is_nothrow_constructible<E,E2>::value);
template <typename E2,
typename std::enable_if<detail::failure_is_explicit_value_convertible<E,E2>::value,int>::type = 0>
constexpr explicit failure(E2&& error)
noexcept(std::is_nothrow_constructible<E,E2>::value);
/// \}
/// \brief Constructs this failure by copying the contents of an existing
/// one
///
/// \param other the other failure to copy
/* implicit */ failure(const failure& other) = default;
/// \brief Constructs this failure by moving the contents of an existing
/// one
///
/// \param other the other failure to move
/* implicit */ failure(failure&& other) = default;
/// \brief Constructs this failure by copy-converting \p other
///
/// \param other the other failure to copy
template <typename E2,
typename = typename std::enable_if<std::is_constructible<E,const E2&>::value>::type>
constexpr /* implicit */ failure(const failure<E2>& other)
noexcept(std::is_nothrow_constructible<E,const E2&>::value);
/// \brief Constructs this failure by move-converting \p other
///
/// \param other the other failure to copy
template <typename E2,
typename = typename std::enable_if<std::is_constructible<E,E2&&>::value>::type>
constexpr /* implicit */ failure(failure<E2>&& other)
noexcept(std::is_nothrow_constructible<E,E2&&>::value);
//--------------------------------------------------------------------------
/// \brief Assigns the value of \p error to this failure through
/// move-assignment
///
/// \param error the value to assign
/// \return reference to `(*this)`
template <typename E2,
typename = typename std::enable_if<detail::failure_is_value_assignable<E,E2>::value>::type>
RESULT_CPP14_CONSTEXPR
auto operator=(E2&& error)
noexcept(std::is_nothrow_assignable<E,E2>::value || std::is_lvalue_reference<E>::value) -> failure&;
/// \brief Assigns the contents of \p other to this by copy-assignment
///
/// \param other the other failure to copy
/// \return reference to `(*this)`
auto operator=(const failure& other) -> failure& = default;
/// \brief Assigns the contents of \p other to this by move-assignment
///
/// \param other the other failure to move
/// \return reference to `(*this)`
auto operator=(failure&& other) -> failure& = default;
/// \brief Assigns the contents of \p other to this by copy conversion
///
/// \param other the other failure to copy-convert
/// \return reference to `(*this)`
template <typename E2,
typename = typename std::enable_if<std::is_assignable<E&,const E2&>::value>::type>
RESULT_CPP14_CONSTEXPR
auto operator=(const failure<E2>& other)
noexcept(std::is_nothrow_assignable<E,const E2&>::value) -> failure&;
/// \brief Assigns the contents of \p other to this by move conversion
///
/// \param other the other failure to move-convert
/// \return reference to `(*this)`
template <typename E2,
typename = typename std::enable_if<std::is_assignable<E&,E2&&>::value>::type>
RESULT_CPP14_CONSTEXPR
auto operator=(failure<E2>&& other)
noexcept(std::is_nothrow_assignable<E,E2&&>::value) -> failure&;
//--------------------------------------------------------------------------
// Observers
//--------------------------------------------------------------------------
public:
/// \{
/// \brief Gets the underlying error
///
/// \return the underlying error
RESULT_CPP14_CONSTEXPR
auto error() & noexcept
-> typename std::add_lvalue_reference<E>::type;
RESULT_CPP14_CONSTEXPR
auto error() && noexcept
-> typename std::add_rvalue_reference<E>::type;
constexpr auto error() const & noexcept
-> typename std::add_lvalue_reference<typename std::add_const<E>::type>::type;
constexpr auto error() const && noexcept
-> typename std::add_rvalue_reference<typename std::add_const<E>::type>::type;
/// \}
//-------------------------------------------------------------------------
// Private Member Types
//-------------------------------------------------------------------------
private:
using underlying_type = detail::wrapped_result_type<E>;
//-------------------------------------------------------------------------
// Private Members
//-------------------------------------------------------------------------
private:
underlying_type m_failure;
};
#if __cplusplus >= 201703L
template <typename T>
failure(std::reference_wrapper<T>) -> failure<T&>;
template <typename T>
failure(T&&) -> failure<typename std::decay<T>::type>;
#endif
//===========================================================================
// non-member functions : class : failure
//===========================================================================
//---------------------------------------------------------------------------
// Comparison
//---------------------------------------------------------------------------
template <typename E1, typename E2>
constexpr auto operator==(const failure<E1>& lhs,
const failure<E2>& rhs) noexcept -> bool;
template <typename E1, typename E2>
constexpr auto operator!=(const failure<E1>& lhs,
const failure<E2>& rhs) noexcept -> bool;
template <typename E1, typename E2>
constexpr auto operator<(const failure<E1>& lhs,
const failure<E2>& rhs) noexcept -> bool;
template <typename E1, typename E2>
constexpr auto operator>(const failure<E1>& lhs,
const failure<E2>& rhs) noexcept -> bool;
template <typename E1, typename E2>
constexpr auto operator<=(const failure<E1>& lhs,
const failure<E2>& rhs) noexcept -> bool;
template <typename E1, typename E2>
constexpr auto operator>=(const failure<E1>& lhs,
const failure<E2>& rhs) noexcept -> bool;
//---------------------------------------------------------------------------
// Utilities
//---------------------------------------------------------------------------
/// \brief Deduces and constructs a failure type from \p e
///
/// \param e the failure value
/// \return a constructed failure value
template <typename E>
RESULT_WARN_UNUSED
constexpr auto fail(E&& e)
noexcept(std::is_nothrow_constructible<typename std::decay<E>::type,E>::value)
-> failure<typename std::decay<E>::type>;
/// \brief Deduces a failure reference from a reverence_wrapper
///
/// \param e the failure value
/// \return a constructed failure reference
template <typename E>
RESULT_WARN_UNUSED
constexpr auto fail(std::reference_wrapper<E> e)
noexcept -> failure<E&>;
/// \brief Constructs a failure type from a series of arguments
///
/// \tparam E the failure type
/// \param args the arguments to forward to E's constructor
/// \return a constructed failure type
template <typename E, typename...Args,
typename = typename std::enable_if<std::is_constructible<E,Args...>::value>::type>
RESULT_WARN_UNUSED
constexpr auto fail(Args&&...args)
noexcept(std::is_nothrow_constructible<E, Args...>::value)
-> failure<E>;
/// \brief Constructs a failure type from an initializer list and series of
/// arguments
///
/// \tparam E the failure type
/// \param args the arguments to forward to E's constructor
/// \return a constructed failure type
template <typename E, typename U, typename...Args,
typename = typename std::enable_if<std::is_constructible<E,std::initializer_list<U>,Args...>::value>::type>
RESULT_WARN_UNUSED
constexpr auto fail(std::initializer_list<U> ilist, Args&&...args)
noexcept(std::is_nothrow_constructible<E, std::initializer_list<U>, Args...>::value)
-> failure<E>;
/// \brief Swaps the contents of two failure values
///
/// \param lhs the left failure
/// \param rhs the right failure
template <typename E>
auto swap(failure<E>& lhs, failure<E>& rhs)
#if __cplusplus >= 201703L
noexcept(std::is_nothrow_swappable<E>::value) -> void;
#else
noexcept(std::is_nothrow_move_constructible<E>::value) -> void;
#endif
namespace detail {
//=========================================================================
// class : unit
//=========================================================================
/// \brief A standalone monostate object (effectively std::monostate). This
/// exists to allow for `void` specializations
struct unit {};
//=========================================================================
// non-member functions : class : unit
//=========================================================================
constexpr auto operator==(unit, unit) noexcept -> bool { return true; }
constexpr auto operator!=(unit, unit) noexcept -> bool { return false; }
constexpr auto operator<(unit, unit) noexcept -> bool { return false; }
constexpr auto operator>(unit, unit) noexcept -> bool { return false; }
constexpr auto operator<=(unit, unit) noexcept -> bool { return true; }
constexpr auto operator>=(unit, unit) noexcept -> bool { return true; }
//=========================================================================
// class : detail::result_union<T, E, IsTrivial>
//=========================================================================
///////////////////////////////////////////////////////////////////////////
/// \brief A basic utility that acts as a union containing the T and E
/// types
///
/// This is specialized on the case that both T and E are trivial, in which
/// case `result_union` is also trivial
///
/// \tparam T the value type result to be returned
/// \tparam E the error type returned on failure
/// \tparam IsTrivial Whether or not both T and E are trivial
///////////////////////////////////////////////////////////////////////////
template <typename T, typename E,
bool IsTrivial = std::is_trivially_destructible<T>::value &&
std::is_trivially_destructible<E>::value>
struct result_union
{
//-----------------------------------------------------------------------
// Public Member Types
//-----------------------------------------------------------------------
using underlying_value_type = wrapped_result_type<T>;
using underlying_error_type = E;
//-----------------------------------------------------------------------
// Constructors / Assignment
//-----------------------------------------------------------------------
/// \brief Constructs an empty object
///
/// This is for use with conversion constructors, since it allows a
/// temporary unused object to be set
result_union(unit) noexcept;
/// \brief Constructs the underlying value from the specified \p args
///
/// \param args the arguments to forward to T's constructor
template <typename...Args>
constexpr result_union(in_place_t, Args&&...args)
noexcept(std::is_nothrow_constructible<T, Args...>::value);
/// \brief Constructs the underlying error from the specified \p args
///
/// \param args the arguments to forward to E's constructor
template <typename...Args>
constexpr result_union(in_place_error_t, Args&&...args)
noexcept(std::is_nothrow_constructible<E, Args...>::value);
result_union(const result_union&) = default;
result_union(result_union&&) = default;
//-----------------------------------------------------------------------
auto operator=(const result_union&) -> result_union& = default;
auto operator=(result_union&&) -> result_union& = default;
//-----------------------------------------------------------------------
// Modifiers
//-----------------------------------------------------------------------
/// \brief A no-op for trivial types
auto destroy() const noexcept -> void;
//-----------------------------------------------------------------------
// Public Members
//-----------------------------------------------------------------------
union {
underlying_value_type m_value;
underlying_error_type m_error;
unit m_empty;
};
bool m_has_value;
};
//-------------------------------------------------------------------------
template <typename T, typename E>
struct result_union<T, E, false>
{
//-----------------------------------------------------------------------
// Public Member Types
//-----------------------------------------------------------------------
using underlying_value_type = wrapped_result_type<T>;
using underlying_error_type = E;
//-----------------------------------------------------------------------
// Constructors / Assignment / Destructor
//-----------------------------------------------------------------------
/// \brief Constructs an empty object
///
/// This is for use with conversion constructors, since it allows a
/// temporary unused object to be set
result_union(unit) noexcept;
/// \brief Constructs the underlying value from the specified \p args
///
/// \param args the arguments to forward to T's constructor
template <typename...Args>
constexpr result_union(in_place_t, Args&&...args)
noexcept(std::is_nothrow_constructible<T, Args...>::value);
/// \brief Constructs the underlying error from the specified \p args
///
/// \param args the arguments to forward to E's constructor
template <typename...Args>
constexpr result_union(in_place_error_t, Args&&...args)
noexcept(std::is_nothrow_constructible<E, Args...>::value);
result_union(const result_union&) = default;
result_union(result_union&&) = default;
//-----------------------------------------------------------------------
/// \brief Destroys the underlying stored object
~result_union()
noexcept(std::is_nothrow_destructible<T>::value &&
std::is_nothrow_destructible<E>::value);
//-----------------------------------------------------------------------
auto operator=(const result_union&) -> result_union& = default;
auto operator=(result_union&&) -> result_union& = default;
//-----------------------------------------------------------------------
// Modifiers
//-----------------------------------------------------------------------
/// \brief Destroys the underlying stored object
auto destroy() -> void;
//-----------------------------------------------------------------------
// Public Members
//-----------------------------------------------------------------------
union {
underlying_value_type m_value;
underlying_error_type m_error;
unit m_empty;
};
bool m_has_value;
};
//=========================================================================
// class : result_construct_base<T, E>
//=========================================================================
///////////////////////////////////////////////////////////////////////////
/// \brief Base class of assignment to enable construction and assignment
///
/// This class is used with several pieces of construction to ensure
/// trivial constructibility and assignability:
///
/// * `result_trivial_copy_ctor_base`
/// * `result_trivial_move_ctor_base`
/// * `result_copy_assign_base`
/// * `result_move_assign_base`
///
/// \tparam T the value type
/// \tparam E the error type
///////////////////////////////////////////////////////////////////////////
template <typename T, typename E>
struct result_construct_base
{
//-----------------------------------------------------------------------
// Constructors / Assignment
//-----------------------------------------------------------------------
/// \brief Constructs an empty object
///
/// This is for use with conversion constructors, since it allows a
/// temporary unused object to be set
result_construct_base(unit) noexcept;
/// \brief Constructs the underlying value from the specified \p args
///
/// \param args the arguments to forward to T's constructor
template <typename...Args>
constexpr result_construct_base(in_place_t, Args&&...args)
noexcept(std::is_nothrow_constructible<T, Args...>::value);
/// \brief Constructs the underlying error from the specified \p args
///
/// \param args the arguments to forward to E's constructor
template <typename...Args>
constexpr result_construct_base(in_place_error_t, Args&&...args)
noexcept(std::is_nothrow_constructible<E, Args...>::value);
result_construct_base(const result_construct_base&) = default;
result_construct_base(result_construct_base&&) = default;
auto operator=(const result_construct_base&) -> result_construct_base& = default;
auto operator=(result_construct_base&&) -> result_construct_base& = default;
//-----------------------------------------------------------------------
// Construction / Assignment
//-----------------------------------------------------------------------
/// \brief Constructs the value type from \p args
///
/// \note This is an implementation detail only meant to be used during
/// construction
///
/// \pre there is no contained value or error at the time of construction
///
/// \param args the arguments to forward to T's constructor
template <typename...Args>
auto construct_value(Args&&...args)
noexcept(std::is_nothrow_constructible<T,Args...>::value) -> void;
/// \brief Constructs the error type from \p args
///
/// \note This is an implementation detail only meant to be used during
/// construction
///
/// \pre there is no contained value or error at the time of construction
///
/// \param args the arguments to forward to E's constructor
template <typename...Args>
auto construct_error(Args&&...args)
noexcept(std::is_nothrow_constructible<E,Args...>::value) -> void;
/// \brief Constructs the underlying error from the \p other result
///
/// If \p other contains a value, then the T type will be
/// default-constructed.
///
/// \note This is an implementation detail only meant to be used during
/// construction of `result<void, E>` types
///
/// \pre there is no contained value or error at the time of construction
///
/// \param other the other result to construct
template <typename Result>
auto construct_error_from_result(Result&& other) -> void;
/// \brief Constructs the underlying type from a result object
///
/// \note This is an implementation detail only meant to be used during
/// construction
///
/// \pre there is no contained value or error at the time of construction
///
/// \param other the other result to construct
template <typename Result>
auto construct_from_result(Result&& other) -> void;
//-----------------------------------------------------------------------