[libc++] Simplify a few places where we use __index_sequence (#167213)

This is done in two ways:
1) `index_sequence_for` is back-ported as `__index_sequence_for`
2) Extra functions just to expand the parameter pack are replaced with
lambdas
This commit is contained in:
Nikolas Klauser 2025-12-02 15:02:04 +01:00 committed by GitHub
parent cd5ed7ca87
commit c2a0350d86
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 30 additions and 48 deletions

View File

@ -81,16 +81,12 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& __mu(reference_w
return __t.get();
}
template <class _Ti, class... _Uj, size_t... _Indx>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __invoke_result_t<_Ti&, _Uj...>
__mu_expand(_Ti& __ti, tuple<_Uj...>& __uj, __index_sequence<_Indx...>) {
return __ti(std::forward<_Uj>(std::get<_Indx>(__uj))...);
}
template <class _Ti, class... _Uj, __enable_if_t<is_bind_expression<_Ti>::value, int> = 0>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __invoke_result_t<_Ti&, _Uj...>
__mu(_Ti& __ti, tuple<_Uj...>& __uj) {
return std::__mu_expand(__ti, __uj, __make_index_sequence<sizeof...(_Uj)>());
return [&]<size_t... _Indices>(__index_sequence<_Indices...>) -> __invoke_result_t<_Ti&, _Uj...> {
return __ti(std::forward<_Uj>(std::get<_Indices>(__uj))...);
}(__index_sequence_for<_Uj...>{});
}
template <bool _IsPh, class _Ti, class _Uj>
@ -217,10 +213,7 @@ public:
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type
operator()(_Args&&... __args) {
return std::__apply_functor(
__f_,
__bound_args_,
__make_index_sequence<sizeof...(_BoundArgs)>(),
tuple<_Args&&...>(std::forward<_Args>(__args)...));
__f_, __bound_args_, __index_sequence_for<_BoundArgs...>(), tuple<_Args&&...>(std::forward<_Args>(__args)...));
}
template <class... _Args>
@ -228,10 +221,7 @@ public:
typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type
operator()(_Args&&... __args) const {
return std::__apply_functor(
__f_,
__bound_args_,
__make_index_sequence<sizeof...(_BoundArgs)>(),
tuple<_Args&&...>(std::forward<_Args>(__args)...));
__f_, __bound_args_, __index_sequence_for<_BoundArgs...>(), tuple<_Args&&...>(std::forward<_Args>(__args)...));
}
};

View File

