1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
13 // template<class OtherExtents>
14 // constexpr explicit(!is_convertible_v<OtherExtents, extents_type>)
15 // mapping(const layout_left::mapping<OtherExtents>&) noexcept;
18 // - extents_type::rank() <= 1 is true, and
19 // - is_constructible_v<extents_type, OtherExtents> is true.
21 // Preconditions: other.required_span_size() is representable as a value of type index_type
27 #include <span> // dynamic_extent
28 #include <type_traits>
30 #include "test_macros.h"
32 template <class To
, class From
>
33 constexpr void test_implicit_conversion(To dest
, From src
) {
34 assert(dest
.extents() == src
.extents());
37 template <bool implicit
, class ToE
, class FromE
>
38 constexpr void test_conversion(FromE src_exts
) {
39 using To
= std::layout_right::mapping
<ToE
>;
40 using From
= std::layout_left::mapping
<FromE
>;
43 ASSERT_NOEXCEPT(To(src
));
46 assert(dest
.extents() == src
.extents());
47 if constexpr (implicit
) {
49 assert(dest
.extents() == src
.extents());
50 test_implicit_conversion
<To
, From
>(src
, src
);
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());
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>());
68 template <class IdxT
, size_t... Extents
>
69 using lr_mapping_t
= std::layout_right::mapping
<std::extents
<IdxT
, Extents
...>>;
70 template <class IdxT
, size_t... Extents
>
71 using ll_mapping_t
= std::layout_left::mapping
<std::extents
<IdxT
, Extents
...>>;
73 constexpr void test_no_implicit_conversion() {
74 constexpr size_t D
= std::dynamic_extent
;
76 // Sanity check that one static to dynamic conversion works
77 static_assert(std::is_constructible_v
<lr_mapping_t
<int, D
>, ll_mapping_t
<int, 5>>);
78 static_assert(std::is_convertible_v
<ll_mapping_t
<int, 5>, lr_mapping_t
<int, D
>>);
80 // Check that dynamic to static conversion only works explicitly
81 static_assert(std::is_constructible_v
<lr_mapping_t
<int, 5>, ll_mapping_t
<int, D
>>);
82 static_assert(!std::is_convertible_v
<ll_mapping_t
<int, D
>, lr_mapping_t
<int, 5>>);
84 // Sanity check that smaller index_type to larger index_type conversion works
85 static_assert(std::is_constructible_v
<lr_mapping_t
<size_t, 5>, ll_mapping_t
<int, 5>>);
86 static_assert(std::is_convertible_v
<ll_mapping_t
<int, 5>, lr_mapping_t
<size_t, 5>>);
88 // Check that larger index_type to smaller index_type conversion works explicitly only
89 static_assert(std::is_constructible_v
<lr_mapping_t
<int, 5>, ll_mapping_t
<size_t, 5>>);
90 static_assert(!std::is_convertible_v
<ll_mapping_t
<size_t, 5>, lr_mapping_t
<int, 5>>);
93 constexpr void test_rank_mismatch() {
94 constexpr size_t D
= std::dynamic_extent
;
96 static_assert(!std::is_constructible_v
<lr_mapping_t
<int, D
>, ll_mapping_t
<int>>);
97 static_assert(!std::is_constructible_v
<lr_mapping_t
<int>, ll_mapping_t
<int, D
, D
>>);
98 static_assert(!std::is_constructible_v
<lr_mapping_t
<int, D
>, ll_mapping_t
<int, D
, D
>>);
99 static_assert(!std::is_constructible_v
<lr_mapping_t
<int, D
, D
, D
>, ll_mapping_t
<int, D
, D
>>);
102 constexpr void test_static_extent_mismatch() {
103 static_assert(!std::is_constructible_v
<lr_mapping_t
<int, 5>, ll_mapping_t
<int, 4>>);
106 constexpr void test_rank_greater_one() {
107 constexpr size_t D
= std::dynamic_extent
;
109 static_assert(!std::is_constructible_v
<lr_mapping_t
<int, D
, D
>, ll_mapping_t
<int, D
, D
>>);
110 static_assert(!std::is_constructible_v
<lr_mapping_t
<int, 1, 1>, ll_mapping_t
<int, 1, 1>>);
111 static_assert(!std::is_constructible_v
<lr_mapping_t
<int, D
, D
, D
>, ll_mapping_t
<int, D
, D
, D
>>);
114 constexpr bool test() {
115 test_conversion
<int, int>();
116 test_conversion
<int, size_t>();
117 test_conversion
<size_t, int>();
118 test_conversion
<size_t, long>();
119 test_no_implicit_conversion();
120 test_rank_mismatch();
121 test_static_extent_mismatch();
122 test_rank_greater_one();
126 int main(int, char**) {
128 static_assert(test());