blob: 4ec80efc808075dd4b695d62dbc7ba5c288e4825 [file] [log] [blame]
Marshall Clow8a0794b2018-07-24 03:01:02 +00001// -*- C++ -*-
2//===------------------------------ span ---------------------------------===//
3//
Chandler Carruth57b08b02019-01-19 10:56:40 +00004// 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
Marshall Clow8a0794b2018-07-24 03:01:02 +00007//
8//===---------------------------------------------------------------------===//
Stephan T. Lavavejdec89052018-11-14 03:06:06 +00009// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
Marshall Clow8a0794b2018-07-24 03:01:02 +000010
11// <span>
12
Marshall Clow8eda3ad2019-03-06 03:59:44 +000013// template<class OtherElementType, size_t OtherExtent>
Marshall Clow8a0794b2018-07-24 03:01:02 +000014// constexpr span(const span<OtherElementType, OtherExtent>& s) noexcept;
15//
16// Remarks: This constructor shall not participate in overload resolution unless:
17// Extent == dynamic_extent || Extent == OtherExtent is true, and
18// OtherElementType(*)[] is convertible to ElementType(*)[].
19
20
21#include <span>
22#include <cassert>
23#include <string>
24
25#include "test_macros.h"
26
27void checkCV()
28{
29 std::span< int> sp;
30// std::span<const int> csp;
31 std::span< volatile int> vsp;
32// std::span<const volatile int> cvsp;
33
34 std::span< int, 0> sp0;
35// std::span<const int, 0> csp0;
36 std::span< volatile int, 0> vsp0;
37// std::span<const volatile int, 0> cvsp0;
38
39// dynamic -> dynamic
40 {
41 std::span<const int> s1{ sp}; // a span<const int> pointing at int.
42 std::span< volatile int> s2{ sp}; // a span< volatile int> pointing at int.
43 std::span<const volatile int> s3{ sp}; // a span<const volatile int> pointing at int.
44 std::span<const volatile int> s4{ vsp}; // a span<const volatile int> pointing at volatile int.
45 assert(s1.size() + s2.size() + s3.size() + s4.size() == 0);
46 }
47
48// static -> static
49 {
50 std::span<const int, 0> s1{ sp0}; // a span<const int> pointing at int.
51 std::span< volatile int, 0> s2{ sp0}; // a span< volatile int> pointing at int.
52 std::span<const volatile int, 0> s3{ sp0}; // a span<const volatile int> pointing at int.
53 std::span<const volatile int, 0> s4{ vsp0}; // a span<const volatile int> pointing at volatile int.
54 assert(s1.size() + s2.size() + s3.size() + s4.size() == 0);
55 }
56
57// static -> dynamic
58 {
59 std::span<const int> s1{ sp0}; // a span<const int> pointing at int.
60 std::span< volatile int> s2{ sp0}; // a span< volatile int> pointing at int.
61 std::span<const volatile int> s3{ sp0}; // a span<const volatile int> pointing at int.
62 std::span<const volatile int> s4{ vsp0}; // a span<const volatile int> pointing at volatile int.
63 assert(s1.size() + s2.size() + s3.size() + s4.size() == 0);
64 }
65
Stephan T. Lavaveja1857e22020-01-07 23:33:52 -080066// dynamic -> static (not allowed)
Marshall Clow8a0794b2018-07-24 03:01:02 +000067}
68
69
70template <typename T>
71constexpr bool testConstexprSpan()
72{
73 std::span<T> s0{};
Stephan T. Lavaveja1857e22020-01-07 23:33:52 -080074 std::span<T, 0> s1{};
Marshall Clow8a0794b2018-07-24 03:01:02 +000075 std::span<T> s2(s1); // static -> dynamic
76 ASSERT_NOEXCEPT(std::span<T> {s0});
77 ASSERT_NOEXCEPT(std::span<T, 0>{s1});
78 ASSERT_NOEXCEPT(std::span<T> {s1});
Marshall Clow8a0794b2018-07-24 03:01:02 +000079
80 return
81 s1.data() == nullptr && s1.size() == 0
82 && s2.data() == nullptr && s2.size() == 0;
83}
84
85
86template <typename T>
87void testRuntimeSpan()
88{
89 std::span<T> s0{};
Stephan T. Lavaveja1857e22020-01-07 23:33:52 -080090 std::span<T, 0> s1{};
Marshall Clow8a0794b2018-07-24 03:01:02 +000091 std::span<T> s2(s1); // static -> dynamic
92 ASSERT_NOEXCEPT(std::span<T> {s0});
93 ASSERT_NOEXCEPT(std::span<T, 0>{s1});
94 ASSERT_NOEXCEPT(std::span<T> {s1});
Marshall Clow8a0794b2018-07-24 03:01:02 +000095
96 assert(s1.data() == nullptr && s1.size() == 0);
97 assert(s2.data() == nullptr && s2.size() == 0);
98}
99
100
Marshall Clow8a0794b2018-07-24 03:01:02 +0000101struct A{};
102
JF Bastien2df59c52019-02-04 20:31:13 +0000103int main(int, char**)
Marshall Clow8a0794b2018-07-24 03:01:02 +0000104{
105 static_assert(testConstexprSpan<int>(), "");
106 static_assert(testConstexprSpan<long>(), "");
107 static_assert(testConstexprSpan<double>(), "");
108 static_assert(testConstexprSpan<A>(), "");
109
110 testRuntimeSpan<int>();
111 testRuntimeSpan<long>();
112 testRuntimeSpan<double>();
113 testRuntimeSpan<std::string>();
114 testRuntimeSpan<A>();
115
Marshall Clow8a0794b2018-07-24 03:01:02 +0000116 checkCV();
JF Bastien2df59c52019-02-04 20:31:13 +0000117
118 return 0;
Marshall Clow8a0794b2018-07-24 03:01:02 +0000119}