|
10 | 10 | // <span> |
11 | 11 |
|
12 | 12 | // constexpr iterator begin() const noexcept; |
13 | | -// constexpr const_iterator cbegin() const noexcept; |
| 13 | +// constexpr const_iterator cbegin() const noexcept; // since C++23 |
14 | 14 |
|
15 | 15 | #include <span> |
16 | 16 | #include <cassert> |
17 | 17 | #include <string> |
18 | 18 |
|
19 | 19 | #include "test_macros.h" |
20 | 20 |
|
21 | | -template <class Span> |
22 | | -constexpr bool testConstexprSpan(Span s) |
23 | | -{ |
24 | | - bool ret = true; |
25 | | - typename Span::iterator b = s.begin(); |
26 | | - |
27 | | - if (s.empty()) |
28 | | - { |
29 | | - ret = ret && (b == s.end()); |
30 | | - } |
31 | | - else |
32 | | - { |
33 | | - ret = ret && ( *b == s[0]); |
34 | | - ret = ret && (&*b == &s[0]); |
35 | | - } |
36 | | - return ret; |
| 21 | +template <class Span, class Iter> |
| 22 | +constexpr bool testSpanImpl(Span s, Iter first) { |
| 23 | + bool ret = true; |
| 24 | + if (s.empty()) { |
| 25 | + ret = ret && (first == s.end()); |
| 26 | + } else { |
| 27 | + ret = ret && (*first == s[0]); |
| 28 | + ret = ret && (&*first == &s[0]); |
| 29 | + } |
| 30 | + return ret; |
37 | 31 | } |
38 | 32 |
|
| 33 | +template <class EType, size_t Extent, class... Args> |
| 34 | +constexpr bool testSpan(Args&&... args) { |
| 35 | + auto s1 = std::span<EType>(std::forward<Args>(args)...); |
| 36 | + bool ret = true; |
39 | 37 |
|
40 | | -template <class Span> |
41 | | -void testRuntimeSpan(Span s) |
42 | | -{ |
43 | | - typename Span::iterator b = s.begin(); |
44 | | - |
45 | | - if (s.empty()) |
46 | | - { |
47 | | - assert(b == s.end()); |
48 | | - } |
49 | | - else |
50 | | - { |
51 | | - assert( *b == s[0]); |
52 | | - assert(&*b == &s[0]); |
53 | | - } |
| 38 | + ret = ret && testSpanImpl(s1, s1.begin()); |
| 39 | +#if TEST_STD_VER >= 23 |
| 40 | + ret = ret && testSpanImpl(s1, s1.cbegin()); |
| 41 | +#endif |
| 42 | + |
| 43 | + auto s2 = std::span<EType, Extent>(std::forward<Args>(args)...); |
| 44 | + ret = ret && testSpanImpl(s2, s2.begin()); |
| 45 | +#if TEST_STD_VER >= 23 |
| 46 | + ret = ret && testSpanImpl(s2, s2.cbegin()); |
| 47 | +#endif |
| 48 | + |
| 49 | + return ret; |
54 | 50 | } |
55 | 51 |
|
56 | | -struct A{}; |
57 | | -bool operator==(A, A) {return true;} |
58 | | - |
59 | | -constexpr int iArr1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
60 | | - int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; |
61 | | - |
62 | | - |
63 | | -int main(int, char**) |
64 | | -{ |
65 | | - static_assert(testConstexprSpan(std::span<int>()), ""); |
66 | | - static_assert(testConstexprSpan(std::span<long>()), ""); |
67 | | - static_assert(testConstexprSpan(std::span<double>()), ""); |
68 | | - static_assert(testConstexprSpan(std::span<A>()), ""); |
69 | | - static_assert(testConstexprSpan(std::span<std::string>()), ""); |
70 | | - |
71 | | - static_assert(testConstexprSpan(std::span<int, 0>()), ""); |
72 | | - static_assert(testConstexprSpan(std::span<long, 0>()), ""); |
73 | | - static_assert(testConstexprSpan(std::span<double, 0>()), ""); |
74 | | - static_assert(testConstexprSpan(std::span<A, 0>()), ""); |
75 | | - static_assert(testConstexprSpan(std::span<std::string, 0>()), ""); |
76 | | - |
77 | | - static_assert(testConstexprSpan(std::span<const int>(iArr1, 1)), ""); |
78 | | - static_assert(testConstexprSpan(std::span<const int>(iArr1, 2)), ""); |
79 | | - static_assert(testConstexprSpan(std::span<const int>(iArr1, 3)), ""); |
80 | | - static_assert(testConstexprSpan(std::span<const int>(iArr1, 4)), ""); |
81 | | - static_assert(testConstexprSpan(std::span<const int>(iArr1, 5)), ""); |
82 | | - |
83 | | - |
84 | | - testRuntimeSpan(std::span<int> ()); |
85 | | - testRuntimeSpan(std::span<long> ()); |
86 | | - testRuntimeSpan(std::span<double> ()); |
87 | | - testRuntimeSpan(std::span<A> ()); |
88 | | - testRuntimeSpan(std::span<std::string>()); |
89 | | - |
90 | | - testRuntimeSpan(std::span<int, 0> ()); |
91 | | - testRuntimeSpan(std::span<long, 0> ()); |
92 | | - testRuntimeSpan(std::span<double, 0> ()); |
93 | | - testRuntimeSpan(std::span<A, 0> ()); |
94 | | - testRuntimeSpan(std::span<std::string, 0>()); |
95 | | - |
96 | | - testRuntimeSpan(std::span<int>(iArr2, 1)); |
97 | | - testRuntimeSpan(std::span<int>(iArr2, 2)); |
98 | | - testRuntimeSpan(std::span<int>(iArr2, 3)); |
99 | | - testRuntimeSpan(std::span<int>(iArr2, 4)); |
100 | | - testRuntimeSpan(std::span<int>(iArr2, 5)); |
101 | | - |
102 | | - std::string s; |
103 | | - testRuntimeSpan(std::span<std::string>(&s, (std::size_t) 0)); |
104 | | - testRuntimeSpan(std::span<std::string>(&s, 1)); |
| 52 | +struct A {}; |
| 53 | +bool operator==(A, A) { return true; } |
| 54 | + |
| 55 | +constexpr int iArr1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 56 | +int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; |
| 57 | + |
| 58 | +int main(int, char**) { |
| 59 | + ASSERT_RUNTIME_AND_CONSTEXPR(testSpan<int, 0>()); |
| 60 | + ASSERT_RUNTIME_AND_CONSTEXPR(testSpan<long, 0>()); |
| 61 | + ASSERT_RUNTIME_AND_CONSTEXPR(testSpan<double, 0>()); |
| 62 | + ASSERT_RUNTIME_AND_CONSTEXPR(testSpan<A, 0>()); |
| 63 | + ASSERT_RUNTIME_AND_CONSTEXPR(testSpan<std::string, 0>()); |
| 64 | + |
| 65 | + ASSERT_RUNTIME_AND_CONSTEXPR(testSpan<const int, 1>(iArr1, 1)); |
| 66 | + ASSERT_RUNTIME_AND_CONSTEXPR(testSpan<const int, 2>(iArr1, 2)); |
| 67 | + ASSERT_RUNTIME_AND_CONSTEXPR(testSpan<const int, 3>(iArr1, 3)); |
| 68 | + ASSERT_RUNTIME_AND_CONSTEXPR(testSpan<const int, 4>(iArr1, 4)); |
| 69 | + ASSERT_RUNTIME_AND_CONSTEXPR(testSpan<const int, 5>(iArr1, 5)); |
| 70 | + |
| 71 | + testSpan<int, 1>(iArr2, 1); |
| 72 | + testSpan<int, 2>(iArr2, 2); |
| 73 | + testSpan<int, 3>(iArr2, 3); |
| 74 | + testSpan<int, 4>(iArr2, 4); |
| 75 | + testSpan<int, 5>(iArr2, 5); |
| 76 | + |
| 77 | + std::string s1; |
| 78 | + constexpr static std::string s2; |
| 79 | + testSpan<std::string, 0>(&s1, static_cast<size_t>(0)); |
| 80 | + ASSERT_RUNTIME_AND_CONSTEXPR(testSpan<const std::string, 0>(&s2, static_cast<size_t>(0))); |
| 81 | + testSpan<std::string, 1>(&s1, 1); |
| 82 | + ASSERT_RUNTIME_AND_CONSTEXPR(testSpan<const std::string, 1>(&s2, 1)); |
105 | 83 |
|
106 | 84 | return 0; |
107 | 85 | } |
0 commit comments