xref: /llvm-project/libcxx/test/std/containers/views/views.span/span.cons/range.pass.cpp (revision fb855eb941b6d740cc6560297d0b4d3201dcaf9f)
1a8cf78c7SLouis Dionne //===---------------------------------------------------------------------===//
2a8cf78c7SLouis Dionne //
3a8cf78c7SLouis Dionne // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4a8cf78c7SLouis Dionne // See https://llvm.org/LICENSE.txt for license information.
5a8cf78c7SLouis Dionne // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a8cf78c7SLouis Dionne //
7a8cf78c7SLouis Dionne //===---------------------------------------------------------------------===//
8a8cf78c7SLouis Dionne // UNSUPPORTED: c++03, c++11, c++14, c++17
9a8cf78c7SLouis Dionne 
10a8cf78c7SLouis Dionne // <span>
11a8cf78c7SLouis Dionne 
12a8cf78c7SLouis Dionne //  template<class R>
13a8cf78c7SLouis Dionne //    constexpr explicit(Extent != dynamic_extent) span(R&& r);
14a8cf78c7SLouis Dionne 
15a8cf78c7SLouis Dionne 
16a8cf78c7SLouis Dionne #include <span>
17a8cf78c7SLouis Dionne #include <cassert>
18a8cf78c7SLouis Dionne #include <ranges>
19a8cf78c7SLouis Dionne #include <string_view>
20a8cf78c7SLouis Dionne #include <type_traits>
21a8cf78c7SLouis Dionne #include <vector>
22a8cf78c7SLouis Dionne 
23a8cf78c7SLouis Dionne #include "test_iterators.h"
24a8cf78c7SLouis Dionne 
25*fb855eb9SMark de Wever template <class T, std::size_t Extent>
test_from_range()26a8cf78c7SLouis Dionne constexpr void test_from_range() {
27a8cf78c7SLouis Dionne   T val[3]{};
28a8cf78c7SLouis Dionne   std::span<T, Extent> s{val};
29a8cf78c7SLouis Dionne   assert(s.size() == std::size(val));
30a8cf78c7SLouis Dionne   assert(s.data() == std::data(val));
31a8cf78c7SLouis Dionne }
32a8cf78c7SLouis Dionne 
33a8cf78c7SLouis Dionne struct A {};
34a8cf78c7SLouis Dionne 
test()35a8cf78c7SLouis Dionne constexpr bool test() {
36a8cf78c7SLouis Dionne   test_from_range<int, std::dynamic_extent>();
37a8cf78c7SLouis Dionne   test_from_range<int, 3>();
38a8cf78c7SLouis Dionne   test_from_range<A, std::dynamic_extent>();
39a8cf78c7SLouis Dionne   test_from_range<A, 3>();
40a8cf78c7SLouis Dionne   return true;
41a8cf78c7SLouis Dionne }
42a8cf78c7SLouis Dionne 
43a8cf78c7SLouis Dionne static_assert(!std::is_constructible_v<std::span<int>, std::vector<float>&>);    // wrong type
44a8cf78c7SLouis Dionne static_assert(!std::is_constructible_v<std::span<int, 3>, std::vector<float>&>); // wrong type
45a8cf78c7SLouis Dionne 
46a8cf78c7SLouis Dionne static_assert(std::is_constructible_v<std::span<int>, std::vector<int>&>);                // non-borrowed lvalue
47a8cf78c7SLouis Dionne static_assert(std::is_constructible_v<std::span<int, 3>, std::vector<int>&>);             // non-borrowed lvalue
48a8cf78c7SLouis Dionne static_assert(std::is_constructible_v<std::span<const int>, std::vector<int>&>);          // non-borrowed lvalue
49a8cf78c7SLouis Dionne static_assert(std::is_constructible_v<std::span<const int, 3>, std::vector<int>&>);       // non-borrowed lvalue
50a8cf78c7SLouis Dionne static_assert(!std::is_constructible_v<std::span<int>, const std::vector<int>&>);         // non-borrowed const lvalue
51a8cf78c7SLouis Dionne static_assert(!std::is_constructible_v<std::span<int, 3>, const std::vector<int>&>);      // non-borrowed const lvalue
52a8cf78c7SLouis Dionne static_assert(std::is_constructible_v<std::span<const int>, const std::vector<int>&>);    // non-borrowed const lvalue
53a8cf78c7SLouis Dionne static_assert(std::is_constructible_v<std::span<const int, 3>, const std::vector<int>&>); // non-borrowed const lvalue
54a8cf78c7SLouis Dionne static_assert(std::is_constructible_v<std::span<const int>, std::vector<int>>);           // non-borrowed rvalue
55a8cf78c7SLouis Dionne static_assert(std::is_constructible_v<std::span<const int, 3>, std::vector<int>>);        // non-borrowed rvalue
56a8cf78c7SLouis Dionne static_assert(!std::is_constructible_v<std::span<int>, std::vector<int>&&>);              // non-borrowed rvalue
57a8cf78c7SLouis Dionne static_assert(!std::is_constructible_v<std::span<int, 3>, std::vector<int>&&>);           // non-borrowed rvalue
58a8cf78c7SLouis Dionne 
59a8cf78c7SLouis Dionne static_assert(std::is_constructible_v<std::span<int>, std::ranges::subrange<contiguous_iterator<int*>>>);         // contiguous borrowed rvalue
60a8cf78c7SLouis Dionne static_assert(std::is_constructible_v<std::span<int, 3>, std::ranges::subrange<contiguous_iterator<int*>>>);      // contiguous borrowed rvalue
61a8cf78c7SLouis Dionne static_assert(!std::is_constructible_v<std::span<int>, std::ranges::subrange<random_access_iterator<int*>>>);     // non-contiguous borrowed rvalue
62a8cf78c7SLouis Dionne static_assert(!std::is_constructible_v<std::span<int, 3>, std::ranges::subrange<random_access_iterator<int*>>>);  // non-contiguous borrowed rvalue
63a8cf78c7SLouis Dionne 
64a8cf78c7SLouis Dionne using BorrowedContiguousSizedRange = std::string_view;
65a8cf78c7SLouis Dionne static_assert(std::is_constructible_v<std::span<const char>, BorrowedContiguousSizedRange>);
66a8cf78c7SLouis Dionne static_assert(std::is_constructible_v<std::span<const char, 3>, BorrowedContiguousSizedRange>);
67a8cf78c7SLouis Dionne static_assert(!std::is_constructible_v<std::span<char>, BorrowedContiguousSizedRange>);
68a8cf78c7SLouis Dionne static_assert(!std::is_constructible_v<std::span<char, 3>, BorrowedContiguousSizedRange>);
69a8cf78c7SLouis Dionne 
70a8cf78c7SLouis Dionne static_assert(std::is_convertible_v<BorrowedContiguousSizedRange&, std::span<const char>>);
71a8cf78c7SLouis Dionne static_assert(!std::is_convertible_v<BorrowedContiguousSizedRange&, std::span<const char, 3>>);
72a8cf78c7SLouis Dionne static_assert(!std::is_convertible_v<BorrowedContiguousSizedRange&, std::span<char>>);
73a8cf78c7SLouis Dionne static_assert(!std::is_convertible_v<BorrowedContiguousSizedRange&, std::span<char, 3>>);
74a8cf78c7SLouis Dionne static_assert(std::is_convertible_v<const BorrowedContiguousSizedRange&, std::span<const char>>);
75a8cf78c7SLouis Dionne static_assert(!std::is_convertible_v<const BorrowedContiguousSizedRange&, std::span<const char, 3>>);
76a8cf78c7SLouis Dionne 
main(int,char **)77a8cf78c7SLouis Dionne int main(int, char**) {
78a8cf78c7SLouis Dionne   test();
79a8cf78c7SLouis Dionne   static_assert(test());
80a8cf78c7SLouis Dionne 
81a8cf78c7SLouis Dionne   return 0;
82a8cf78c7SLouis Dionne }
83