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