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