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