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
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 // UNSUPPORTED: libcpp-has-no-incomplete-ranges
11
12 // constexpr subrange() requires default_initializable<I>;
13
14 #include <ranges>
15
16 #include <cassert>
17 #include <cstddef>
18
19 #include "test_iterators.h"
20
21 // An input_or_output_iterator that is not default constructible so we can test
22 // the `requires` on subrange's default constructor.
23 struct NoDefaultIterator {
24 using difference_type = std::ptrdiff_t;
25 NoDefaultIterator() = delete;
26 NoDefaultIterator& operator++();
27 void operator++(int);
28 int& operator*() const;
29 friend bool operator==(NoDefaultIterator const&, NoDefaultIterator const&);
30 };
31 static_assert(std::input_or_output_iterator<NoDefaultIterator>);
32
33 // A sentinel type for the above iterator
34 struct Sentinel {
35 friend bool operator==(NoDefaultIterator const&, Sentinel const&);
36 friend bool operator==(Sentinel const&, NoDefaultIterator const&);
37 friend bool operator!=(NoDefaultIterator const&, Sentinel const&);
38 friend bool operator!=(Sentinel const&, NoDefaultIterator const&);
39 };
40
test()41 constexpr bool test() {
42 {
43 static_assert(!std::is_default_constructible_v<std::ranges::subrange<NoDefaultIterator, Sentinel, std::ranges::subrange_kind::sized>>);
44 static_assert(!std::is_default_constructible_v<std::ranges::subrange<NoDefaultIterator, Sentinel, std::ranges::subrange_kind::unsized>>);
45 }
46
47 {
48 using Iter = forward_iterator<int*>;
49 std::ranges::subrange<Iter, Iter, std::ranges::subrange_kind::sized> subrange;
50 assert(subrange.begin() == Iter());
51 assert(subrange.end() == Iter());
52 }
53 {
54 using Iter = forward_iterator<int*>;
55 std::ranges::subrange<Iter, Iter, std::ranges::subrange_kind::unsized> subrange;
56 assert(subrange.begin() == Iter());
57 assert(subrange.end() == Iter());
58 }
59
60 return true;
61 }
62
main(int,char **)63 int main(int, char**) {
64 test();
65 static_assert(test());
66
67 return 0;
68 }
69