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

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