| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
// UNSUPPORTED: c++03, c++11, c++14, c++17 |
| 10 |
|
| 11 |
// <span> |
| 12 |
|
| 13 |
// constexpr explicit(extent != dynamic_extent) span(std::initializer_list<value_type> il); // Since C++26 |
| 14 |
|
| 15 |
#include <any> |
| 16 |
#include <cassert> |
| 17 |
#include <cstddef> |
| 18 |
#include <initializer_list> |
| 19 |
#include <span> |
| 20 |
#include <type_traits> |
| 21 |
|
| 22 |
#include "test_convertible.h" |
| 23 |
#include "test_macros.h" |
| 24 |
|
| 25 |
#if TEST_STD_VER >= 26 |
| 26 |
|
| 27 |
// SFINAE |
| 28 |
|
| 29 |
template <typename T> |
| 30 |
concept ConstElementType = std::is_const_v<typename T::element_type>; |
| 31 |
|
| 32 |
static_assert(ConstElementType<std::span<const int>>); |
| 33 |
static_assert(!ConstElementType<std::span<int>>); |
| 34 |
static_assert(ConstElementType<std::span<const int, 94>>); |
| 35 |
static_assert(!ConstElementType<std::span<int, 94>>); |
| 36 |
|
| 37 |
// Constructor constraings |
| 38 |
|
| 39 |
template <typename I, typename T, std::size_t... N> |
| 40 |
concept HasInitializerListCtr = requires(I il) { std::span<T, N...>{il}; }; |
| 41 |
|
| 42 |
static_assert(HasInitializerListCtr<std::initializer_list<const int>, const int>); |
| 43 |
static_assert(!HasInitializerListCtr<std::initializer_list<int>, int>); |
| 44 |
static_assert(HasInitializerListCtr<std::initializer_list<const int>, const int, 94>); |
| 45 |
static_assert(!HasInitializerListCtr<std::initializer_list<int>, int, 94>); |
| 46 |
|
| 47 |
// Constructor conditionally explicit |
| 48 |
|
| 49 |
static_assert(!test_convertible<std::span<const int, 28>, std::initializer_list<int>>(), |
| 50 |
"This constructor must be explicit"); |
| 51 |
static_assert(std::is_constructible_v<std::span<const int, 28>, std::initializer_list<int>>); |
| 52 |
static_assert(test_convertible<std::span<const int>, std::initializer_list<int>>(), |
| 53 |
"This constructor must not be explicit"); |
| 54 |
static_assert(std::is_constructible_v<std::span<const int>, std::initializer_list<int>>); |
| 55 |
|
| 56 |
#endif |
| 57 |
|
| 58 |
struct Sink { |
| 59 |
constexpr Sink() = default; |
| 60 |
constexpr Sink(Sink*) {} |
| 61 |
}; |
| 62 |
|
| 63 |
constexpr std::size_t count(std::span<const Sink> sp) { return sp.size(); } |
| 64 |
|
| 65 |
template <std::size_t N> |
| 66 |
constexpr std::size_t count_n(std::span<const Sink, N> sp) { |
| 67 |
return sp.size(); |
| 68 |
} |
| 69 |
|
| 70 |
constexpr bool test() { |
| 71 |
#if TEST_STD_VER >= 26 |
| 72 |
// Dynamic extent |
| 73 |
{ |
| 74 |
Sink a[10]; |
| 75 |
|
| 76 |
assert(count({a}) == 1); |
| 77 |
assert(count({a, a + 10}) == 2); |
| 78 |
assert(count({a, a + 1, a + 2}) == 3); |
| 79 |
assert(count(std::initializer_list<Sink>{a[0], a[1], a[2], a[3]}) == 4); |
| 80 |
} |
| 81 |
#else |
| 82 |
{ |
| 83 |
Sink a[10]; |
| 84 |
|
| 85 |
assert(count({a}) == 10); |
| 86 |
assert(count({a, a + 10}) == 10); |
| 87 |
assert(count_n<10>({a}) == 10); |
| 88 |
} |
| 89 |
#endif |
| 90 |
|
| 91 |
return true; |
| 92 |
} |
| 93 |
|
| 94 |
// Test P2447R4 "Annex C examples" |
| 95 |
|
| 96 |
constexpr int three(std::span<void* const> sp) { return static_cast<int>(sp.size()); } |
| 97 |
|
| 98 |
constexpr int four(std::span<const std::any> sp) { return static_cast<int>(sp.size()); } |
| 99 |
|
| 100 |
bool test_P2447R4_annex_c_examples() { |
| 101 |
// 1. Overload resolution is affected |
| 102 |
// --> tested in "initializer_list.verify.cpp" |
| 103 |
|
| 104 |
// 2. The `initializer_list` ctor has high precedence |
| 105 |
// --> tested in "initializer_list.verify.cpp" |
| 106 |
|
| 107 |
// 3. Implicit two-argument construction with a highly convertible value_type |
| 108 |
#if TEST_STD_VER >= 26 |
| 109 |
{ |
| 110 |
void* a[10]; |
| 111 |
assert(three({a, 0}) == 2); |
| 112 |
} |
| 113 |
{ |
| 114 |
std::any a[10]; |
| 115 |
assert(four({a, a + 10}) == 2); |
| 116 |
} |
| 117 |
#else |
| 118 |
{ |
| 119 |
void* a[10]; |
| 120 |
assert(three({a, 0}) == 0); |
| 121 |
} |
| 122 |
{ |
| 123 |
std::any a[10]; |
| 124 |
assert(four({a, a + 10}) == 10); |
| 125 |
} |
| 126 |
#endif |
| 127 |
|
| 128 |
return true; |
| 129 |
} |
| 130 |
|
| 131 |
int main(int, char**) { |
| 132 |
assert(test()); |
| 133 |
static_assert(test()); |
| 134 |
|
| 135 |
assert(test_P2447R4_annex_c_examples()); |
| 136 |
|
| 137 |
return 0; |
| 138 |
} |