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
9
10// <span>
11
12// template<class It, class EndOrSize>
13// span(It, EndOrSize) -> span<remove_reference_t<iter_reference_t<_It>>>;
14//
15// template<class T, size_t N>
16// span(T (&)[N]) -> span<T, N>;
17//
18// template<class T, size_t N>
19// span(array<T, N>&) -> span<T, N>;
20//
21// template<class T, size_t N>
22// span(const array<T, N>&) -> span<const T, N>;
23//
24// template<class R>
25// span(R&&) -> span<remove_reference_t<ranges::range_reference_t<R>>>;
26
27
28#include <span>
29#include <array>
30#include <cassert>
31#include <iterator>
32#include <memory>
33#include <string>
34#include <type_traits>
35
36#include "test_macros.h"
37
38void test_iterator_sentinel() {
39 int arr[] = {1, 2, 3};
40 {
41 std::span s{std::begin(arr), std::end(arr)};
42 ASSERT_SAME_TYPE(decltype(s), std::span<int>);
43 assert(s.size() == std::size(arr));
44 assert(s.data() == std::data(arr));
45 }
46 {
47 std::span s{std::begin(arr), 3};
48 ASSERT_SAME_TYPE(decltype(s), std::span<int>);
49 assert(s.size() == std::size(arr));
50 assert(s.data() == std::data(arr));
51 }
52
53#if _LIBCPP_STD_VER >= 26
54 // P3029R1: deduction from `integral_constant`
55 {
56 std::span s{std::begin(arr), std::integral_constant<size_t, 3>{}};
57 ASSERT_SAME_TYPE(decltype(s), std::span<int, 3>);
58 assert(s.size() == std::size(arr));
59 assert(s.data() == std::data(arr));
60 }
61#endif
62}
63
64void test_c_array() {
65 {
66 int arr[] = {1, 2, 3};
67 std::span s{arr};
68 ASSERT_SAME_TYPE(decltype(s), std::span<int, 3>);
69 assert(s.size() == std::size(arr));
70 assert(s.data() == std::data(arr));
71 }
72
73 {
74 const int arr[] = {1,2,3};
75 std::span s{arr};
76 ASSERT_SAME_TYPE(decltype(s), std::span<const int, 3>);
77 assert(s.size() == std::size(arr));
78 assert(s.data() == std::data(arr));
79 }
80}
81
82void test_std_array() {
83 {
84 std::array<double, 4> arr = {1.0, 2.0, 3.0, 4.0};
85 std::span s{arr};
86 ASSERT_SAME_TYPE(decltype(s), std::span<double, 4>);
87 assert(s.size() == arr.size());
88 assert(s.data() == arr.data());
89 }
90
91 {
92 const std::array<long, 5> arr = {4, 5, 6, 7, 8};
93 std::span s{arr};
94 ASSERT_SAME_TYPE(decltype(s), std::span<const long, 5>);
95 assert(s.size() == arr.size());
96 assert(s.data() == arr.data());
97 }
98}
99
100void test_range_std_container() {
101 {
102 std::string str{"ABCDE"};
103 std::span s{str};
104 ASSERT_SAME_TYPE(decltype(s), std::span<char>);
105 assert(s.size() == str.size());
106 assert(s.data() == str.data());
107 }
108
109 {
110 const std::string str{"QWERTYUIOP"};
111 std::span s{str};
112 ASSERT_SAME_TYPE(decltype(s), std::span<const char>);
113 assert(s.size() == str.size());
114 assert(s.data() == str.data());
115 }
116}
117
118int main(int, char**)
119{
120 test_iterator_sentinel();
121 test_c_array();
122 test_std_array();
123 test_range_std_container();
124
125 return 0;
126}
127

source code of libcxx/test/std/containers/views/views.span/span.cons/deduct.pass.cpp