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
24 #include <type_traits>
28 #include "test_macros.h"
30 template <class To
, class From
>
31 constexpr void test_implicit_conversion(To dest
, From src
) {
32 assert(dest
.extents() == src
.extents());
35 template <bool implicit
, class ToE
, class FromE
>
36 constexpr void test_conversion(FromE src_exts
) {
37 using To
= std::layout_right::mapping
<ToE
>;
38 using From
= std::layout_left::mapping
<FromE
>;
41 ASSERT_NOEXCEPT(To(src
));
44 assert(dest
.extents() == src
.extents());
45 if constexpr (implicit
) {
47 assert(dest
.extents() == src
.extents());
48 test_implicit_conversion
<To
, From
>(src
, src
);
52 template <class T1
, class T2
>
53 constexpr void test_conversion() {
54 constexpr size_t D
= std::dynamic_extent
;
55 constexpr bool idx_convertible
=
56 static_cast<size_t>(std::numeric_limits
<T1
>::max()) >= static_cast<size_t>(std::numeric_limits
<T2
>::max());
59 test_conversion
<idx_convertible
&& true, std::extents
<T1
>>(std::extents
<T2
>());
60 test_conversion
<idx_convertible
&& true, std::extents
<T1
, D
>>(std::extents
<T2
, D
>(5));
61 test_conversion
<idx_convertible
&& false, std::extents
<T1
, 5>>(std::extents
<T2
, D
>(5));
62 test_conversion
<idx_convertible
&& true, std::extents
<T1
, 5>>(std::extents
<T2
, 5>());
66 template <class IdxT
, size_t... Extents
>
67 using lr_mapping_t
= std::layout_right::mapping
<std::extents
<IdxT
, Extents
...>>;
68 template <class IdxT
, size_t... Extents
>
69 using ll_mapping_t
= std::layout_left::mapping
<std::extents
<IdxT
, Extents
...>>;
71 constexpr void test_no_implicit_conversion() {
72 constexpr size_t D
= std::dynamic_extent
;
74 // Sanity check that one static to dynamic conversion works
75 static_assert(std::is_constructible_v
<lr_mapping_t
<int, D
>, ll_mapping_t
<int, 5>>);
76 static_assert(std::is_convertible_v
<ll_mapping_t
<int, 5>, lr_mapping_t
<int, D
>>);
78 // Check that dynamic to static conversion only works explicitly
79 static_assert(std::is_constructible_v
<lr_mapping_t
<int, 5>, ll_mapping_t
<int, D
>>);
80 static_assert(!std::is_convertible_v
<ll_mapping_t
<int, D
>, lr_mapping_t
<int, 5>>);
82 // Sanity check that smaller index_type to larger index_type conversion works
83 static_assert(std::is_constructible_v
<lr_mapping_t
<size_t, 5>, ll_mapping_t
<int, 5>>);
84 static_assert(std::is_convertible_v
<ll_mapping_t
<int, 5>, lr_mapping_t
<size_t, 5>>);
86 // Check that larger index_type to smaller index_type conversion works explicitly only
87 static_assert(std::is_constructible_v
<lr_mapping_t
<int, 5>, ll_mapping_t
<size_t, 5>>);
88 static_assert(!std::is_convertible_v
<ll_mapping_t
<size_t, 5>, lr_mapping_t
<int, 5>>);
91 constexpr void test_rank_mismatch() {
92 constexpr size_t D
= std::dynamic_extent
;
94 static_assert(!std::is_constructible_v
<lr_mapping_t
<int, D
>, ll_mapping_t
<int>>);
95 static_assert(!std::is_constructible_v
<lr_mapping_t
<int>, ll_mapping_t
<int, D
, D
>>);
96 static_assert(!std::is_constructible_v
<lr_mapping_t
<int, D
>, ll_mapping_t
<int, D
, D
>>);
97 static_assert(!std::is_constructible_v
<lr_mapping_t
<int, D
, D
, D
>, ll_mapping_t
<int, D
, D
>>);
100 constexpr void test_static_extent_mismatch() {
101 static_assert(!std::is_constructible_v
<lr_mapping_t
<int, 5>, ll_mapping_t
<int, 4>>);
104 constexpr void test_rank_greater_one() {
105 constexpr size_t D
= std::dynamic_extent
;
107 static_assert(!std::is_constructible_v
<lr_mapping_t
<int, D
, D
>, ll_mapping_t
<int, D
, D
>>);
108 static_assert(!std::is_constructible_v
<lr_mapping_t
<int, 1, 1>, ll_mapping_t
<int, 1, 1>>);
109 static_assert(!std::is_constructible_v
<lr_mapping_t
<int, D
, D
, D
>, ll_mapping_t
<int, D
, D
, D
>>);
112 constexpr bool test() {
113 test_conversion
<int, int>();
114 test_conversion
<int, size_t>();
115 test_conversion
<size_t, int>();
116 test_conversion
<size_t, long>();
117 test_no_implicit_conversion();
118 test_rank_mismatch();
119 test_static_extent_mismatch();
120 test_rank_greater_one();
124 int main(int, char**) {
126 static_assert(test());