@@ -49,6 +49,22 @@ template <class Tp>
49
49
constexpr bool can_swap () {
50
50
return std::is_same<decltype (can_swap_test<Tp>(0 )), void >::value;
51
51
}
52
+
53
+ TEST_CONSTEXPR_CXX23 void test_unique_ptr () {
54
+ std::unique_ptr<int > i[3 ];
55
+ for (int k = 0 ; k < 3 ; ++k)
56
+ i[k].reset (new int (k + 1 ));
57
+ std::unique_ptr<int > j[3 ];
58
+ for (int k = 0 ; k < 3 ; ++k)
59
+ j[k].reset (new int (k + 4 ));
60
+ std::swap (i, j);
61
+ assert (*i[0 ] == 4 );
62
+ assert (*i[1 ] == 5 );
63
+ assert (*i[2 ] == 6 );
64
+ assert (*j[0 ] == 1 );
65
+ assert (*j[1 ] == 2 );
66
+ assert (*j[2 ] == 3 );
67
+ }
52
68
#endif
53
69
54
70
TEST_CONSTEXPR_CXX20 bool test () {
@@ -121,22 +137,10 @@ TEST_CONSTEXPR_CXX20 bool test() {
121
137
static_assert (noexcept (std::swap (ma, ma)), " " );
122
138
}
123
139
124
- // We can't test unique_ptr in constant evaluation before C++23 as it's constexpr only since C++23.
125
- if (TEST_STD_VER >= 23 || !TEST_IS_CONSTANT_EVALUATED) {
126
- std::unique_ptr<int > i[3 ];
127
- for (int k = 0 ; k < 3 ; ++k)
128
- i[k].reset (new int (k + 1 ));
129
- std::unique_ptr<int > j[3 ];
130
- for (int k = 0 ; k < 3 ; ++k)
131
- j[k].reset (new int (k + 4 ));
132
- std::swap (i, j);
133
- assert (*i[0 ] == 4 );
134
- assert (*i[1 ] == 5 );
135
- assert (*i[2 ] == 6 );
136
- assert (*j[0 ] == 1 );
137
- assert (*j[1 ] == 2 );
138
- assert (*j[2 ] == 3 );
139
- }
140
+ // `unique_ptr` is constexpr only since C++23, so we can't inline `test_unique_ptr()` into `test()`
141
+ // because `test()` is constexpr since C++20.
142
+ if (TEST_STD_VER >= 23 || !TEST_IS_CONSTANT_EVALUATED)
143
+ test_unique_ptr ();
140
144
#endif
141
145
142
146
return true ;
0 commit comments