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 // constexpr span(pointer first, pointer last);
14 // Requires: [first, last) shall be a valid range.
15 // If extent is not equal to dynamic_extent, then last - first shall be equal to extent.
16 //
17
18 #include <span>
19 #include <cassert>
20 #include <string>
21
22 #include "test_macros.h"
23
24
25 int arr[] = {1,2,3};
26 const int carr[] = {4,5,6};
27 volatile int varr[] = {7,8,9};
28 const volatile int cvarr[] = {1,3,5};
29
30 template<class T, size_t extent>
createImplicitSpan(T * first,T * last)31 std::span<T, extent> createImplicitSpan(T* first, T* last) {
32 return {first, last}; // expected-error {{chosen constructor is explicit in copy-initialization}}
33 }
34
main(int,char **)35 int main(int, char**)
36 {
37 // We can't check that the size doesn't match - because that's a runtime property
38 // std::span<int, 2> s1(arr, arr + 3);
39
40 // Type wrong
41 {
42 std::span<float> s1(arr, arr + 3); // expected-error {{no matching constructor for initialization of 'std::span<float>'}}
43 std::span<float, 3> s2(arr, arr + 3); // expected-error {{no matching constructor for initialization of 'std::span<float, 3>'}}
44 }
45
46 // CV wrong (dynamically sized)
47 {
48 std::span< int> s1{ carr, carr + 3}; // expected-error {{no matching constructor for initialization of 'std::span<int>'}}
49 std::span< int> s2{ varr, varr + 3}; // expected-error {{no matching constructor for initialization of 'std::span<int>'}}
50 std::span< int> s3{cvarr, cvarr + 3}; // expected-error {{no matching constructor for initialization of 'std::span<int>'}}
51 std::span<const int> s4{ varr, varr + 3}; // expected-error {{no matching constructor for initialization of 'std::span<const int>'}}
52 std::span<const int> s5{cvarr, cvarr + 3}; // expected-error {{no matching constructor for initialization of 'std::span<const int>'}}
53 std::span< volatile int> s6{ carr, carr + 3}; // expected-error {{no matching constructor for initialization of 'std::span<volatile int>'}}
54 std::span< volatile int> s7{cvarr, cvarr + 3}; // expected-error {{no matching constructor for initialization of 'std::span<volatile int>'}}
55 }
56
57 // CV wrong (statically sized)
58 {
59 std::span< int,3> s1{ carr, carr + 3}; // expected-error {{no matching constructor for initialization of 'std::span<int, 3>'}}
60 std::span< int,3> s2{ varr, varr + 3}; // expected-error {{no matching constructor for initialization of 'std::span<int, 3>'}}
61 std::span< int,3> s3{cvarr, cvarr + 3}; // expected-error {{no matching constructor for initialization of 'std::span<int, 3>'}}
62 std::span<const int,3> s4{ varr, varr + 3}; // expected-error {{no matching constructor for initialization of 'std::span<const int, 3>'}}
63 std::span<const int,3> s5{cvarr, cvarr + 3}; // expected-error {{no matching constructor for initialization of 'std::span<const int, 3>'}}
64 std::span< volatile int,3> s6{ carr, carr + 3}; // expected-error {{no matching constructor for initialization of 'std::span<volatile int, 3>'}}
65 std::span< volatile int,3> s7{cvarr, cvarr + 3}; // expected-error {{no matching constructor for initialization of 'std::span<volatile int, 3>'}}
66 }
67
68 // explicit constructor necessary
69 {
70 createImplicitSpan<int, 1>(arr, arr + 1);
71 }
72
73 return 0;
74 }
75