Marshall Clow | 8a0794b | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===------------------------------ span ---------------------------------===// |
| 3 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Marshall Clow | 8a0794b | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 7 | // |
| 8 | //===---------------------------------------------------------------------===// |
Stephan T. Lavavej | dec8905 | 2018-11-14 03:06:06 +0000 | [diff] [blame] | 9 | // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 |
Marshall Clow | 8a0794b | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 10 | |
| 11 | // <span> |
| 12 | |
Marshall Clow | 8eda3ad | 2019-03-06 03:59:44 +0000 | [diff] [blame] | 13 | // template<class OtherElementType, size_t OtherExtent> |
Marshall Clow | 8a0794b | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 14 | // 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 | |
| 27 | void 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. Lavavej | a1857e2 | 2020-01-07 23:33:52 -0800 | [diff] [blame] | 66 | // dynamic -> static (not allowed) |
Marshall Clow | 8a0794b | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | |
| 70 | template <typename T> |
| 71 | constexpr bool testConstexprSpan() |
| 72 | { |
| 73 | std::span<T> s0{}; |
Stephan T. Lavavej | a1857e2 | 2020-01-07 23:33:52 -0800 | [diff] [blame] | 74 | std::span<T, 0> s1{}; |
Marshall Clow | 8a0794b | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 75 | 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 Clow | 8a0794b | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 79 | |
| 80 | return |
| 81 | s1.data() == nullptr && s1.size() == 0 |
| 82 | && s2.data() == nullptr && s2.size() == 0; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | template <typename T> |
| 87 | void testRuntimeSpan() |
| 88 | { |
| 89 | std::span<T> s0{}; |
Stephan T. Lavavej | a1857e2 | 2020-01-07 23:33:52 -0800 | [diff] [blame] | 90 | std::span<T, 0> s1{}; |
Marshall Clow | 8a0794b | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 91 | 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 Clow | 8a0794b | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 95 | |
| 96 | assert(s1.data() == nullptr && s1.size() == 0); |
| 97 | assert(s2.data() == nullptr && s2.size() == 0); |
| 98 | } |
| 99 | |
| 100 | |
Marshall Clow | 8a0794b | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 101 | struct A{}; |
| 102 | |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 103 | int main(int, char**) |
Marshall Clow | 8a0794b | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 104 | { |
| 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 Clow | 8a0794b | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 116 | checkCV(); |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 117 | |
| 118 | return 0; |
Marshall Clow | 8a0794b | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 119 | } |