88 lines · cpp
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-exception6//7//===---------------------------------------------------------------------===//8// UNSUPPORTED: c++03, c++11, c++14, c++179 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 type43static_assert(!std::is_constructible_v<std::span<int, 3>, std::vector<float>&>); // wrong type44 45static_assert(std::is_constructible_v<std::span<int>, std::vector<int>&>); // non-borrowed lvalue46static_assert(std::is_constructible_v<std::span<int, 3>, std::vector<int>&>); // non-borrowed lvalue47static_assert(std::is_constructible_v<std::span<const int>, std::vector<int>&>); // non-borrowed lvalue48static_assert(std::is_constructible_v<std::span<const int, 3>, std::vector<int>&>); // non-borrowed lvalue49static_assert(!std::is_constructible_v<std::span<int>, const std::vector<int>&>); // non-borrowed const lvalue50static_assert(!std::is_constructible_v<std::span<int, 3>, const std::vector<int>&>); // non-borrowed const lvalue51static_assert(std::is_constructible_v<std::span<const int>, const std::vector<int>&>); // non-borrowed const lvalue52static_assert(std::is_constructible_v<std::span<const int, 3>, const std::vector<int>&>); // non-borrowed const lvalue53static_assert(std::is_constructible_v<std::span<const int>, std::vector<int>>); // non-borrowed rvalue54static_assert(std::is_constructible_v<std::span<const int, 3>, std::vector<int>>); // non-borrowed rvalue55static_assert(!std::is_constructible_v<std::span<int>, std::vector<int>&&>); // non-borrowed rvalue56static_assert(!std::is_constructible_v<std::span<int, 3>, std::vector<int>&&>); // non-borrowed rvalue57 58static_assert(std::is_constructible_v<std::span<int>,59 std::ranges::subrange<contiguous_iterator<int*>>>); // contiguous borrowed rvalue60static_assert(std::is_constructible_v<std::span<int, 3>,61 std::ranges::subrange<contiguous_iterator<int*>>>); // contiguous borrowed rvalue62static_assert(63 !std::is_constructible_v<std::span<int>,64 std::ranges::subrange<random_access_iterator<int*>>>); // non-contiguous borrowed rvalue65static_assert(66 !std::is_constructible_v<std::span<int, 3>,67 std::ranges::subrange<random_access_iterator<int*>>>); // non-contiguous borrowed rvalue68 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