@ -86,12 +86,10 @@ class __call_once_param {
public:
_LIBCPP_HIDE_FROM_ABI explicit __call_once_param(_Fp& __f) : __f_(__f) {}
_LIBCPP_HIDE_FROM_ABI void operator()() { __execute(__make_index_sequence<tuple_size<_Fp>::value>()); }
private:
template <size_t... _Indices>
_LIBCPP_HIDE_FROM_ABI void __execute(__index_sequence<_Indices...>) {
std::__invoke(std::get<_Indices>(std::move(__f_))...);
_LIBCPP_HIDE_FROM_ABI void operator()() {
[&]<size_t... _Indices>(__index_sequence<_Indices...>) -> void {
std::__invoke(std::get<_Indices>(std::move(__f_))...);
}(__make_index_sequence<tuple_size<_Fp>::value>());
}
};

View File

@ -42,6 +42,9 @@ using __index_sequence _LIBCPP_NODEBUG = __integer_sequence<size_t, _Indices...>
template <size_t _SequenceSize>
using __make_index_sequence _LIBCPP_NODEBUG = __make_integer_sequence_impl<__integer_sequence, size_t, _SequenceSize>;
template <class... _Args>
using __index_sequence_for _LIBCPP_NODEBUG = __make_index_sequence<sizeof...(_Args)>;
# if _LIBCPP_STD_VER >= 14
template <class _Tp, _Tp... _Indices>

View File

@ -219,11 +219,7 @@ struct pair
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args, tuple<_Args2...> __second_args) noexcept(
is_nothrow_constructible<first_type, _Args1...>::value && is_nothrow_constructible<second_type, _Args2...>::value)
: pair(__pc,
__first_args,
__second_args,
__make_index_sequence<sizeof...(_Args1)>(),
__make_index_sequence<sizeof...(_Args2)>()) {}
: pair(__pc, __first_args, __second_args, __index_sequence_for<_Args1...>(), __index_sequence_for<_Args2...>()) {}
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 pair&
operator=(__conditional_t<is_copy_assignable<first_type>::value && is_copy_assignable<second_type>::value,

View File

@ -1833,12 +1833,10 @@ public:
_LIBCPP_HIDE_FROM_ABI __async_func(__async_func&& __f) : __f_(std::move(__f.__f_)) {}
_LIBCPP_HIDE_FROM_ABI _Rp operator()() { return __execute(__make_index_sequence<sizeof...(_Args) + 1>()); }
private:
template <size_t... _Indices>
_LIBCPP_HIDE_FROM_ABI _Rp __execute(__index_sequence<_Indices...>) {
return std::__invoke(std::move(std::get<_Indices>(__f_))...);
_LIBCPP_HIDE_FROM_ABI _Rp operator()() {
return [&]<size_t... _Indices>(__index_sequence<_Indices...>) -> _Rp {
return std::__invoke(std::move(std::get<_Indices>(__f_))...);
}(__index_sequence_for<_Fp, _Args...>{});
}
};

View File

@ -434,10 +434,10 @@ public:
piecewise_construct,
__transform_tuple(typename __uses_alloc_ctor< _T1, inner_allocator_type&, _Args1... >::type(),
std::move(__x),
__make_index_sequence<sizeof...(_Args1)>()),
__index_sequence_for<_Args1...>()),
__transform_tuple(typename __uses_alloc_ctor< _T2, inner_allocator_type&, _Args2... >::type(),
std::move(__y),
__make_index_sequence<sizeof...(_Args2)>()));
__index_sequence_for<_Args2...>()));
}
template <class _T1, class _T2>

View File

