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