| 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 | // UNSUPPORTED: c++03, c++11, c++14, c++17 |
| 9 | |
| 10 | // <span> |
| 11 | |
| 12 | // template<class R> |
| 13 | // constexpr explicit(Extent != dynamic_extent) span(R&& r); |
| 14 | |
| 15 | #include <span> |
| 16 | #include <cassert> |
| 17 | #include <ranges> |
| 18 | #include <string_view> |
| 19 | #include <type_traits> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include "test_iterators.h" |
| 23 | |
| 24 | template <class T, std::size_t Extent> |
| 25 | constexpr void test_from_range() { |
| 26 | T val[3]{}; |
| 27 | std::span<T, Extent> s{val}; |
| 28 | assert(s.size() == std::size(val)); |
| 29 | assert(s.data() == std::data(val)); |
| 30 | } |
| 31 | |
| 32 | struct A {}; |
| 33 | |
| 34 | constexpr bool test() { |
| 35 | test_from_range<int, std::dynamic_extent>(); |
| 36 | test_from_range<int, 3>(); |
| 37 | test_from_range<A, std::dynamic_extent>(); |
| 38 | test_from_range<A, 3>(); |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | static_assert(!std::is_constructible_v<std::span<int>, std::vector<float>&>); // wrong type |
| 43 | static_assert(!std::is_constructible_v<std::span<int, 3>, std::vector<float>&>); // wrong type |
| 44 | |
| 45 | static_assert(std::is_constructible_v<std::span<int>, std::vector<int>&>); // non-borrowed lvalue |
| 46 | static_assert(std::is_constructible_v<std::span<int, 3>, std::vector<int>&>); // non-borrowed lvalue |
| 47 | static_assert(std::is_constructible_v<std::span<const int>, std::vector<int>&>); // non-borrowed lvalue |
| 48 | static_assert(std::is_constructible_v<std::span<const int, 3>, std::vector<int>&>); // non-borrowed lvalue |
| 49 | static_assert(!std::is_constructible_v<std::span<int>, const std::vector<int>&>); // non-borrowed const lvalue |
| 50 | static_assert(!std::is_constructible_v<std::span<int, 3>, const std::vector<int>&>); // non-borrowed const lvalue |
| 51 | static_assert(std::is_constructible_v<std::span<const int>, const std::vector<int>&>); // non-borrowed const lvalue |
| 52 | static_assert(std::is_constructible_v<std::span<const int, 3>, const std::vector<int>&>); // non-borrowed const lvalue |
| 53 | static_assert(std::is_constructible_v<std::span<const int>, std::vector<int>>); // non-borrowed rvalue |
| 54 | static_assert(std::is_constructible_v<std::span<const int, 3>, std::vector<int>>); // non-borrowed rvalue |
| 55 | static_assert(!std::is_constructible_v<std::span<int>, std::vector<int>&&>); // non-borrowed rvalue |
| 56 | static_assert(!std::is_constructible_v<std::span<int, 3>, std::vector<int>&&>); // non-borrowed rvalue |
| 57 | |
| 58 | static_assert(std::is_constructible_v<std::span<int>, |
| 59 | std::ranges::subrange<contiguous_iterator<int*>>>); // contiguous borrowed rvalue |
| 60 | static_assert(std::is_constructible_v<std::span<int, 3>, |
| 61 | std::ranges::subrange<contiguous_iterator<int*>>>); // contiguous borrowed rvalue |
| 62 | static_assert( |
| 63 | !std::is_constructible_v<std::span<int>, |
| 64 | std::ranges::subrange<random_access_iterator<int*>>>); // non-contiguous borrowed rvalue |
| 65 | static_assert( |
| 66 | !std::is_constructible_v<std::span<int, 3>, |
| 67 | std::ranges::subrange<random_access_iterator<int*>>>); // non-contiguous borrowed rvalue |
| 68 | |
| 69 | using BorrowedContiguousSizedRange = std::string_view; |
| 70 | static_assert(std::is_constructible_v<std::span<const char>, BorrowedContiguousSizedRange>); |
| 71 | static_assert(std::is_constructible_v<std::span<const char, 3>, BorrowedContiguousSizedRange>); |
| 72 | static_assert(!std::is_constructible_v<std::span<char>, BorrowedContiguousSizedRange>); |
| 73 | static_assert(!std::is_constructible_v<std::span<char, 3>, BorrowedContiguousSizedRange>); |
| 74 | |
| 75 | static_assert(std::is_convertible_v<BorrowedContiguousSizedRange&, std::span<const char>>); |
| 76 | static_assert(!std::is_convertible_v<BorrowedContiguousSizedRange&, std::span<const char, 3>>); |
| 77 | static_assert(!std::is_convertible_v<BorrowedContiguousSizedRange&, std::span<char>>); |
| 78 | static_assert(!std::is_convertible_v<BorrowedContiguousSizedRange&, std::span<char, 3>>); |
| 79 | static_assert(std::is_convertible_v<const BorrowedContiguousSizedRange&, std::span<const char>>); |
| 80 | static_assert(!std::is_convertible_v<const BorrowedContiguousSizedRange&, std::span<const char, 3>>); |
| 81 | |
| 82 | int main(int, char**) { |
| 83 | test(); |
| 84 | static_assert(test()); |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |