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 | //===---------------------------------------------------------------------===// |
Louis Dionne | 31cbe0f | 2020-06-01 10:38:23 -0400 | [diff] [blame^] | 9 | // UNSUPPORTED: c++03, c++11, c++14, c++17 |
Marshall Clow | 8a0794b | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 10 | |
| 11 | // <span> |
| 12 | |
| 13 | // template<size_t N> |
| 14 | // constexpr span(array<value_type, N>& arr) noexcept; |
| 15 | // template<size_t N> |
Stephan T. Lavavej | dec8905 | 2018-11-14 03:06:06 +0000 | [diff] [blame] | 16 | // constexpr span(const array<value_type, N>& arr) noexcept; |
Marshall Clow | 8a0794b | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 17 | // |
| 18 | // Remarks: These constructors shall not participate in overload resolution unless: |
| 19 | // — extent == dynamic_extent || N == extent is true, and |
| 20 | // — remove_pointer_t<decltype(data(arr))>(*)[] is convertible to ElementType(*)[]. |
| 21 | // |
| 22 | |
| 23 | |
| 24 | #include <span> |
Stephan T. Lavavej | a1857e2 | 2020-01-07 23:33:52 -0800 | [diff] [blame] | 25 | #include <array> |
Marshall Clow | 8a0794b | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 26 | #include <cassert> |
| 27 | #include <string> |
| 28 | |
| 29 | #include "test_macros.h" |
Marshall Clow | 8bbf22e | 2018-07-24 03:41:36 +0000 | [diff] [blame] | 30 | // 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 Clow | 8a0794b | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 33 | |
| 34 | |
| 35 | void 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 | |
Casey Carter | 634a0ac | 2020-05-15 15:14:57 -0700 | [diff] [blame] | 71 | template <typename T, typename U> |
| 72 | constexpr bool testConstructorArray() { |
| 73 | std::array<U, 2> val = {U(), U()}; |
| 74 | ASSERT_NOEXCEPT(std::span<T>{val}); |
| 75 | ASSERT_NOEXCEPT(std::span<T, 2>{val}); |
| 76 | std::span<T> s1{val}; |
| 77 | std::span<T, 2> s2{val}; |
| 78 | return s1.data() == &val[0] && s1.size() == 2 && s2.data() == &val[0] && |
| 79 | s2.size() == 2; |
Michael Schellenberger Costa | ab9f111 | 2020-05-14 10:50:03 -0400 | [diff] [blame] | 80 | } |
| 81 | |
Casey Carter | 634a0ac | 2020-05-15 15:14:57 -0700 | [diff] [blame] | 82 | template <typename T, typename U> |
| 83 | constexpr bool testConstructorConstArray() { |
| 84 | const std::array<U, 2> val = {U(), U()}; |
| 85 | ASSERT_NOEXCEPT(std::span<const T>{val}); |
| 86 | ASSERT_NOEXCEPT(std::span<const T, 2>{val}); |
| 87 | std::span<const T> s1{val}; |
| 88 | std::span<const T, 2> s2{val}; |
| 89 | return s1.data() == &val[0] && s1.size() == 2 && s2.data() == &val[0] && |
| 90 | s2.size() == 2; |
Michael Schellenberger Costa | ab9f111 | 2020-05-14 10:50:03 -0400 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | template <typename T> |
| 94 | constexpr bool testConstructors() { |
Casey Carter | 634a0ac | 2020-05-15 15:14:57 -0700 | [diff] [blame] | 95 | static_assert(testConstructorArray<T, T>(), ""); |
| 96 | static_assert(testConstructorArray<const T, const T>(), ""); |
| 97 | static_assert(testConstructorArray<const T, T>(), ""); |
| 98 | static_assert(testConstructorConstArray<T, T>(), ""); |
| 99 | static_assert(testConstructorConstArray<const T, const T>(), ""); |
| 100 | static_assert(testConstructorConstArray<const T, T>(), ""); |
Michael Schellenberger Costa | ab9f111 | 2020-05-14 10:50:03 -0400 | [diff] [blame] | 101 | |
Casey Carter | 634a0ac | 2020-05-15 15:14:57 -0700 | [diff] [blame] | 102 | return testConstructorArray<T, T>() && |
| 103 | testConstructorArray<const T, const T>() && |
| 104 | testConstructorArray<const T, T>() && |
| 105 | testConstructorConstArray<T, T>() && |
| 106 | testConstructorConstArray<const T, const T>() && |
| 107 | testConstructorConstArray<const T, T>(); |
Marshall Clow | 8a0794b | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | struct A{}; |
| 111 | |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 112 | int main(int, char**) |
Marshall Clow | 8a0794b | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 113 | { |
Michael Schellenberger Costa | ab9f111 | 2020-05-14 10:50:03 -0400 | [diff] [blame] | 114 | assert(testConstructors<int>()); |
| 115 | assert(testConstructors<long>()); |
| 116 | assert(testConstructors<double>()); |
| 117 | assert(testConstructors<A>()); |
Marshall Clow | 8a0794b | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 118 | |
Michael Schellenberger Costa | ab9f111 | 2020-05-14 10:50:03 -0400 | [diff] [blame] | 119 | assert(testConstructors<int*>()); |
Michael Schellenberger Costa | ab9f111 | 2020-05-14 10:50:03 -0400 | [diff] [blame] | 120 | assert(testConstructors<const int*>()); |
Marshall Clow | 8a0794b | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 121 | |
| 122 | checkCV(); |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 123 | |
Michael Schellenberger Costa | ab9f111 | 2020-05-14 10:50:03 -0400 | [diff] [blame] | 124 | return 0; |
Marshall Clow | 8a0794b | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 125 | } |