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 <span> // dynamic_extent 36 #include <type_traits> 37 38 template <class To, class From> 39 constexpr void test_implicit_conversion(To dest, From src) { 40 assert(dest == src); 41 } 42 43 template <bool implicit, class To, class From> 44 constexpr void test_conversion(From src) { 45 To dest(src); 46 assert(dest == src); 47 if constexpr (implicit) { 48 dest = src; 49 assert(dest == src); 50 test_implicit_conversion<To, From>(src, src); 51 } 52 } 53 54 template <class T1, class T2> 55 constexpr void test_conversion() { 56 constexpr size_t D = std::dynamic_extent; 57 constexpr bool idx_convertible = 58 static_cast<size_t>(std::numeric_limits<T1>::max()) >= static_cast<size_t>(std::numeric_limits<T2>::max()); 59 60 // clang-format off 61 test_conversion<idx_convertible && true, std::extents<T1>>(std::extents<T2>()); 62 test_conversion<idx_convertible && true, std::extents<T1, D>>(std::extents<T2, D>(5)); 63 test_conversion<idx_convertible && false, std::extents<T1, 5>>(std::extents<T2, D>(5)); 64 test_conversion<idx_convertible && true, std::extents<T1, 5>>(std::extents<T2, 5>()); 65 test_conversion<idx_convertible && false, std::extents<T1, 5, D>>(std::extents<T2, D, D>(5, 5)); 66 test_conversion<idx_convertible && true, std::extents<T1, D, D>>(std::extents<T2, D, D>(5, 5)); 67 test_conversion<idx_convertible && true, std::extents<T1, D, D>>(std::extents<T2, D, 7>(5)); 68 test_conversion<idx_convertible && true, std::extents<T1, 5, 7>>(std::extents<T2, 5, 7>()); 69 test_conversion<idx_convertible && false, std::extents<T1, 5, D, 8, D, D>>(std::extents<T2, D, D, 8, 9, 1>(5, 7)); 70 test_conversion<idx_convertible && true, std::extents<T1, D, D, D, D, D>>( 71 std::extents<T2, D, D, D, D, D>(5, 7, 8, 9, 1)); 72 test_conversion<idx_convertible && true, std::extents<T1, D, D, 8, 9, D>>(std::extents<T2, D, 7, 8, 9, 1>(5)); 73 test_conversion<idx_convertible && true, std::extents<T1, 5, 7, 8, 9, 1>>(std::extents<T2, 5, 7, 8, 9, 1>()); 74 // clang-format on 75 } 76 77 constexpr void test_no_implicit_conversion() { 78 constexpr size_t D = std::dynamic_extent; 79 // Sanity check that one static to dynamic conversion works 80 static_assert(std::is_constructible_v<std::extents<int, D>, std::extents<int, 5>>, ""); 81 static_assert(std::is_convertible_v<std::extents<int, 5>, std::extents<int, D>>, ""); 82 83 // Check that dynamic to static conversion only works explicitly only 84 static_assert(std::is_constructible_v<std::extents<int, 5>, std::extents<int, D>>, ""); 85 static_assert(!std::is_convertible_v<std::extents<int, D>, std::extents<int, 5>>, ""); 86 87 // Sanity check that one static to dynamic conversion works 88 static_assert(std::is_constructible_v<std::extents<int, D, 7>, std::extents<int, 5, 7>>, ""); 89 static_assert(std::is_convertible_v<std::extents<int, 5, 7>, std::extents<int, D, 7>>, ""); 90 91 // Check that dynamic to static conversion only works explicitly only 92 static_assert(std::is_constructible_v<std::extents<int, 5, 7>, std::extents<int, D, 7>>, ""); 93 static_assert(!std::is_convertible_v<std::extents<int, D, 7>, std::extents<int, 5, 7>>, ""); 94 95 // Sanity check that smaller index_type to larger index_type conversion works 96 static_assert(std::is_constructible_v<std::extents<size_t, 5>, std::extents<int, 5>>, ""); 97 static_assert(std::is_convertible_v<std::extents<int, 5>, std::extents<size_t, 5>>, ""); 98 99 // Check that larger index_type to smaller index_type conversion works explicitly only 100 static_assert(std::is_constructible_v<std::extents<int, 5>, std::extents<size_t, 5>>, ""); 101 static_assert(!std::is_convertible_v<std::extents<size_t, 5>, std::extents<int, 5>>, ""); 102 } 103 104 constexpr void test_rank_mismatch() { 105 constexpr size_t D = std::dynamic_extent; 106 107 static_assert(!std::is_constructible_v<std::extents<int, D>, std::extents<int>>, ""); 108 static_assert(!std::is_constructible_v<std::extents<int>, std::extents<int, D, D>>, ""); 109 static_assert(!std::is_constructible_v<std::extents<int, D>, std::extents<int, D, D>>, ""); 110 static_assert(!std::is_constructible_v<std::extents<int, D, D, D>, std::extents<int, D, D>>, ""); 111 } 112 113 constexpr void test_static_extent_mismatch() { 114 constexpr size_t D = std::dynamic_extent; 115 116 static_assert(!std::is_constructible_v<std::extents<int, D, 5>, std::extents<int, D, 4>>, ""); 117 static_assert(!std::is_constructible_v<std::extents<int, 5>, std::extents<int, 4>>, ""); 118 static_assert(!std::is_constructible_v<std::extents<int, 5, D>, std::extents<int, 4, D>>, ""); 119 } 120 121 constexpr bool test() { 122 test_conversion<int, int>(); 123 test_conversion<int, size_t>(); 124 test_conversion<size_t, int>(); 125 test_conversion<size_t, long>(); 126 test_no_implicit_conversion(); 127 test_rank_mismatch(); 128 test_static_extent_mismatch(); 129 return true; 130 } 131 132 int main(int, char**) { 133 test(); 134 static_assert(test()); 135 136 return 0; 137 } 138