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<size_t N>
13// constexpr span(array<value_type, N>& arr) noexcept;
14// template<size_t N>
15// constexpr span(const array<value_type, N>& arr) noexcept;
16//
17// Remarks: These constructors shall not participate in overload resolution unless:
18// - extent == dynamic_extent || N == extent is true, and
19// - remove_pointer_t<decltype(data(arr))>(*)[] is convertible to ElementType(*)[].
20//
21
22#include <span>
23#include <array>
24#include <cassert>
25#include <string>
26
27#include "test_macros.h"
28
29void checkCV() {
30 std::array<int, 3> arr = {1, 2, 3};
31 // STL says these are not cromulent
32 // std::array<const int,3> carr = {4,5,6};
33 // std::array<volatile int, 3> varr = {7,8,9};
34 // std::array<const volatile int, 3> cvarr = {1,3,5};
35
36 // Types the same (dynamic sized)
37 {
38 std::span< int> s1{arr}; // a span< int> pointing at int.
39 }
40
41 // Types the same (static sized)
42 {
43 std::span< int, 3> s1{arr}; // a span< int> pointing at int.
44 }
45
46 // types different (dynamic sized)
47 {
48 std::span<const int> s1{arr}; // a span<const int> pointing at int.
49 std::span< volatile int> s2{arr}; // a span< volatile int> pointing at int.
50 std::span< volatile int> s3{arr}; // a span< volatile int> pointing at const int.
51 std::span<const volatile int> s4{arr}; // a span<const volatile int> pointing at int.
52 }
53
54 // types different (static sized)
55 {
56 std::span<const int, 3> s1{arr}; // a span<const int> pointing at int.
57 std::span< volatile int, 3> s2{arr}; // a span< volatile int> pointing at int.
58 std::span< volatile int, 3> s3{arr}; // a span< volatile int> pointing at const int.
59 std::span<const volatile int, 3> s4{arr}; // a span<const volatile int> pointing at int.
60 }
61}
62
63template <typename T, typename U>
64constexpr bool testConstructorArray() {
65 std::array<U, 2> val = {U(), U()};
66 ASSERT_NOEXCEPT(std::span<T>{val});
67 ASSERT_NOEXCEPT(std::span<T, 2>{val});
68 std::span<T> s1{val};
69 std::span<T, 2> s2{val};
70 return s1.data() == &val[0] && s1.size() == 2 && s2.data() == &val[0] && s2.size() == 2;
71}
72
73template <typename T, typename U>
74constexpr bool testConstructorConstArray() {
75 const std::array<U, 2> val = {U(), U()};
76 ASSERT_NOEXCEPT(std::span<const T>{val});
77 ASSERT_NOEXCEPT(std::span<const T, 2>{val});
78 std::span<const T> s1{val};
79 std::span<const T, 2> s2{val};
80 return s1.data() == &val[0] && s1.size() == 2 && s2.data() == &val[0] && s2.size() == 2;
81}
82
83template <typename T>
84constexpr bool testConstructors() {
85 static_assert(testConstructorArray<T, T>(), "");
86 static_assert(testConstructorArray<const T, const T>(), "");
87 static_assert(testConstructorArray<const T, T>(), "");
88 static_assert(testConstructorConstArray<T, T>(), "");
89 static_assert(testConstructorConstArray<const T, const T>(), "");
90 static_assert(testConstructorConstArray<const T, T>(), "");
91
92 return testConstructorArray<T, T>() && testConstructorArray<const T, const T>() &&
93 testConstructorArray<const T, T>() && testConstructorConstArray<T, T>() &&
94 testConstructorConstArray<const T, const T>() && testConstructorConstArray<const T, T>();
95}
96
97struct A {};
98
99int main(int, char**) {
100 assert(testConstructors<int>());
101 assert(testConstructors<long>());
102 assert(testConstructors<double>());
103 assert(testConstructors<A>());
104
105 assert(testConstructors<int*>());
106 assert(testConstructors<const int*>());
107
108 checkCV();
109
110 return 0;
111}
112

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