1
1
// Copyright (c) Microsoft Corporation.
2
2
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3
3
4
+ #define _SILENCE_STDEXT_ARR_ITERS_DEPRECATION_WARNING
5
+
4
6
#include < algorithm>
5
7
#include < cassert>
8
+ #include < cstddef>
6
9
#include < iterator>
10
+ #include < memory>
11
+ #include < tuple>
7
12
#include < type_traits>
8
13
#include < utility>
9
14
#include < vector>
10
15
16
+ #define STATIC_ASSERT (...) static_assert(__VA_ARGS__, #__VA_ARGS__)
17
+
18
+ template <class T >
19
+ void check_checked_array_iterator_category_and_convertibility () {
20
+ STATIC_ASSERT (std::is_same_v<typename stdext::checked_array_iterator<T*>::iterator_category,
21
+ std::random_access_iterator_tag>);
22
+
23
+ STATIC_ASSERT (std::is_same_v<typename stdext::checked_array_iterator<T*>::value_type, std::remove_cv_t <T>>);
24
+
25
+ STATIC_ASSERT (std::is_same_v<typename stdext::checked_array_iterator<T*>::difference_type, std::ptrdiff_t >);
26
+
27
+ STATIC_ASSERT (std::is_same_v<typename stdext::checked_array_iterator<T*>::pointer, T*>);
28
+
29
+ STATIC_ASSERT (std::is_same_v<typename stdext::checked_array_iterator<T*>::reference, T&>);
30
+
31
+ STATIC_ASSERT (std::is_convertible_v<stdext::checked_array_iterator<T*>, stdext::checked_array_iterator<const T*>>);
32
+
33
+ #ifdef __cpp_lib_concepts
34
+ STATIC_ASSERT (
35
+ std::is_same_v<typename stdext::checked_array_iterator<T*>::iterator_concept, std::contiguous_iterator_tag>);
36
+
37
+ STATIC_ASSERT (std::contiguous_iterator<stdext::checked_array_iterator<T*>>);
38
+ #endif // __cpp_lib_concepts
39
+ }
40
+
41
+ template <class T >
42
+ void check_unchecked_array_iterator_category_and_convertibility () {
43
+ STATIC_ASSERT (std::is_same_v<typename stdext::unchecked_array_iterator<T*>::iterator_category,
44
+ std::random_access_iterator_tag>);
45
+
46
+ STATIC_ASSERT (std::is_same_v<typename stdext::unchecked_array_iterator<T*>::value_type, std::remove_cv_t <T>>);
47
+
48
+ STATIC_ASSERT (std::is_same_v<typename stdext::unchecked_array_iterator<T*>::difference_type, std::ptrdiff_t >);
49
+
50
+ STATIC_ASSERT (std::is_same_v<typename stdext::unchecked_array_iterator<T*>::pointer, T*>);
51
+
52
+ STATIC_ASSERT (std::is_same_v<typename stdext::unchecked_array_iterator<T*>::reference, T&>);
53
+
54
+ STATIC_ASSERT (
55
+ std::is_convertible_v<stdext::unchecked_array_iterator<T*>, stdext::unchecked_array_iterator<const T*>>);
56
+
57
+ #ifdef __cpp_lib_concepts
58
+ STATIC_ASSERT (
59
+ std::is_same_v<typename stdext::unchecked_array_iterator<T*>::iterator_concept, std::contiguous_iterator_tag>);
60
+
61
+ STATIC_ASSERT (std::contiguous_iterator<stdext::unchecked_array_iterator<T*>>);
62
+ #endif // __cpp_lib_concepts
63
+ }
64
+
11
65
int main () {
12
66
{
67
+ check_checked_array_iterator_category_and_convertibility<int >();
68
+ check_checked_array_iterator_category_and_convertibility<const int >();
69
+ check_checked_array_iterator_category_and_convertibility<std::tuple<const char *>>();
70
+
71
+
13
72
int * const p = new int [9 ];
14
73
15
74
for (int i = 0 ; i < 9 ; ++i) {
@@ -19,31 +78,28 @@ int main() {
19
78
20
79
auto cat = stdext::make_checked_array_iterator (p, 9 );
21
80
22
- static_assert (std::is_same_v<decltype (cat), stdext::checked_array_iterator<int *>>,
23
- " stdext::make_checked_array_iterator(p, 9)'s return type is wrong!" );
24
-
25
-
26
- auto dog = stdext::make_checked_array_iterator (p, 9 , 3 );
27
-
28
- static_assert (std::is_same_v<decltype (dog), stdext::checked_array_iterator<int *>>,
29
- " stdext::make_checked_array_iterator(p, 9, 3)'s return type is wrong!" );
30
-
81
+ STATIC_ASSERT (std::is_same_v<decltype (cat), stdext::checked_array_iterator<int *>>);
31
82
32
- static_assert (
33
- std::is_same_v<stdext::checked_array_iterator<int *>::iterator_category, std::random_access_iterator_tag>,
34
- " stdext::checked_array_iterator<int *>::iterator_category is wrong!" );
83
+ #if _HAS_CXX20
84
+ assert (std::to_address (cat) == &*cat);
85
+ assert (std::to_address (cat + 8 ) == &*cat + 8 );
86
+ assert (std::to_address (cat + 8 ) == std::to_address (cat) + 8 );
87
+ assert (std::to_address (cat + 9 ) == std::to_address (cat) + 9 );
88
+ #endif // _HAS_CXX20
35
89
36
- static_assert (std::is_same_v<stdext::checked_array_iterator<int *>::value_type, int >,
37
- " stdext::checked_array_iterator<int *>::value_type is wrong!" );
38
90
39
- static_assert (std::is_same_v<stdext::checked_array_iterator<int *>::difference_type, ptrdiff_t >,
40
- " stdext::checked_array_iterator<int *>::difference_type is wrong!" );
91
+ auto dog = stdext::make_checked_array_iterator (p, 9 , 3 );
41
92
42
- static_assert (std::is_same_v<stdext::checked_array_iterator<int *>::pointer, int *>,
43
- " stdext::checked_array_iterator<int *>::pointer is wrong!" );
93
+ STATIC_ASSERT (std::is_same_v<decltype (dog), stdext::checked_array_iterator<int *>>);
44
94
45
- static_assert (std::is_same_v<stdext::checked_array_iterator<int *>::reference, int &>,
46
- " stdext::checked_array_iterator<int *>::reference is wrong!" );
95
+ #if _HAS_CXX20
96
+ assert (std::to_address (dog) == &*dog);
97
+ assert (std::to_address (dog + 5 ) == &*dog + 5 );
98
+ assert (std::to_address (dog + 5 ) == std::to_address (dog) + 5 );
99
+ assert (std::to_address (dog - 3 ) == &*dog - 3 );
100
+ assert (std::to_address (dog - 3 ) == std::to_address (dog) - 3 );
101
+ assert (std::to_address (dog + 6 ) == std::to_address (dog) + 6 );
102
+ #endif // _HAS_CXX20
47
103
48
104
49
105
{
@@ -184,6 +240,11 @@ int main() {
184
240
}
185
241
186
242
{
243
+ check_unchecked_array_iterator_category_and_convertibility<int >();
244
+ check_unchecked_array_iterator_category_and_convertibility<const int >();
245
+ check_unchecked_array_iterator_category_and_convertibility<std::tuple<const char *>>();
246
+
247
+
187
248
int * const p = new int [9 ];
188
249
189
250
for (int i = 0 ; i < 9 ; ++i) {
@@ -193,31 +254,28 @@ int main() {
193
254
194
255
auto cat = stdext::make_unchecked_array_iterator (p);
195
256
196
- static_assert (std::is_same_v<decltype (cat), stdext::unchecked_array_iterator<int *>>,
197
- " stdext::make_unchecked_array_iterator(p)'s return type is wrong!" );
257
+ STATIC_ASSERT (std::is_same_v<decltype (cat), stdext::unchecked_array_iterator<int *>>);
198
258
259
+ #if _HAS_CXX20
260
+ assert (std::to_address (cat) == &*cat);
261
+ assert (std::to_address (cat + 8 ) == &*cat + 8 );
262
+ assert (std::to_address (cat + 8 ) == std::to_address (cat) + 8 );
263
+ assert (std::to_address (cat + 9 ) == std::to_address (cat) + 9 );
264
+ #endif // _HAS_CXX20
199
265
200
- auto dog = stdext::make_unchecked_array_iterator (p + 3 );
201
-
202
- static_assert (std::is_same_v<decltype (dog), stdext::unchecked_array_iterator<int *>>,
203
- " stdext::make_unchecked_array_iterator(p + 3)'s return type is wrong!" );
204
266
267
+ auto dog = stdext::make_unchecked_array_iterator (p + 3 );
205
268
206
- static_assert (
207
- std::is_same_v<stdext::unchecked_array_iterator<int *>::iterator_category, std::random_access_iterator_tag>,
208
- " stdext::unchecked_array_iterator<int *>::iterator_category is wrong!" );
209
-
210
- static_assert (std::is_same_v<stdext::unchecked_array_iterator<int *>::value_type, int >,
211
- " stdext::unchecked_array_iterator<int *>::value_type is wrong!" );
212
-
213
- static_assert (std::is_same_v<stdext::unchecked_array_iterator<int *>::difference_type, ptrdiff_t >,
214
- " stdext::unchecked_array_iterator<int *>::difference_type is wrong!" );
215
-
216
- static_assert (std::is_same_v<stdext::unchecked_array_iterator<int *>::pointer, int *>,
217
- " stdext::unchecked_array_iterator<int *>::pointer is wrong!" );
269
+ STATIC_ASSERT (std::is_same_v<decltype (dog), stdext::unchecked_array_iterator<int *>>);
218
270
219
- static_assert (std::is_same_v<stdext::unchecked_array_iterator<int *>::reference, int &>,
220
- " stdext::unchecked_array_iterator<int *>::reference is wrong!" );
271
+ #if _HAS_CXX20
272
+ assert (std::to_address (dog) == &*dog);
273
+ assert (std::to_address (dog + 5 ) == &*dog + 5 );
274
+ assert (std::to_address (dog + 5 ) == std::to_address (dog) + 5 );
275
+ assert (std::to_address (dog - 3 ) == &*dog - 3 );
276
+ assert (std::to_address (dog - 3 ) == std::to_address (dog) - 3 );
277
+ assert (std::to_address (dog + 6 ) == std::to_address (dog) + 6 );
278
+ #endif // _HAS_CXX20
221
279
222
280
223
281
{
0 commit comments