xref: /llvm-project/libcxx/test/std/containers/views/views.span/span.cons/iterator_len.pass.cpp (revision e99c4906e44ae3f921fa05356909d006cda8d954)
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 It>
13 // constexpr explicit(Extent != dynamic_extent) span(It first, size_type count);
14 //  If Extent is not equal to dynamic_extent, then count shall be equal to Extent.
15 //
16 
17 #include <cassert>
18 #include <cstddef>
19 #include <iterator>
20 #include <span>
21 #include <type_traits>
22 
23 template <std::size_t Extent>
24 constexpr void test_constructibility() {
25   struct Other {};
26   static_assert(std::is_constructible_v<std::span<int, Extent>, int*, std::size_t>);
27   static_assert(!std::is_constructible_v<std::span<int, Extent>, const int*, std::size_t>);
28   static_assert(std::is_constructible_v<std::span<const int, Extent>, int*, std::size_t>);
29   static_assert(std::is_constructible_v<std::span<const int, Extent>, const int*, std::size_t>);
30   static_assert(!std::is_constructible_v<std::span<int, Extent>, volatile int*, std::size_t>);
31   static_assert(!std::is_constructible_v<std::span<int, Extent>, const volatile int*, std::size_t>);
32   static_assert(!std::is_constructible_v<std::span<const int, Extent>, volatile int*, std::size_t>);
33   static_assert(!std::is_constructible_v<std::span<const int, Extent>, const volatile int*, std::size_t>);
34   static_assert(!std::is_constructible_v<std::span<volatile int, Extent>, const int*, std::size_t>);
35   static_assert(!std::is_constructible_v<std::span<volatile int, Extent>, const volatile int*, std::size_t>);
36   static_assert(
37       !std::is_constructible_v<std::span<int, Extent>, double*, std::size_t>); // iterator type differs from span type
38   static_assert(!std::is_constructible_v<std::span<int, Extent>, std::size_t, size_t>);
39   static_assert(!std::is_constructible_v<std::span<int, Extent>, Other*, std::size_t>); // unrelated iterator type
40 }
41 
42 template <class T>
43 constexpr bool test_ctor() {
44   T val[2] = {};
45   auto s1 = std::span<T>(val, 2);
46   auto s2 = std::span<T, 2>(val, 2);
47   assert(s1.data() == std::data(val) && s1.size() == std::size(val));
48   assert(s2.data() == std::data(val) && s2.size() == std::size(val));
49   return true;
50 }
51 
52 constexpr bool test() {
53   test_constructibility<std::dynamic_extent>();
54   test_constructibility<3>();
55 
56   struct A {};
57   test_ctor<int>();
58   test_ctor<A>();
59 
60   return true;
61 }
62 
63 int main(int, char**) {
64   test();
65   static_assert(test());
66 
67   return 0;
68 }
69