Skip to content

Commit c374788

Browse files
author
Xiaoyang Liu
authored
[libc++][ranges] use static operator() for C++23 ranges (#86052)
## Abstract This pull request converts the `operator()` of all CPOs and niebloids related to C++23 ranges to `static`. ## Motivation In `libc++`, CPOs and niebloids are implemented as function objects. Currently, the `operator()` for such a function object is a `const`-qualified member function. This means that even if the function object is has no data members, an extra register is used to pass in the `this` pointer when calling `operator()`, unless the compiler can inline the function call. Declaraing `operator()` as `static` would optimize away the unnecessary `this` pointer passing for stateless function objects, since there is no object instance state that needs to be accessed. ## Reference - [P1169R4: static `operator()`](https://wg21.link/P1169R4)
1 parent b621269 commit c374788

File tree

6 files changed

+23
-24
lines changed

6 files changed

+23
-24
lines changed

libcxx/include/__algorithm/ranges_ends_with.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace ranges {
3939
namespace __ends_with {
4040
struct __fn {
4141
template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Pred, class _Proj1, class _Proj2>
42-
static _LIBCPP_HIDE_FROM_ABI constexpr bool __ends_with_fn_impl_bidirectional(
42+
_LIBCPP_HIDE_FROM_ABI static constexpr bool __ends_with_fn_impl_bidirectional(
4343
_Iter1 __first1,
4444
_Sent1 __last1,
4545
_Iter2 __first2,
@@ -56,7 +56,7 @@ struct __fn {
5656
}
5757

5858
template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Pred, class _Proj1, class _Proj2>
59-
static _LIBCPP_HIDE_FROM_ABI constexpr bool __ends_with_fn_impl(
59+
_LIBCPP_HIDE_FROM_ABI static constexpr bool __ends_with_fn_impl(
6060
_Iter1 __first1,
6161
_Sent1 __last1,
6262
_Iter2 __first2,
@@ -65,7 +65,7 @@ struct __fn {
6565
_Proj1& __proj1,
6666
_Proj2& __proj2) {
6767
if constexpr (std::bidirectional_iterator<_Sent1> && std::bidirectional_iterator<_Sent2> &&
68-
(!std::random_access_iterator<_Sent1>)&&(!std::random_access_iterator<_Sent2>)) {
68+
(!std::random_access_iterator<_Sent1>) && (!std::random_access_iterator<_Sent2>)) {
6969
return __ends_with_fn_impl_bidirectional(__first1, __last1, __first2, __last2, __pred, __proj1, __proj2);
7070

7171
} else {

libcxx/include/__algorithm/ranges_starts_with.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ struct __fn {
4242
class _Proj1 = identity,
4343
class _Proj2 = identity>
4444
requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
45-
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(
45+
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI static constexpr bool operator()(
4646
_Iter1 __first1,
4747
_Sent1 __last1,
4848
_Iter2 __first2,
4949
_Sent2 __last2,
5050
_Pred __pred = {},
5151
_Proj1 __proj1 = {},
52-
_Proj2 __proj2 = {}) const {
52+
_Proj2 __proj2 = {}) {
5353
return __mismatch::__fn::__go(
5454
std::move(__first1),
5555
std::move(__last1),
@@ -67,8 +67,8 @@ struct __fn {
6767
class _Proj1 = identity,
6868
class _Proj2 = identity>
6969
requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>, _Pred, _Proj1, _Proj2>
70-
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr bool operator()(
71-
_Range1&& __range1, _Range2&& __range2, _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) const {
70+
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI static constexpr bool
71+
operator()(_Range1&& __range1, _Range2&& __range2, _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) {
7272
return __mismatch::__fn::__go(
7373
ranges::begin(__range1),
7474
ranges::end(__range1),

libcxx/include/__ranges/as_rvalue_view.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,18 @@ namespace views {
111111
namespace __as_rvalue {
112112
struct __fn : __range_adaptor_closure<__fn> {
113113
template <class _Range>
114-
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range) const
115-
noexcept(noexcept(/**/ as_rvalue_view(std::forward<_Range>(__range))))
116-
-> decltype(/*--*/ as_rvalue_view(std::forward<_Range>(__range))) {
117-
return /*-------------*/ as_rvalue_view(std::forward<_Range>(__range));
114+
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI static constexpr auto
115+
operator()(_Range&& __range) noexcept(noexcept(as_rvalue_view(std::forward<_Range>(__range))))
116+
-> decltype(/*--------------------------*/ as_rvalue_view(std::forward<_Range>(__range))) {
117+
return /*---------------------------------*/ as_rvalue_view(std::forward<_Range>(__range));
118118
}
119119

120120
template <class _Range>
121121
requires same_as<range_rvalue_reference_t<_Range>, range_reference_t<_Range>>
122-
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range) const
123-
noexcept(noexcept(/**/ views::all(std::forward<_Range>(__range))))
124-
-> decltype(/*--*/ views::all(std::forward<_Range>(__range))) {
125-
return /*-------------*/ views::all(std::forward<_Range>(__range));
122+
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI static constexpr auto
123+
operator()(_Range&& __range) noexcept(noexcept(views::all(std::forward<_Range>(__range))))
124+
-> decltype(/*--------------------------*/ views::all(std::forward<_Range>(__range))) {
125+
return /*---------------------------------*/ views::all(std::forward<_Range>(__range));
126126
}
127127
};
128128
} // namespace __as_rvalue

libcxx/include/__ranges/repeat_view.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,13 @@ namespace views {
229229
namespace __repeat {
230230
struct __fn {
231231
template <class _Tp>
232-
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __value) const
232+
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Tp&& __value)
233233
noexcept(noexcept(ranges::repeat_view(std::forward<_Tp>(__value))))
234234
-> decltype( ranges::repeat_view(std::forward<_Tp>(__value)))
235235
{ return ranges::repeat_view(std::forward<_Tp>(__value)); }
236236

237-
238237
template <class _Tp, class _Bound>
239-
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __value, _Bound&& __bound_sentinel) const
238+
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Tp&& __value, _Bound&& __bound_sentinel)
240239
noexcept(noexcept(ranges::repeat_view(std::forward<_Tp>(__value), std::forward<_Bound>(__bound_sentinel))))
241240
-> decltype( ranges::repeat_view(std::forward<_Tp>(__value), std::forward<_Bound>(__bound_sentinel)))
242241
{ return ranges::repeat_view(std::forward<_Tp>(__value), std::forward<_Bound>(__bound_sentinel)); }

libcxx/include/__ranges/to.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto to(_Args&&... __args)
207207
static_assert(
208208
!is_volatile_v<_Container>, "The target container cannot be volatile-qualified, please remove the volatile");
209209

210-
auto __to_func = []<input_range _Range, class... _Tail>(_Range&& __range, _Tail&&... __tail)
210+
auto __to_func = []<input_range _Range, class... _Tail>(_Range&& __range, _Tail&&... __tail) static
211211
requires requires { //
212212
/**/ ranges::to<_Container>(std::forward<_Range>(__range), std::forward<_Tail>(__tail)...);
213213
}
@@ -223,7 +223,7 @@ _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto to(_Args&&... __args)
223223
// clang-format off
224224
auto __to_func = []<input_range _Range, class... _Tail,
225225
class _DeducedExpr = typename _Deducer<_Container, _Range, _Tail...>::type>
226-
(_Range&& __range, _Tail&& ... __tail)
226+
(_Range&& __range, _Tail&& ... __tail) static
227227
requires requires { //
228228
/**/ ranges::to<_DeducedExpr>(std::forward<_Range>(__range), std::forward<_Tail>(__tail)...);
229229
}

libcxx/include/__ranges/zip_view.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -489,12 +489,12 @@ namespace views {
489489
namespace __zip {
490490

491491
struct __fn {
492-
_LIBCPP_HIDE_FROM_ABI constexpr auto operator()() const noexcept { return empty_view<tuple<>>{}; }
492+
_LIBCPP_HIDE_FROM_ABI static constexpr auto operator()() noexcept { return empty_view<tuple<>>{}; }
493493

494494
template <class... _Ranges>
495-
_LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Ranges&&... __rs) const
496-
noexcept(noexcept(zip_view<all_t<_Ranges&&>...>(std::forward<_Ranges>(__rs)...)))
497-
-> decltype(zip_view<all_t<_Ranges&&>...>(std::forward<_Ranges>(__rs)...)) {
495+
_LIBCPP_HIDE_FROM_ABI static constexpr auto
496+
operator()(_Ranges&&... __rs) noexcept(noexcept(zip_view<all_t<_Ranges&&>...>(std::forward<_Ranges>(__rs)...)))
497+
-> decltype(zip_view<all_t<_Ranges&&>...>(std::forward<_Ranges>(__rs)...)) {
498498
return zip_view<all_t<_Ranges>...>(std::forward<_Ranges>(__rs)...);
499499
}
500500
};

0 commit comments

Comments
 (0)