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
24template <class T, std::size_t Extent>
25constexpr 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
32struct A {};
33
34constexpr 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
42static_assert(!std::is_constructible_v<std::span<int>, std::vector<float>&>); // wrong type
43static_assert(!std::is_constructible_v<std::span<int, 3>, std::vector<float>&>); // wrong type
44
45static_assert(std::is_constructible_v<std::span<int>, std::vector<int>&>); // non-borrowed lvalue
46static_assert(std::is_constructible_v<std::span<int, 3>, std::vector<int>&>); // non-borrowed lvalue
47static_assert(std::is_constructible_v<std::span<const int>, std::vector<int>&>); // non-borrowed lvalue
48static_assert(std::is_constructible_v<std::span<const int, 3>, std::vector<int>&>); // non-borrowed lvalue
49static_assert(!std::is_constructible_v<std::span<int>, const std::vector<int>&>); // non-borrowed const lvalue
50static_assert(!std::is_constructible_v<std::span<int, 3>, const std::vector<int>&>); // non-borrowed const lvalue
51static_assert(std::is_constructible_v<std::span<const int>, const std::vector<int>&>); // non-borrowed const lvalue
52static_assert(std::is_constructible_v<std::span<const int, 3>, const std::vector<int>&>); // non-borrowed const lvalue
53static_assert(std::is_constructible_v<std::span<const int>, std::vector<int>>); // non-borrowed rvalue
54static_assert(std::is_constructible_v<std::span<const int, 3>, std::vector<int>>); // non-borrowed rvalue
55static_assert(!std::is_constructible_v<std::span<int>, std::vector<int>&&>); // non-borrowed rvalue
56static_assert(!std::is_constructible_v<std::span<int, 3>, std::vector<int>&&>); // non-borrowed rvalue
57
58static_assert(std::is_constructible_v<std::span<int>,
59 std::ranges::subrange<contiguous_iterator<int*>>>); // contiguous borrowed rvalue
60static_assert(std::is_constructible_v<std::span<int, 3>,
61 std::ranges::subrange<contiguous_iterator<int*>>>); // contiguous borrowed rvalue
62static_assert(
63 !std::is_constructible_v<std::span<int>,
64 std::ranges::subrange<random_access_iterator<int*>>>); // non-contiguous borrowed rvalue
65static_assert(
66 !std::is_constructible_v<std::span<int, 3>,
67 std::ranges::subrange<random_access_iterator<int*>>>); // non-contiguous borrowed rvalue
68
69using BorrowedContiguousSizedRange = std::string_view;
70static_assert(std::is_constructible_v<std::span<const char>, BorrowedContiguousSizedRange>);
71static_assert(std::is_constructible_v<std::span<const char, 3>, BorrowedContiguousSizedRange>);
72static_assert(!std::is_constructible_v<std::span<char>, BorrowedContiguousSizedRange>);
73static_assert(!std::is_constructible_v<std::span<char, 3>, BorrowedContiguousSizedRange>);
74
75static_assert(std::is_convertible_v<BorrowedContiguousSizedRange&, std::span<const char>>);
76static_assert(!std::is_convertible_v<BorrowedContiguousSizedRange&, std::span<const char, 3>>);
77static_assert(!std::is_convertible_v<BorrowedContiguousSizedRange&, std::span<char>>);
78static_assert(!std::is_convertible_v<BorrowedContiguousSizedRange&, std::span<char, 3>>);
79static_assert(std::is_convertible_v<const BorrowedContiguousSizedRange&, std::span<const char>>);
80static_assert(!std::is_convertible_v<const BorrowedContiguousSizedRange&, std::span<const char, 3>>);
81
82int main(int, char**) {
83 test();
84 static_assert(test());
85
86 return 0;
87}
88

source code of libcxx/test/std/containers/views/views.span/span.cons/range.pass.cpp