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
|
// { dg-options "-std=gnu++23" }
// { dg-do compile { target c++23 } }
#include <expected>
#include <type_traits>
// Default construction
template<typename T, typename E>
constexpr bool default_constructible
= std::is_default_constructible_v<std::expected<T, E>>;
struct A { A(int); };
static_assert( default_constructible< int, int > );
static_assert( default_constructible< A, int > == false );
static_assert( default_constructible< int, A > );
static_assert( default_constructible< A, A > == false );
static_assert( default_constructible< int, A > );
static_assert( default_constructible< void, int > );
// Destruction
template<typename T, typename E>
constexpr bool trivially_destructible
= std::is_trivially_destructible_v<std::expected<T, E>>;
struct B { ~B(); };
static_assert( trivially_destructible< int, int > );
static_assert( trivially_destructible< B, int > == false );
static_assert( trivially_destructible< int, B > == false );
static_assert( trivially_destructible< B, B > == false );
static_assert( trivially_destructible< void, int > );
static_assert( trivially_destructible< void, B > == false );
enum Result { No, Yes, NoThrow, Trivial };
// Copy construction
template<typename T, typename E>
constexpr Result copy_constructible
= std::is_trivially_copy_constructible_v<std::expected<T, E>> ? Trivial
: std::is_copy_constructible_v<std::expected<T, E>> ? Yes
: No;
struct C { C(const C&); };
struct D { D(D&&); };
static_assert( copy_constructible< int, int > == Trivial );
static_assert( copy_constructible< C, C > == Yes );
static_assert( copy_constructible< C, int > == Yes );
static_assert( copy_constructible< int, C > == Yes );
static_assert( copy_constructible< int, D > == No );
static_assert( copy_constructible< D, int > == No );
static_assert( copy_constructible< D, D > == No );
static_assert( copy_constructible< void, int > == Trivial );
static_assert( copy_constructible< void, C > == Yes );
static_assert( copy_constructible< void, D > == No );
// Move construction
template<typename T, typename E>
constexpr Result move_constructible
= std::is_trivially_move_constructible_v<std::expected<T, E>> ? Trivial
: std::is_nothrow_move_constructible_v<std::expected<T, E>> ? NoThrow
: std::is_move_constructible_v<std::expected<T, E>> ? Yes
: No;
struct E { E(E&&) noexcept; };
static_assert( move_constructible< int, int > == Trivial );
static_assert( move_constructible< C, C > == Yes );
static_assert( move_constructible< C, int > == Yes );
static_assert( move_constructible< int, C > == Yes );
static_assert( move_constructible< D, D > == Yes );
static_assert( move_constructible< D, int > == Yes );
static_assert( move_constructible< int, D > == Yes );
static_assert( move_constructible< E, E > == NoThrow );
static_assert( move_constructible< E, int > == NoThrow );
static_assert( move_constructible< int, E > == NoThrow );
static_assert( move_constructible< void, int > == Trivial );
static_assert( move_constructible< void, C > == Yes );
static_assert( move_constructible< void, D > == Yes );
static_assert( move_constructible< void, E > == NoThrow );
// Copy assignment
template<typename T, typename E>
constexpr bool copy_assignable
= std::is_copy_assignable_v<std::expected<T, E>>;
struct F { F(F&&); F& operator=(const F&); }; // not copy-constructible
struct G { G(const G&); G(G&&); G& operator=(const G&); }; // throwing move
static_assert( copy_assignable< int, int > );
static_assert( copy_assignable< F, int > == false );
static_assert( copy_assignable< int, F > == false );
static_assert( copy_assignable< F, F > == false );
static_assert( copy_assignable< G, int > );
static_assert( copy_assignable< int, G > );
static_assert( copy_assignable< G, G > == false );
static_assert( copy_assignable< void, int > );
static_assert( copy_assignable< void, F > == false );
static_assert( copy_assignable< void, G > );
// Move assignment
template<typename T, typename E>
constexpr bool move_assignable
= std::is_move_assignable_v<std::expected<T, E>>;
static_assert( move_assignable< int, int > );
static_assert( move_assignable< F, int > );
static_assert( move_assignable< int, F > );
static_assert( move_assignable< F, F > == false );
static_assert( move_assignable< G, int > );
static_assert( move_assignable< int, G > );
static_assert( move_assignable< G, G > == false );
static_assert( move_assignable< void, int > );
static_assert( move_assignable< void, F > );
static_assert( move_assignable< void, G > );
// QoI properties
static_assert( sizeof(std::expected<char, unsigned char>) == 2 );
static_assert( sizeof(std::expected<void, char>) == 2 );
static_assert( sizeof(std::expected<void*, char>) == 2 * __alignof(void*) );
static_assert( alignof(std::expected<void, char>) == 1 );
static_assert( alignof(std::expected<void*, char>) == alignof(void*) );
|