1 // -*- C++ -*-
2 //===------------------------------ span ---------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===---------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 
11 // <span>
12 
13 // template<size_t N>
14 //     constexpr span(array<value_type, N>& arr) noexcept;
15 // template<size_t N>
16 //     constexpr span(const array<value_type, N>& arr) noexcept;
17 //
18 // Remarks: These constructors shall not participate in overload resolution unless:
19 //   — extent == dynamic_extent || N == extent is true, and
20 //   — remove_pointer_t<decltype(data(arr))>(*)[] is convertible to ElementType(*)[].
21 //
22 
23 
24 #include <span>
25 #include <array>
26 #include <cassert>
27 #include <string>
28 
29 #include "test_macros.h"
30 // std::array is explicitly allowed to be initialized with A a = { init-list };.
31 // Disable the missing braces warning for this reason.
32 #include "disable_missing_braces_warning.h"
33 
34 
checkCV()35 void checkCV()
36 {
37     std::array<int, 3> arr  = {1,2,3};
38 //  STL says these are not cromulent
39 //  std::array<const int,3> carr = {4,5,6};
40 //  std::array<volatile int, 3> varr = {7,8,9};
41 //  std::array<const volatile int, 3> cvarr = {1,3,5};
42 
43 //  Types the same (dynamic sized)
44     {
45     std::span<               int> s1{  arr};    // a span<               int> pointing at int.
46     }
47 
48 //  Types the same (static sized)
49     {
50     std::span<               int,3> s1{  arr};  // a span<               int> pointing at int.
51     }
52 
53 
54 //  types different (dynamic sized)
55     {
56     std::span<const          int> s1{ arr};     // a span<const          int> pointing at int.
57     std::span<      volatile int> s2{ arr};     // a span<      volatile int> pointing at int.
58     std::span<      volatile int> s3{ arr};     // a span<      volatile int> pointing at const int.
59     std::span<const volatile int> s4{ arr};     // a span<const volatile int> pointing at int.
60     }
61 
62 //  types different (static sized)
63     {
64     std::span<const          int,3> s1{ arr};   // a span<const          int> pointing at int.
65     std::span<      volatile int,3> s2{ arr};   // a span<      volatile int> pointing at int.
66     std::span<      volatile int,3> s3{ arr};   // a span<      volatile int> pointing at const int.
67     std::span<const volatile int,3> s4{ arr};   // a span<const volatile int> pointing at int.
68     }
69 }
70 
71 template <typename T, typename U>
testConstructorArray()72 constexpr bool testConstructorArray() {
73   std::array<U, 2> val = {U(), U()};
74   ASSERT_NOEXCEPT(std::span<T>{val});
75   ASSERT_NOEXCEPT(std::span<T, 2>{val});
76   std::span<T> s1{val};
77   std::span<T, 2> s2{val};
78   return s1.data() == &val[0] && s1.size() == 2 && s2.data() == &val[0] &&
79          s2.size() == 2;
80 }
81 
82 template <typename T, typename U>
testConstructorConstArray()83 constexpr bool testConstructorConstArray() {
84   const std::array<U, 2> val = {U(), U()};
85   ASSERT_NOEXCEPT(std::span<const T>{val});
86   ASSERT_NOEXCEPT(std::span<const T, 2>{val});
87   std::span<const T> s1{val};
88   std::span<const T, 2> s2{val};
89   return s1.data() == &val[0] && s1.size() == 2 && s2.data() == &val[0] &&
90          s2.size() == 2;
91 }
92 
93 template <typename T>
testConstructors()94 constexpr bool testConstructors() {
95   static_assert(testConstructorArray<T, T>(), "");
96   static_assert(testConstructorArray<const T, const T>(), "");
97   static_assert(testConstructorArray<const T, T>(), "");
98   static_assert(testConstructorConstArray<T, T>(), "");
99   static_assert(testConstructorConstArray<const T, const T>(), "");
100   static_assert(testConstructorConstArray<const T, T>(), "");
101 
102   return testConstructorArray<T, T>() &&
103          testConstructorArray<const T, const T>() &&
104          testConstructorArray<const T, T>() &&
105          testConstructorConstArray<T, T>() &&
106          testConstructorConstArray<const T, const T>() &&
107          testConstructorConstArray<const T, T>();
108 }
109 
110 struct A{};
111 
main(int,char **)112 int main(int, char**)
113 {
114     assert(testConstructors<int>());
115     assert(testConstructors<long>());
116     assert(testConstructors<double>());
117     assert(testConstructors<A>());
118 
119     assert(testConstructors<int*>());
120     assert(testConstructors<const int*>());
121 
122     checkCV();
123 
124     return 0;
125 }
126