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