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 End>
13 // constexpr explicit(Extent != dynamic_extent) span(It first, End 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
21 #include "test_iterators.h"
22
23 template <class T, class Sentinel>
test_ctor()24 constexpr bool test_ctor() {
25 T val[2] = {};
26 auto s1 = std::span<T>(std::begin(val), Sentinel(std::end(val)));
27 auto s2 = std::span<T, 2>(std::begin(val), Sentinel(std::end(val)));
28 assert(s1.data() == std::data(val) && s1.size() == std::size(val));
29 assert(s2.data() == std::data(val) && s2.size() == std::size(val));
30 return true;
31 }
32
33 template <size_t Extent>
test_constructibility()34 constexpr void test_constructibility() {
35 static_assert(std::is_constructible_v<std::span<int, Extent>, int*, int*>);
36 static_assert(!std::is_constructible_v<std::span<int, Extent>, const int*, const int*>);
37 static_assert(!std::is_constructible_v<std::span<int, Extent>, volatile int*, volatile int*>);
38 static_assert(std::is_constructible_v<std::span<const int, Extent>, int*, int*>);
39 static_assert(std::is_constructible_v<std::span<const int, Extent>, const int*, const int*>);
40 static_assert(!std::is_constructible_v<std::span<const int, Extent>, volatile int*, volatile int*>);
41 static_assert(std::is_constructible_v<std::span<volatile int, Extent>, int*, int*>);
42 static_assert(!std::is_constructible_v<std::span<volatile int, Extent>, const int*, const int*>);
43 static_assert(std::is_constructible_v<std::span<volatile int, Extent>, volatile int*, volatile int*>);
44 static_assert(!std::is_constructible_v<std::span<int, Extent>, int*, float*>); // types wrong
45 }
46
test()47 constexpr bool test() {
48 test_constructibility<std::dynamic_extent>();
49 test_constructibility<3>();
50 struct A {};
51 assert((test_ctor<int, int*>()));
52 assert((test_ctor<int, sized_sentinel<int*>>()));
53 assert((test_ctor<A, A*>()));
54 assert((test_ctor<A, sized_sentinel<A*>>()));
55 return true;
56 }
57
main(int,char **)58 int main(int, char**) {
59 test();
60 static_assert(test());
61
62 return 0;
63 }
64