1823fa098SKonstantin Varlamov //===----------------------------------------------------------------------===//
2823fa098SKonstantin Varlamov //
3823fa098SKonstantin Varlamov // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4823fa098SKonstantin Varlamov // See https://llvm.org/LICENSE.txt for license information.
5823fa098SKonstantin Varlamov // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6823fa098SKonstantin Varlamov //
7823fa098SKonstantin Varlamov //===----------------------------------------------------------------------===//
8823fa098SKonstantin Varlamov
9823fa098SKonstantin Varlamov // UNSUPPORTED: c++03, c++11, c++14, c++17
10823fa098SKonstantin Varlamov
11823fa098SKonstantin Varlamov // <ranges>
12823fa098SKonstantin Varlamov
13823fa098SKonstantin Varlamov // template<class T> struct tuple_size;
14823fa098SKonstantin Varlamov // template<size_t I, class T> struct tuple_element;
15823fa098SKonstantin Varlamov
16823fa098SKonstantin Varlamov #include <ranges>
17823fa098SKonstantin Varlamov // Note: make sure to not include `<utility>` (or any other header including `<utility>`) because it also makes some
18823fa098SKonstantin Varlamov // tuple specializations available, thus obscuring whether the `<ranges>` includes work correctly.
19823fa098SKonstantin Varlamov
20823fa098SKonstantin Varlamov using Iterator = int*;
21823fa098SKonstantin Varlamov
22823fa098SKonstantin Varlamov class SizedSentinel {
23823fa098SKonstantin Varlamov public:
24823fa098SKonstantin Varlamov constexpr bool operator==(int*) const;
25*d8681356SMark de Wever friend constexpr std::ptrdiff_t operator-(const SizedSentinel&, int*);
26*d8681356SMark de Wever friend constexpr std::ptrdiff_t operator-(int*, const SizedSentinel&);
27823fa098SKonstantin Varlamov };
28823fa098SKonstantin Varlamov
29823fa098SKonstantin Varlamov static_assert(std::sized_sentinel_for<SizedSentinel, Iterator>);
30823fa098SKonstantin Varlamov using SizedRange = std::ranges::subrange<Iterator, SizedSentinel>;
31823fa098SKonstantin Varlamov
32823fa098SKonstantin Varlamov using UnsizedSentinel = std::unreachable_sentinel_t;
33823fa098SKonstantin Varlamov static_assert(!std::sized_sentinel_for<UnsizedSentinel, Iterator>);
34823fa098SKonstantin Varlamov using UnsizedRange = std::ranges::subrange<Iterator, UnsizedSentinel>;
35823fa098SKonstantin Varlamov
36823fa098SKonstantin Varlamov // Because the sentinel is unsized while the subrange is sized, an additional integer member will be used to store the
37823fa098SKonstantin Varlamov // size -- make sure it doesn't affect the value of `tuple_size`.
38823fa098SKonstantin Varlamov using ThreeElementRange = std::ranges::subrange<Iterator, UnsizedSentinel, std::ranges::subrange_kind::sized>;
39823fa098SKonstantin Varlamov static_assert(std::ranges::sized_range<ThreeElementRange>);
40823fa098SKonstantin Varlamov
41823fa098SKonstantin Varlamov static_assert(std::tuple_size<SizedRange>::value == 2);
42823fa098SKonstantin Varlamov static_assert(std::tuple_size<UnsizedRange>::value == 2);
43823fa098SKonstantin Varlamov static_assert(std::tuple_size<ThreeElementRange>::value == 2);
44823fa098SKonstantin Varlamov
45823fa098SKonstantin Varlamov template <int I, class Range, class Expected>
test_tuple_element()46823fa098SKonstantin Varlamov constexpr bool test_tuple_element() {
47823fa098SKonstantin Varlamov static_assert(std::same_as<typename std::tuple_element<I, Range>::type, Expected>);
48823fa098SKonstantin Varlamov static_assert(std::same_as<typename std::tuple_element<I, const Range>::type, Expected>);
49823fa098SKonstantin Varlamov // Note: the Standard does not mandate a specialization of `tuple_element` for volatile, so trying a `volatile Range`
50823fa098SKonstantin Varlamov // would fail to compile.
51823fa098SKonstantin Varlamov
52823fa098SKonstantin Varlamov return true;
53823fa098SKonstantin Varlamov }
54823fa098SKonstantin Varlamov
main(int,char **)55823fa098SKonstantin Varlamov int main(int, char**) {
56823fa098SKonstantin Varlamov static_assert(test_tuple_element<0, SizedRange, Iterator>());
57823fa098SKonstantin Varlamov static_assert(test_tuple_element<1, SizedRange, SizedSentinel>());
58823fa098SKonstantin Varlamov static_assert(test_tuple_element<0, UnsizedRange, Iterator>());
59823fa098SKonstantin Varlamov static_assert(test_tuple_element<1, UnsizedRange, UnsizedSentinel>());
60823fa098SKonstantin Varlamov
61823fa098SKonstantin Varlamov return 0;
62823fa098SKonstantin Varlamov }
63