Skip to content

Commit 823fa09

Browse files
committed
[libc++][ranges][NFC] Test the specializations of tuple_{size,element} for ranges.
Also update the synopsis in `<ranges>` to mention the specializations. Differential Revision: https://reviews.llvm.org/D118686
1 parent 167b623 commit 823fa09

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

libcxx/docs/Status/RangesPaper.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Section,Description,Dependencies,Assignee,Complete
2-
`[tuple.helper] <https://wg21.link/tuple.helper>`_,Update <tuple> includes.,None,Konstantin Varlamov,Not started
2+
`[tuple.helper] <https://wg21.link/tuple.helper>`_,`Update <tuple> includes. <https://llvm.org/D118686>`_,None,Konstantin Varlamov,
33
`[range.cmp] <https://wg21.link/range.cmp>`_,"| `ranges::equal_to <https://llvm.org/D100429>`_
44
| `ranges::not_equal_to <https://llvm.org/D100429>`_
55
| `ranges::less <https://llvm.org/D100429>`_

libcxx/include/ranges

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,36 @@ namespace std::ranges {
198198
class join_view;
199199
}
200200
201+
namespace std {
202+
namespace views = ranges::views;
203+
204+
template<class T> struct tuple_size;
205+
template<size_t I, class T> struct tuple_element;
206+
207+
template<class I, class S, ranges::subrange_kind K>
208+
struct tuple_size<ranges::subrange<I, S, K>>
209+
: integral_constant<size_t, 2> {};
210+
211+
template<class I, class S, ranges::subrange_kind K>
212+
struct tuple_element<0, ranges::subrange<I, S, K>> {
213+
using type = I;
214+
};
215+
216+
template<class I, class S, ranges::subrange_kind K>
217+
struct tuple_element<1, ranges::subrange<I, S, K>> {
218+
using type = S;
219+
};
220+
221+
template<class I, class S, ranges::subrange_kind K>
222+
struct tuple_element<0, const ranges::subrange<I, S, K>> {
223+
using type = I;
224+
};
225+
226+
template<class I, class S, ranges::subrange_kind K>
227+
struct tuple_element<1, const ranges::subrange<I, S, K>> {
228+
using type = S;
229+
};
230+
}
201231
*/
202232

203233
// Make sure all feature-test macros are available.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
}

0 commit comments

Comments
 (0)