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