2 //===------------------------------ span ---------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===---------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
13 // template<class Container>
14 // constexpr span(Container& cont);
15 // template<class Container>
16 // constexpr span(const Container& cont);
18 // Remarks: These constructors shall not participate in overload resolution unless:
19 // — extent == dynamic_extent,
20 // — Container is not a specialization of span,
21 // — Container is not a specialization of array,
22 // — is_array_v<Container> is false,
23 // — data(cont) and size(cont) are both well-formed, and
24 // — remove_pointer_t<decltype(data(cont))>(*)[] is convertible to ElementType(*)[].
30 #include <forward_list>
34 #include "test_macros.h"
36 // Look ma - I'm a container!
39 constexpr IsAContainer() : v_
{} {}
40 constexpr size_t size() const {return 1;}
41 constexpr T
*data() {return &v_
;}
42 constexpr const T
*data() const {return &v_
;}
44 constexpr const T
*getV() const {return &v_
;} // for checking
49 struct NotAContainerNoData
{
50 size_t size() const {return 0;}
54 struct NotAContainerNoSize
{
55 const T
*data() const {return nullptr;}
59 struct NotAContainerPrivate
{
61 size_t size() const {return 0;}
62 const T
*data() const {return nullptr;}
65 template<class T
, size_t extent
, class container
>
66 std::span
<T
, extent
> createImplicitSpan(container c
) {
67 return {c
}; // expected-error {{chosen constructor is explicit in copy-initialization}}
73 // Making non-const spans from const sources (a temporary binds to `const &`)
75 std::span
<int> s1
{IsAContainer
<int>()}; // expected-error {{no matching constructor for initialization of 'std::span<int>'}}
76 std::span
<int> s3
{std::vector
<int>()}; // expected-error {{no matching constructor for initialization of 'std::span<int>'}}
79 // Missing size and/or data
81 std::span
<const int> s1
{NotAContainerNoData
<int>()}; // expected-error {{no matching constructor for initialization of 'std::span<const int>'}}
82 std::span
<const int> s3
{NotAContainerNoSize
<int>()}; // expected-error {{no matching constructor for initialization of 'std::span<const int>'}}
83 std::span
<const int> s5
{NotAContainerPrivate
<int>()}; // expected-error {{no matching constructor for initialization of 'std::span<const int>'}}
85 // Again with the standard containers
86 std::span
<const int> s11
{std::deque
<int>()}; // expected-error {{no matching constructor for initialization of 'std::span<const int>'}}
87 std::span
<const int> s13
{std::list
<int>()}; // expected-error {{no matching constructor for initialization of 'std::span<const int>'}}
88 std::span
<const int> s15
{std::forward_list
<int>()}; // expected-error {{no matching constructor for initialization of 'std::span<const int>'}}
94 std::span
<float> s1
{c
}; // expected-error {{no matching constructor for initialization of 'std::span<float>'}}
99 IsAContainer
<const int> c
;
100 IsAContainer
<const volatile int> cv
;
101 IsAContainer
< volatile int> v
;
103 std::span
< int> s1
{c
}; // expected-error {{no matching constructor for initialization of 'std::span<int>'}}
104 std::span
< int> s2
{v
}; // expected-error {{no matching constructor for initialization of 'std::span<int>'}}
105 std::span
< int> s3
{cv
}; // expected-error {{no matching constructor for initialization of 'std::span<int>'}}
106 std::span
<const int> s4
{v
}; // expected-error {{no matching constructor for initialization of 'std::span<const int>'}}
107 std::span
<const int> s5
{cv
}; // expected-error {{no matching constructor for initialization of 'std::span<const int>'}}
108 std::span
< volatile int> s6
{c
}; // expected-error {{no matching constructor for initialization of 'std::span<volatile int>'}}
109 std::span
< volatile int> s7
{cv
}; // expected-error {{no matching constructor for initialization of 'std::span<volatile int>'}}
112 // explicit constructor necessary
115 const IsAContainer
<int> cc
;
117 createImplicitSpan
<int, 1>(c
);
118 createImplicitSpan
<int, 1>(cc
);