@ -576,7 +576,7 @@ __memberwise_forward_assign(_Dest& __dest, _Source&& __source, __type_list<_Up..
template <class... _Tp>
class _LIBCPP_NO_SPECIALIZATIONS tuple {
typedef __tuple_impl<__make_index_sequence<sizeof...(_Tp)>, _Tp...> _BaseT;
typedef __tuple_impl<__index_sequence_for<_Tp...>, _Tp...> _BaseT;
_BaseT __base_;
@ -858,7 +858,7 @@ public:
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 tuple&
operator=(_If<_And<is_copy_assignable<_Tp>...>::value, tuple, __nat> const& __tuple) noexcept(
_And<is_nothrow_copy_assignable<_Tp>...>::value) {
std::__memberwise_copy_assign(*this, __tuple, __make_index_sequence<sizeof...(_Tp)>());
std::__memberwise_copy_assign(*this, __tuple, __index_sequence_for<_Tp...>());
return *this;
}
@ -866,15 +866,14 @@ public:
_LIBCPP_HIDE_FROM_ABI constexpr const tuple& operator=(tuple const& __tuple) const
requires(_And<is_copy_assignable<const _Tp>...>::value)
{
std::__memberwise_copy_assign(*this, __tuple, __make_index_sequence<sizeof...(_Tp)>());
std::__memberwise_copy_assign(*this, __tuple, __index_sequence_for<_Tp...>());
return *this;
}
_LIBCPP_HIDE_FROM_ABI constexpr const tuple& operator=(tuple&& __tuple) const
requires(_And<is_assignable<const _Tp&, _Tp>...>::value)
{
std::__memberwise_forward_assign(
*this, std::move(__tuple), __type_list<_Tp...>(), __make_index_sequence<sizeof...(_Tp)>());
std::__memberwise_forward_assign(*this, std::move(__tuple), __type_list<_Tp...>(), __index_sequence_for<_Tp...>());
return *this;
}
# endif // _LIBCPP_STD_VER >= 23
@ -882,8 +881,7 @@ public:
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 tuple&
operator=(_If<_And<is_move_assignable<_Tp>...>::value, tuple, __nat>&& __tuple) noexcept(
_And<is_nothrow_move_assignable<_Tp>...>::value) {
std::__memberwise_forward_assign(
*this, std::move(__tuple), __type_list<_Tp...>(), __make_index_sequence<sizeof...(_Tp)>());
std::__memberwise_forward_assign(*this, std::move(__tuple), __type_list<_Tp...>(), __index_sequence_for<_Tp...>());
return *this;
}
@ -893,7 +891,7 @@ public:
int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 tuple&
operator=(tuple<_Up...> const& __tuple) noexcept(_And<is_nothrow_assignable<_Tp&, _Up const&>...>::value) {
std::__memberwise_copy_assign(*this, __tuple, __make_index_sequence<sizeof...(_Tp)>());
std::__memberwise_copy_assign(*this, __tuple, __index_sequence_for<_Tp...>());
return *this;
}
@ -902,8 +900,7 @@ public:
int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 tuple&
operator=(tuple<_Up...>&& __tuple) noexcept(_And<is_nothrow_assignable<_Tp&, _Up>...>::value) {
std::__memberwise_forward_assign(
*this, std::move(__tuple), __type_list<_Up...>(), __make_index_sequence<sizeof...(_Tp)>());
std::__memberwise_forward_assign(*this, std::move(__tuple), __type_list<_Up...>(), __index_sequence_for<_Tp...>());
return *this;
}
@ -912,7 +909,7 @@ public:
enable_if_t< _And<_BoolConstant<sizeof...(_Tp) == sizeof...(_UTypes)>,
is_assignable<const _Tp&, const _UTypes&>...>::value>* = nullptr>
_LIBCPP_HIDE_FROM_ABI constexpr const tuple& operator=(const tuple<_UTypes...>& __u) const {
std::__memberwise_copy_assign(*this, __u, __make_index_sequence<sizeof...(_Tp)>());
std::__memberwise_copy_assign(*this, __u, index_sequence_for<_Tp...>());
return *this;
}
@ -920,7 +917,7 @@ public:
enable_if_t< _And<_BoolConstant<sizeof...(_Tp) == sizeof...(_UTypes)>,
is_assignable<const _Tp&, _UTypes>...>::value>* = nullptr>
_LIBCPP_HIDE_FROM_ABI constexpr const tuple& operator=(tuple<_UTypes...>&& __u) const {
std::__memberwise_forward_assign(*this, __u, __type_list<_UTypes...>(), __make_index_sequence<sizeof...(_Tp)>());
std::__memberwise_forward_assign(*this, __u, __type_list<_UTypes...>(), index_sequence_for<_Tp...>());
return *this;
}
# endif // _LIBCPP_STD_VER >= 23
@ -986,7 +983,7 @@ public:
__enable_if_t< _And< _BoolConstant<_Np == sizeof...(_Tp)>, is_assignable<_Tp&, _Up const&>... >::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 tuple&
operator=(array<_Up, _Np> const& __array) noexcept(_And<is_nothrow_assignable<_Tp&, _Up const&>...>::value) {
std::__memberwise_copy_assign(*this, __array, __make_index_sequence<sizeof...(_Tp)>());
std::__memberwise_copy_assign(*this, __array, __index_sequence_for<_Tp...>());
return *this;
}
@ -998,7 +995,7 @@ public:
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 tuple&
operator=(array<_Up, _Np>&& __array) noexcept(_And<is_nothrow_assignable<_Tp&, _Up>...>::value) {
std::__memberwise_forward_assign(
*this, std::move(__array), __type_list<_If<true, _Up, _Tp>...>(), __make_index_sequence<sizeof...(_Tp)>());
*this, std::move(__array), __type_list<_If<true, _Up, _Tp>...>(), __index_sequence_for<_Tp...>());
return *this;
}