xref: /llvm-project/libcxx/test/std/containers/views/mdspan/extents/ctad.pass.cpp (revision e99c4906e44ae3f921fa05356909d006cda8d954)
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... Integrals>
13 // explicit extents(Integrals...) -> see below;
14 //   Constraints: (is_convertible_v<Integrals, size_t> && ...) is true.
15 //
16 // Remarks: The deduced type is dextents<size_t, sizeof...(Integrals)>.           // until C++26
17 // Remarks: The deduced type is extents<size_t, maybe-static-ext<Integrals>...>.  // since C++26
18 
19 #include <cassert>
20 #include <cstddef>
21 #include <mdspan>
22 #include <span> // dynamic_extent
23 #include <type_traits>
24 
25 #include "../ConvertibleToIntegral.h"
26 #include "test_macros.h"
27 
28 struct NoDefaultCtorIndex {
29   size_t value;
30   constexpr NoDefaultCtorIndex() = delete;
31   constexpr NoDefaultCtorIndex(size_t val) : value(val) {}
32   constexpr operator size_t() const noexcept { return value; }
33 };
34 
35 template <class E, class Expected>
36 constexpr void test(E e, Expected expected) {
37   ASSERT_SAME_TYPE(E, Expected);
38   assert(e == expected);
39 }
40 
41 constexpr bool test() {
42   constexpr std::size_t D = std::dynamic_extent;
43 
44   test(std::extents(), std::extents<size_t>());
45   test(std::extents(1), std::extents<std::size_t, D>(1));
46   test(std::extents(1, 2u), std::extents<std::size_t, D, D>(1, 2u));
47   test(std::extents(1, 2u, 3, 4, 5, 6, 7, 8, 9),
48        std::extents<std::size_t, D, D, D, D, D, D, D, D, D>(1, 2u, 3, 4, 5, 6, 7, 8, 9));
49   test(std::extents(NoDefaultCtorIndex{1}, NoDefaultCtorIndex{2}), std::extents<std::size_t, D, D>(1, 2));
50 
51 #if _LIBCPP_STD_VER >= 26
52   // P3029R1: deduction from `integral_constant`
53   test(std::extents(std::integral_constant<size_t, 5>{}), std::extents<std::size_t, 5>());
54   test(std::extents(std::integral_constant<size_t, 5>{}, 6), std::extents<std::size_t, 5, std::dynamic_extent>(6));
55   test(std::extents(std::integral_constant<size_t, 5>{}, 6, std::integral_constant<size_t, 7>{}),
56        std::extents<std::size_t, 5, std::dynamic_extent, 7>(6));
57 #endif
58 
59   return true;
60 }
61 
62 int main(int, char**) {
63   test();
64   static_assert(test());
65 
66   return 0;
67 }
68