@@ -33,17 +33,17 @@ struct Dummy {
33
33
34
34
struct ThrowsCtorT {
35
35
ThrowsCtorT (int ) noexcept (false ) {}
36
- ThrowsCtorT & operator =(int ) noexcept { return *this ; }
36
+ ThrowsCtorT& operator =(int ) noexcept { return *this ; }
37
37
};
38
38
39
39
struct ThrowsAssignT {
40
40
ThrowsAssignT (int ) noexcept {}
41
- ThrowsAssignT & operator =(int ) noexcept (false ) { return *this ; }
41
+ ThrowsAssignT& operator =(int ) noexcept (false ) { return *this ; }
42
42
};
43
43
44
44
struct NoThrowT {
45
45
NoThrowT (int ) noexcept {}
46
- NoThrowT & operator =(int ) noexcept { return *this ; }
46
+ NoThrowT& operator =(int ) noexcept { return *this ; }
47
47
};
48
48
49
49
} // namespace MetaHelpers
@@ -55,7 +55,7 @@ struct ThrowsCtorT {
55
55
int value;
56
56
ThrowsCtorT () : value(0 ) {}
57
57
ThrowsCtorT (int ) noexcept (false ) { throw 42 ; }
58
- ThrowsCtorT & operator =(int v) noexcept {
58
+ ThrowsCtorT& operator =(int v) noexcept {
59
59
value = v;
60
60
return *this ;
61
61
}
@@ -64,9 +64,12 @@ struct ThrowsCtorT {
64
64
struct MoveCrashes {
65
65
int value;
66
66
MoveCrashes (int v = 0 ) noexcept : value{v} {}
67
- MoveCrashes (MoveCrashes &&) noexcept { assert (false ); }
68
- MoveCrashes &operator =(MoveCrashes &&) noexcept { assert (false ); return *this ; }
69
- MoveCrashes &operator =(int v) noexcept {
67
+ MoveCrashes (MoveCrashes&&) noexcept { assert (false ); }
68
+ MoveCrashes& operator =(MoveCrashes&&) noexcept {
69
+ assert (false );
70
+ return *this ;
71
+ }
72
+ MoveCrashes& operator =(int v) noexcept {
70
73
value = v;
71
74
return *this ;
72
75
}
@@ -76,8 +79,8 @@ struct ThrowsCtorTandMove {
76
79
int value;
77
80
ThrowsCtorTandMove () : value(0 ) {}
78
81
ThrowsCtorTandMove (int ) noexcept (false ) { throw 42 ; }
79
- ThrowsCtorTandMove (ThrowsCtorTandMove &&) noexcept (false ) { assert (false ); }
80
- ThrowsCtorTandMove & operator =(int v) noexcept {
82
+ ThrowsCtorTandMove (ThrowsCtorTandMove&&) noexcept (false ) { assert (false ); }
83
+ ThrowsCtorTandMove& operator =(int v) noexcept {
81
84
value = v;
82
85
return *this ;
83
86
}
@@ -87,14 +90,14 @@ struct ThrowsAssignT {
87
90
int value;
88
91
ThrowsAssignT () : value(0 ) {}
89
92
ThrowsAssignT (int v) noexcept : value(v) {}
90
- ThrowsAssignT & operator =(int ) noexcept (false ) { throw 42 ; }
93
+ ThrowsAssignT& operator =(int ) noexcept (false ) { throw 42 ; }
91
94
};
92
95
93
96
struct NoThrowT {
94
97
int value;
95
98
NoThrowT () : value(0 ) {}
96
99
NoThrowT (int v) noexcept : value(v) {}
97
- NoThrowT & operator =(int v) noexcept {
100
+ NoThrowT& operator =(int v) noexcept {
98
101
value = v;
99
102
return *this ;
100
103
}
@@ -126,29 +129,25 @@ void test_T_assignment_sfinae() {
126
129
}
127
130
{
128
131
using V = std::variant<std::string, std::string>;
129
- static_assert (!std::is_assignable<V, const char *>::value, " ambiguous" );
132
+ static_assert (!std::is_assignable<V, const char *>::value, " ambiguous" );
130
133
}
131
134
{
132
- using V = std::variant<std::string, void *>;
135
+ using V = std::variant<std::string, void *>;
133
136
static_assert (!std::is_assignable<V, int >::value, " no matching operator=" );
134
137
}
135
138
{
136
139
using V = std::variant<std::string, float >;
137
- static_assert (std::is_assignable<V, int >::value == VariantAllowsNarrowingConversions,
138
- " no matching operator=" );
140
+ static_assert (std::is_assignable<V, int >::value == VariantAllowsNarrowingConversions, " no matching operator=" );
139
141
}
140
142
{
141
143
using V = std::variant<std::unique_ptr<int >, bool >;
142
- static_assert (!std::is_assignable<V, std::unique_ptr<char >>::value,
143
- " no explicit bool in operator=" );
144
+ static_assert (!std::is_assignable<V, std::unique_ptr<char >>::value, " no explicit bool in operator=" );
144
145
struct X {
145
146
operator void *();
146
147
};
147
- static_assert (!std::is_assignable<V, X>::value,
148
- " no boolean conversion in operator=" );
148
+ static_assert (!std::is_assignable<V, X>::value, " no boolean conversion in operator=" );
149
149
#ifndef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
150
- static_assert (std::is_assignable<V, std::false_type>::value,
151
- " converted to bool in operator=" );
150
+ static_assert (std::is_assignable<V, std::false_type>::value, " converted to bool in operator=" );
152
151
#endif
153
152
}
154
153
{
@@ -157,22 +156,21 @@ void test_T_assignment_sfinae() {
157
156
operator X ();
158
157
};
159
158
using V = std::variant<X>;
160
- static_assert (std::is_assignable<V, Y>::value,
161
- " regression on user-defined conversions in operator=" );
159
+ static_assert (std::is_assignable<V, Y>::value, " regression on user-defined conversions in operator=" );
162
160
}
163
161
#if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
164
162
{
165
- using V = std::variant<int , int &&>;
163
+ using V = std::variant<int , int &&>;
166
164
static_assert (!std::is_assignable<V, int >::value, " ambiguous" );
167
165
}
168
166
{
169
- using V = std::variant<int , const int &>;
167
+ using V = std::variant<int , const int &>;
170
168
static_assert (!std::is_assignable<V, int >::value, " ambiguous" );
171
169
}
172
170
#endif // TEST_VARIANT_HAS_NO_REFERENCES
173
171
}
174
172
175
- void test_T_assignment_basic () {
173
+ TEST_CONSTEXPR_CXX20 bool test_T_assignment_basic () {
176
174
{
177
175
std::variant<int > v (43 );
178
176
v = 42 ;
@@ -201,35 +199,18 @@ void test_T_assignment_basic() {
201
199
#endif
202
200
{
203
201
std::variant<std::string, bool > v = true ;
204
- v = " bar" ;
202
+ v = " bar" ;
205
203
assert (v.index () == 0 );
206
204
assert (std::get<0 >(v) == " bar" );
207
205
}
208
- {
209
- std::variant<bool , std::unique_ptr<int >> v;
210
- v = nullptr ;
211
- assert (v.index () == 1 );
212
- assert (std::get<1 >(v) == nullptr );
213
- }
214
- #if !defined(TEST_VARIANT_HAS_NO_REFERENCES)
215
- {
216
- using V = std::variant<int &, int &&, long >;
217
- int x = 42 ;
218
- V v (43l );
219
- v = x;
220
- assert (v.index () == 0 );
221
- assert (&std::get<0 >(v) == &x);
222
- v = std::move (x);
223
- assert (v.index () == 1 );
224
- assert (&std::get<1 >(v) == &x);
225
- // 'long' is selected by FUN(const int &) since 'const int &' cannot bind
226
- // to 'int&'.
227
- const int &cx = x;
228
- v = cx;
229
- assert (v.index () == 2 );
230
- assert (std::get<2 >(v) == 42 );
231
- }
232
- #endif // TEST_VARIANT_HAS_NO_REFERENCES
206
+ return true ;
207
+ }
208
+
209
+ void test_T_assignment_basic_no_constexpr () {
210
+ std::variant<bool , std::unique_ptr<int >> v;
211
+ v = nullptr ;
212
+ assert (v.index () == 1 );
213
+ assert (std::get<1 >(v) == nullptr );
233
214
}
234
215
235
216
void test_T_assignment_performs_construction () {
@@ -298,23 +279,29 @@ void test_T_assignment_performs_assignment() {
298
279
#endif // TEST_HAS_NO_EXCEPTIONS
299
280
}
300
281
301
- void test_T_assignment_vector_bool () {
282
+ TEST_CONSTEXPR_CXX20 bool test_T_assignment_vector_bool () {
302
283
#ifndef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
303
284
std::vector<bool > vec = {true };
304
285
std::variant<bool , int > v;
305
286
v = vec[0 ];
306
287
assert (v.index () == 0 );
307
288
assert (std::get<0 >(v) == true );
308
289
#endif
290
+ return true ;
309
291
}
310
292
311
293
int main (int , char **) {
312
294
test_T_assignment_basic ();
295
+ test_T_assignment_basic_no_constexpr ();
313
296
test_T_assignment_performs_construction ();
314
297
test_T_assignment_performs_assignment ();
315
298
test_T_assignment_noexcept ();
316
299
test_T_assignment_sfinae ();
317
300
test_T_assignment_vector_bool ();
318
301
302
+ #if TEST_STD_VER >= 20
303
+ static_assert (test_T_assignment_basic ());
304
+ static_assert (test_T_assignment_vector_bool ());
305
+ #endif
319
306
return 0 ;
320
307
}
0 commit comments