1 | //===----------------------------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 |
9 | |
10 | // <mdspan> |
11 | |
12 | // template<class OtherIndexType, size_t... OtherExtents> |
13 | // constexpr explicit(see below) extents(const extents<OtherIndexType, OtherExtents...>&) noexcept; |
14 | // |
15 | // Constraints: |
16 | // * sizeof...(OtherExtents) == rank() is true. |
17 | // * ((OtherExtents == dynamic_extent || Extents == dynamic_extent || |
18 | // OtherExtents == Extents) && ...) is true. |
19 | // |
20 | // Preconditions: |
21 | // * other.extent(r) equals Er for each r for which Er is a static extent, and |
22 | // * either |
23 | // - sizeof...(OtherExtents) is zero, or |
24 | // - other.extent(r) is representable as a value of type index_type for |
25 | // every rank index r of other. |
26 | // |
27 | // Remarks: The expression inside explicit is equivalent to: |
28 | // (((Extents != dynamic_extent) && (OtherExtents == dynamic_extent)) || ... ) || |
29 | // (numeric_limits<index_type>::max() < numeric_limits<OtherIndexType>::max()) |
30 | |
31 | #include <cassert> |
32 | #include <cstddef> |
33 | #include <limits> |
34 | #include <mdspan> |
35 | #include <type_traits> |
36 | |
37 | template <class To, class From> |
38 | constexpr void test_implicit_conversion(To dest, From src) { |
39 | assert(dest == src); |
40 | } |
41 | |
42 | template <bool implicit, class To, class From> |
43 | constexpr void test_conversion(From src) { |
44 | To dest(src); |
45 | assert(dest == src); |
46 | if constexpr (implicit) { |
47 | dest = src; |
48 | assert(dest == src); |
49 | test_implicit_conversion<To, From>(src, src); |
50 | } |
51 | } |
52 | |
53 | template <class T1, class T2> |
54 | constexpr void test_conversion() { |
55 | constexpr size_t D = std::dynamic_extent; |
56 | constexpr bool idx_convertible = |
57 | static_cast<size_t>(std::numeric_limits<T1>::max()) >= static_cast<size_t>(std::numeric_limits<T2>::max()); |
58 | |
59 | // clang-format off |
60 | test_conversion<idx_convertible && true, std::extents<T1>>(std::extents<T2>()); |
61 | test_conversion<idx_convertible && true, std::extents<T1, D>>(std::extents<T2, D>(5)); |
62 | test_conversion<idx_convertible && false, std::extents<T1, 5>>(std::extents<T2, D>(5)); |
63 | test_conversion<idx_convertible && true, std::extents<T1, 5>>(std::extents<T2, 5>()); |
64 | test_conversion<idx_convertible && false, std::extents<T1, 5, D>>(std::extents<T2, D, D>(5, 5)); |
65 | test_conversion<idx_convertible && true, std::extents<T1, D, D>>(std::extents<T2, D, D>(5, 5)); |
66 | test_conversion<idx_convertible && true, std::extents<T1, D, D>>(std::extents<T2, D, 7>(5)); |
67 | test_conversion<idx_convertible && true, std::extents<T1, 5, 7>>(std::extents<T2, 5, 7>()); |
68 | test_conversion<idx_convertible && false, std::extents<T1, 5, D, 8, D, D>>(std::extents<T2, D, D, 8, 9, 1>(5, 7)); |
69 | test_conversion<idx_convertible && true, std::extents<T1, D, D, D, D, D>>( |
70 | std::extents<T2, D, D, D, D, D>(5, 7, 8, 9, 1)); |
71 | test_conversion<idx_convertible && true, std::extents<T1, D, D, 8, 9, D>>(std::extents<T2, D, 7, 8, 9, 1>(5)); |
72 | test_conversion<idx_convertible && true, std::extents<T1, 5, 7, 8, 9, 1>>(std::extents<T2, 5, 7, 8, 9, 1>()); |
73 | // clang-format on |
74 | } |
75 | |
76 | constexpr void test_no_implicit_conversion() { |
77 | constexpr size_t D = std::dynamic_extent; |
78 | // Sanity check that one static to dynamic conversion works |
79 | static_assert(std::is_constructible_v<std::extents<int, D>, std::extents<int, 5>>, "" ); |
80 | static_assert(std::is_convertible_v<std::extents<int, 5>, std::extents<int, D>>, "" ); |
81 | |
82 | // Check that dynamic to static conversion only works explicitly only |
83 | static_assert(std::is_constructible_v<std::extents<int, 5>, std::extents<int, D>>, "" ); |
84 | static_assert(!std::is_convertible_v<std::extents<int, D>, std::extents<int, 5>>, "" ); |
85 | |
86 | // Sanity check that one static to dynamic conversion works |
87 | static_assert(std::is_constructible_v<std::extents<int, D, 7>, std::extents<int, 5, 7>>, "" ); |
88 | static_assert(std::is_convertible_v<std::extents<int, 5, 7>, std::extents<int, D, 7>>, "" ); |
89 | |
90 | // Check that dynamic to static conversion only works explicitly only |
91 | static_assert(std::is_constructible_v<std::extents<int, 5, 7>, std::extents<int, D, 7>>, "" ); |
92 | static_assert(!std::is_convertible_v<std::extents<int, D, 7>, std::extents<int, 5, 7>>, "" ); |
93 | |
94 | // Sanity check that smaller index_type to larger index_type conversion works |
95 | static_assert(std::is_constructible_v<std::extents<size_t, 5>, std::extents<int, 5>>, "" ); |
96 | static_assert(std::is_convertible_v<std::extents<int, 5>, std::extents<size_t, 5>>, "" ); |
97 | |
98 | // Check that larger index_type to smaller index_type conversion works explicitly only |
99 | static_assert(std::is_constructible_v<std::extents<int, 5>, std::extents<size_t, 5>>, "" ); |
100 | static_assert(!std::is_convertible_v<std::extents<size_t, 5>, std::extents<int, 5>>, "" ); |
101 | } |
102 | |
103 | constexpr void test_rank_mismatch() { |
104 | constexpr size_t D = std::dynamic_extent; |
105 | |
106 | static_assert(!std::is_constructible_v<std::extents<int, D>, std::extents<int>>, "" ); |
107 | static_assert(!std::is_constructible_v<std::extents<int>, std::extents<int, D, D>>, "" ); |
108 | static_assert(!std::is_constructible_v<std::extents<int, D>, std::extents<int, D, D>>, "" ); |
109 | static_assert(!std::is_constructible_v<std::extents<int, D, D, D>, std::extents<int, D, D>>, "" ); |
110 | } |
111 | |
112 | constexpr void test_static_extent_mismatch() { |
113 | constexpr size_t D = std::dynamic_extent; |
114 | |
115 | static_assert(!std::is_constructible_v<std::extents<int, D, 5>, std::extents<int, D, 4>>, "" ); |
116 | static_assert(!std::is_constructible_v<std::extents<int, 5>, std::extents<int, 4>>, "" ); |
117 | static_assert(!std::is_constructible_v<std::extents<int, 5, D>, std::extents<int, 4, D>>, "" ); |
118 | } |
119 | |
120 | constexpr bool test() { |
121 | test_conversion<int, int>(); |
122 | test_conversion<int, size_t>(); |
123 | test_conversion<size_t, int>(); |
124 | test_conversion<size_t, long>(); |
125 | test_no_implicit_conversion(); |
126 | test_rank_mismatch(); |
127 | test_static_extent_mismatch(); |
128 | return true; |
129 | } |
130 | |
131 | int main(int, char**) { |
132 | test(); |
133 | static_assert(test()); |
134 | |
135 | return 0; |
136 | } |
137 | |