blob: 31beb2fa202fa4e86b7ce94ba9087781502e67bf [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//===---------------------------------------------------------------------===//
Louis Dionne31cbe0f2020-06-01 10:38:23 -04009// UNSUPPORTED: c++03, c++11, c++14, c++17
Marshall Clow8a0794b2018-07-24 03:01:02 +000010
11// <span>
12
13// template<size_t N>
14// constexpr span(array<value_type, N>& arr) noexcept;
15// template<size_t N>
Stephan T. Lavavejdec89052018-11-14 03:06:06 +000016// constexpr span(const array<value_type, N>& arr) noexcept;
Marshall Clow8a0794b2018-07-24 03:01:02 +000017//
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. Lavaveja1857e22020-01-07 23:33:52 -080025#include <array>
Marshall Clow8a0794b2018-07-24 03:01:02 +000026#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
Casey Carter634a0ac2020-05-15 15:14:57 -070071template <typename T, typename U>
72constexpr 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 Costaab9f1112020-05-14 10:50:03 -040080}
81
Casey Carter634a0ac2020-05-15 15:14:57 -070082template <typename T, typename U>
83constexpr 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 Costaab9f1112020-05-14 10:50:03 -040091}
92
93template <typename T>
94constexpr bool testConstructors() {
Casey Carter634a0ac2020-05-15 15:14:57 -070095 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 Costaab9f1112020-05-14 10:50:03 -0400101
Casey Carter634a0ac2020-05-15 15:14:57 -0700102 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 Clow8a0794b2018-07-24 03:01:02 +0000108}
109
110struct A{};
111
JF Bastien2df59c52019-02-04 20:31:13 +0000112int main(int, char**)
Marshall Clow8a0794b2018-07-24 03:01:02 +0000113{
Michael Schellenberger Costaab9f1112020-05-14 10:50:03 -0400114 assert(testConstructors<int>());
115 assert(testConstructors<long>());
116 assert(testConstructors<double>());
117 assert(testConstructors<A>());
Marshall Clow8a0794b2018-07-24 03:01:02 +0000118
Michael Schellenberger Costaab9f1112020-05-14 10:50:03 -0400119 assert(testConstructors<int*>());
Michael Schellenberger Costaab9f1112020-05-14 10:50:03 -0400120 assert(testConstructors<const int*>());
Marshall Clow8a0794b2018-07-24 03:01:02 +0000121
122 checkCV();
JF Bastien2df59c52019-02-04 20:31:13 +0000123
Michael Schellenberger Costaab9f1112020-05-14 10:50:03 -0400124 return 0;
Marshall Clow8a0794b2018-07-24 03:01:02 +0000125}