Skip to content

Commit ba998d7

Browse files
committed
Add enable_borrowed_range<optional<T&>> and corresponding test
1 parent 4f58bf2 commit ba998d7

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

libcxx/include/optional

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ namespace std {
210210
# include <__iterator/wrap_iter.h>
211211
# include <__memory/addressof.h>
212212
# include <__memory/construct_at.h>
213+
# include <__ranges/enable_borrowed_range.h>
213214
# include <__ranges/enable_view.h>
214215
# include <__tuple/sfinae_helpers.h>
215216
# include <__type_traits/add_pointer.h>
@@ -594,6 +595,10 @@ constexpr bool ranges::enable_view<optional<_Tp>> = true;
594595

595596
template <class _Tp>
596597
constexpr range_format format_kind<optional<_Tp>> = range_format::disabled;
598+
599+
template <class _Tp>
600+
constexpr bool ranges::enable_borrowed_range<optional<_Tp&>> = true;
601+
597602
# endif
598603

599604
# if _LIBCPP_STD_VER >= 20
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
// REQUIRES: std-at-least-c++26
10+
11+
// <optional>
12+
13+
// template <class T> class optional<T&>::iterator;
14+
// template <class T> class optional<T&>::const_iterator;
15+
// template <class T>
16+
// constexpr bool ranges::enable_borrowed_range<optional<T&>> = true;
17+
18+
#include <cassert>
19+
#include <optional>
20+
#include <ranges>
21+
22+
template <typename T>
23+
constexpr bool enable_borrowed_range() {
24+
{
25+
assert(std::ranges::enable_borrowed_range<std::optional<T&>>);
26+
}
27+
return true;
28+
}
29+
30+
template <typename T>
31+
constexpr bool borrowed_range() {
32+
if (std::ranges::range<std::optional<T&>>) {
33+
assert(std::ranges::borrowed_range<std::optional<T&>>);
34+
} else {
35+
assert(!std::ranges::borrowed_range<std::optional<T&>>);
36+
return false;
37+
}
38+
39+
return true;
40+
}
41+
42+
constexpr bool test_enable_borrowed_range() {
43+
assert(enable_borrowed_range<int>());
44+
assert(enable_borrowed_range<const int>());
45+
assert(enable_borrowed_range<int[]>());
46+
assert(enable_borrowed_range<int[10]>());
47+
assert(enable_borrowed_range<int()>());
48+
49+
return true;
50+
}
51+
52+
constexpr bool test_borrowed_range() {
53+
assert(borrowed_range<int>());
54+
assert(borrowed_range<const int>());
55+
assert(!borrowed_range<int[]>());
56+
assert(!borrowed_range<int[10]>());
57+
assert(!borrowed_range<int()>());
58+
59+
return true;
60+
}
61+
62+
int main(int, char**) {
63+
{
64+
static_assert(test_enable_borrowed_range());
65+
assert(test_enable_borrowed_range());
66+
}
67+
68+
{
69+
static_assert(test_borrowed_range());
70+
assert(test_enable_borrowed_range());
71+
}
72+
73+
return 0;
74+
}

0 commit comments

Comments
 (0)