blob: 27623a4c9f74d21bf727147bd4dc2fe392dd37d1 [file] [log] [blame]
Marshall Clow8a0794b2018-07-24 03:01:02 +00001// -*- C++ -*-
2//===------------------------------ span ---------------------------------===//
3//
4// The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
8//
9//===---------------------------------------------------------------------===//
Stephan T. Lavavejdec89052018-11-14 03:06:06 +000010// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
Marshall Clow8a0794b2018-07-24 03:01:02 +000011
12// <span>
13
14// template<size_t N>
15// constexpr span(array<value_type, N>& arr) noexcept;
16// template<size_t N>
Stephan T. Lavavejdec89052018-11-14 03:06:06 +000017// constexpr span(const array<value_type, N>& arr) noexcept;
Marshall Clow8a0794b2018-07-24 03:01:02 +000018//
19// Remarks: These constructors shall not participate in overload resolution unless:
20// — extent == dynamic_extent || N == extent is true, and
21// — remove_pointer_t<decltype(data(arr))>(*)[] is convertible to ElementType(*)[].
22//
23
24
25#include <span>
26#include <cassert>
27#include <string>
28
29#include "test_macros.h"
Marshall Clow8bbf22e2018-07-24 03:41:36 +000030// 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"
Marshall Clow8a0794b2018-07-24 03:01:02 +000033
34
35void 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
72template <typename T>
73constexpr bool testConstexprSpan()
74{
75 constexpr std::array<T,2> val = { T(), T() };
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
81 s1.data() == &val[0] && s1.size() == 2
82 && s2.data() == &val[0] && s2.size() == 2;
83}
84
85
86template <typename T>
87void testRuntimeSpan()
88{
89 std::array<T,2> val;
90 ASSERT_NOEXCEPT(std::span<T> {val});
91 ASSERT_NOEXCEPT(std::span<T, 2>{val});
92 std::span<T> s1{val};
93 std::span<T, 2> s2{val};
94 assert(s1.data() == &val[0] && s1.size() == 2);
95 assert(s2.data() == &val[0] && s2.size() == 2);
96}
97
98struct A{};
99
100int main ()
101{
102 static_assert(testConstexprSpan<int>(), "");
103 static_assert(testConstexprSpan<long>(), "");
104 static_assert(testConstexprSpan<double>(), "");
105 static_assert(testConstexprSpan<A>(), "");
106
107 testRuntimeSpan<int>();
108 testRuntimeSpan<long>();
109 testRuntimeSpan<double>();
110 testRuntimeSpan<std::string>();
111 testRuntimeSpan<A>();
112
113 checkCV();
114}