Skip to content

Commit 2cb2d76

Browse files
[libc++][ranges] Ensure range access CPOs are provided in <iterator> (#151745)
Per [range.access.general]/1, these CPOs are also provided in `<iterator>`. Currently only some of them are provided via transitive inclusion when only `<iterator>` is included. Drive-by: Add an entry for `ranges::reserve_hint` in the general test file for CPOs.
1 parent bad02e3 commit 2cb2d76

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

libcxx/include/iterator

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,16 @@ template <class E> constexpr const E* data(initializer_list<E> il) noexcept;
737737
# include <compare>
738738
# include <concepts>
739739

740+
// [range.access.general]
741+
# if _LIBCPP_STD_VER >= 20
742+
# include <__ranges/access.h>
743+
# include <__ranges/data.h>
744+
# include <__ranges/empty.h>
745+
# include <__ranges/rbegin.h>
746+
# include <__ranges/rend.h>
747+
# include <__ranges/size.h>
748+
# endif
749+
740750
# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
741751
# pragma GCC system_header
742752
# endif

libcxx/test/std/library/description/conventions/customization.point.object/cpo.compile.pass.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ static_assert(test(std::ranges::rend, a));
8181
static_assert(test(std::ranges::size, a));
8282
static_assert(test(std::ranges::ssize, a));
8383

84+
#if TEST_STD_VER >= 26
85+
// static_assert(test(std::views::reserve_hint, a));
86+
#endif
87+
8488
// [range.factories]
8589
// views::empty<T> is not a CPO
8690
static_assert(test(std::views::iota, 1));
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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++20
10+
11+
// [range.access.general]/1:
12+
// In addition to being available via inclusion of the <ranges> header, the customization point objects in
13+
// [range.access] are available when the header <iterator> is included.
14+
15+
#include <iterator>
16+
#include <type_traits>
17+
18+
#include "test_macros.h"
19+
20+
template <class CPO, class... Args>
21+
constexpr void test(CPO& o, Args&&... args) {
22+
static_assert(std::is_const_v<CPO>);
23+
static_assert(std::is_class_v<CPO>);
24+
static_assert(std::is_trivially_copyable_v<CPO>);
25+
static_assert(std::is_trivially_default_constructible_v<CPO>);
26+
27+
auto p = o;
28+
using T = decltype(p);
29+
(void)o(args...); // to make sure the CPO can actually be used
30+
31+
// The type of a customization point object, ignoring cv-qualifiers, shall model semiregular.
32+
static_assert(std::semiregular<T>);
33+
34+
// The type T of a customization point object, ignoring cv-qualifiers, shall model...
35+
static_assert(std::invocable<T&, Args...>);
36+
static_assert(std::invocable<const T&, Args...>);
37+
static_assert(std::invocable<T, Args...>);
38+
static_assert(std::invocable<const T, Args...>);
39+
}
40+
41+
int a[10];
42+
43+
constexpr bool test() {
44+
test(std::ranges::begin, a);
45+
test(std::ranges::end, a);
46+
test(std::ranges::cbegin, a);
47+
test(std::ranges::cdata, a);
48+
test(std::ranges::cend, a);
49+
test(std::ranges::crbegin, a);
50+
test(std::ranges::crend, a);
51+
test(std::ranges::data, a);
52+
test(std::ranges::empty, a);
53+
test(std::ranges::rbegin, a);
54+
test(std::ranges::rend, a);
55+
test(std::ranges::size, a);
56+
test(std::ranges::ssize, a);
57+
58+
#if TEST_STD_VER >= 26
59+
// test(std::views::reserve_hint, a);
60+
#endif
61+
62+
return true;
63+
}
64+
65+
int main() {
66+
test();
67+
static_assert(test());
68+
}

0 commit comments

Comments
 (0)