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 // <ranges>
14 
15 //  template<class T> struct tuple_size;
16 //  template<size_t I, class T> struct tuple_element;
17 
18 #include <ranges>
19 // Note: make sure to not include `<utility>` (or any other header including `<utility>`) because it also makes some
20 // tuple specializations available, thus obscuring whether the `<ranges>` includes work correctly.
21 
22 using Iterator = int*;
23 
24 class SizedSentinel {
25 public:
26     constexpr bool operator==(int*) const;
27     friend constexpr ptrdiff_t operator-(const SizedSentinel&, int*);
28     friend constexpr ptrdiff_t operator-(int*, const SizedSentinel&);
29 };
30 
31 static_assert(std::sized_sentinel_for<SizedSentinel, Iterator>);
32 using SizedRange = std::ranges::subrange<Iterator, SizedSentinel>;
33 
34 using UnsizedSentinel = std::unreachable_sentinel_t;
35 static_assert(!std::sized_sentinel_for<UnsizedSentinel, Iterator>);
36 using UnsizedRange = std::ranges::subrange<Iterator, UnsizedSentinel>;
37 
38 // Because the sentinel is unsized while the subrange is sized, an additional integer member will be used to store the
39 // size -- make sure it doesn't affect the value of `tuple_size`.
40 using ThreeElementRange = std::ranges::subrange<Iterator, UnsizedSentinel, std::ranges::subrange_kind::sized>;
41 static_assert(std::ranges::sized_range<ThreeElementRange>);
42 
43 static_assert(std::tuple_size<SizedRange>::value == 2);
44 static_assert(std::tuple_size<UnsizedRange>::value == 2);
45 static_assert(std::tuple_size<ThreeElementRange>::value == 2);
46 
47 template <int I, class Range, class Expected>
48 constexpr bool test_tuple_element() {
49   static_assert(std::same_as<typename std::tuple_element<I, Range>::type, Expected>);
50   static_assert(std::same_as<typename std::tuple_element<I, const Range>::type, Expected>);
51   // Note: the Standard does not mandate a specialization of `tuple_element` for volatile, so trying a `volatile Range`
52   // would fail to compile.
53 
54   return true;
55 }
56 
57 int main(int, char**) {
58   static_assert(test_tuple_element<0, SizedRange, Iterator>());
59   static_assert(test_tuple_element<1, SizedRange, SizedSentinel>());
60   static_assert(test_tuple_element<0, UnsizedRange, Iterator>());
61   static_assert(test_tuple_element<1, UnsizedRange, UnsizedSentinel>());
62 
63   return 0;
64 }
65