Skip to content

[libc++] Refactor internal index_sequence API to match the public one #149475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion libcxx/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,6 @@ set(files
__tuple/make_tuple_types.h
__tuple/sfinae_helpers.h
__tuple/tuple_element.h
__tuple/tuple_indices.h
__tuple/tuple_like.h
__tuple/tuple_like_ext.h
__tuple/tuple_like_no_subrange.h
Expand Down
21 changes: 13 additions & 8 deletions libcxx/include/__functional/bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,14 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp& __mu(reference_w

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, __tuple_indices<_Indx...>) {
__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) {
typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
return std::__mu_expand(__ti, __uj, __indices());
return std::__mu_expand(__ti, __uj, __make_index_sequence<sizeof...(_Uj)>());
}

template <bool _IsPh, class _Ti, class _Uj>
Expand Down Expand Up @@ -191,7 +190,7 @@ struct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj, true> {

template <class _Fp, class _BoundArgs, size_t... _Indx, class _Args>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __bind_return<_Fp, _BoundArgs, _Args>::type
__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>, _Args&& __args) {
__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __index_sequence<_Indx...>, _Args&& __args) {
return std::__invoke(__f, std::__mu(std::get<_Indx>(__bound_args), __args)...);
}

Expand All @@ -205,8 +204,6 @@ class __bind : public __weak_result_type<__decay_t<_Fp> > {
_Fd __f_;
_Td __bound_args_;

typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;

public:
template <
class _Gp,
Expand All @@ -219,14 +216,22 @@ class __bind : public __weak_result_type<__decay_t<_Fp> > {
template <class... _Args>
_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_, __indices(), tuple<_Args&&...>(std::forward<_Args>(__args)...));
return std::__apply_functor(
__f_,
__bound_args_,
__make_index_sequence<sizeof...(_BoundArgs)>(),
tuple<_Args&&...>(std::forward<_Args>(__args)...));
}

template <class... _Args>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type
operator()(_Args&&... __args) const {
return std::__apply_functor(__f_, __bound_args_, __indices(), tuple<_Args&&...>(std::forward<_Args>(__args)...));
return std::__apply_functor(
__f_,
__bound_args_,
__make_index_sequence<sizeof...(_BoundArgs)>(),
tuple<_Args&&...>(std::forward<_Args>(__args)...));
}
};

Expand Down
10 changes: 5 additions & 5 deletions libcxx/include/__memory_resource/polymorphic_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ class _LIBCPP_AVAILABILITY_PMR polymorphic_allocator {
piecewise_construct,
__transform_tuple(typename __uses_alloc_ctor< _T1, polymorphic_allocator&, _Args1... >::type(),
std::move(__x),
typename __make_tuple_indices<sizeof...(_Args1)>::type{}),
make_index_sequence<sizeof...(_Args1)>()),
__transform_tuple(typename __uses_alloc_ctor< _T2, polymorphic_allocator&, _Args2... >::type(),
std::move(__y),
typename __make_tuple_indices<sizeof...(_Args2)>::type{}));
make_index_sequence<sizeof...(_Args2)>()));
}

template <class _T1, class _T2>
Expand Down Expand Up @@ -194,20 +194,20 @@ class _LIBCPP_AVAILABILITY_PMR polymorphic_allocator {
private:
template <class... _Args, size_t... _Is>
_LIBCPP_HIDE_FROM_ABI tuple<_Args&&...>
__transform_tuple(integral_constant<int, 0>, tuple<_Args...>&& __t, __tuple_indices<_Is...>) {
__transform_tuple(integral_constant<int, 0>, tuple<_Args...>&& __t, index_sequence<_Is...>) {
return std::forward_as_tuple(std::get<_Is>(std::move(__t))...);
}

template <class... _Args, size_t... _Is>
_LIBCPP_HIDE_FROM_ABI tuple<allocator_arg_t const&, polymorphic_allocator&, _Args&&...>
__transform_tuple(integral_constant<int, 1>, tuple<_Args...>&& __t, __tuple_indices<_Is...>) {
__transform_tuple(integral_constant<int, 1>, tuple<_Args...>&& __t, index_sequence<_Is...>) {
using _Tup = tuple<allocator_arg_t const&, polymorphic_allocator&, _Args&&...>;
return _Tup(allocator_arg, *this, std::get<_Is>(std::move(__t))...);
}

template <class... _Args, size_t... _Is>
_LIBCPP_HIDE_FROM_ABI tuple<_Args&&..., polymorphic_allocator&>
__transform_tuple(integral_constant<int, 2>, tuple<_Args...>&& __t, __tuple_indices<_Is...>) {
__transform_tuple(integral_constant<int, 2>, tuple<_Args...>&& __t, index_sequence<_Is...>) {
using _Tup = tuple<_Args&&..., polymorphic_allocator&>;
return _Tup(std::get<_Is>(std::move(__t))..., *this);
}
Expand Down
11 changes: 4 additions & 7 deletions libcxx/include/__mutex/once_flag.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
#include <__functional/invoke.h>
#include <__memory/addressof.h>
#include <__memory/shared_count.h> // __libcpp_acquire_load
#include <__tuple/tuple_indices.h>
#include <__tuple/tuple_size.h>
#include <__utility/forward.h>
#include <__utility/integer_sequence.h>
#include <__utility/move.h>
#include <cstdint>
#ifndef _LIBCPP_CXX03_LANG
Expand Down Expand Up @@ -87,15 +87,12 @@ class __call_once_param {
public:
_LIBCPP_HIDE_FROM_ABI explicit __call_once_param(_Fp& __f) : __f_(__f) {}

_LIBCPP_HIDE_FROM_ABI void operator()() {
typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
__execute(_Index());
}
_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(__tuple_indices<_Indices...>) {
std::__invoke(std::get<0>(std::move(__f_)), std::get<_Indices>(std::move(__f_))...);
_LIBCPP_HIDE_FROM_ABI void __execute(__index_sequence<_Indices...>) {
std::__invoke(std::get<_Indices>(std::move(__f_))...);
}
};

Expand Down
7 changes: 3 additions & 4 deletions libcxx/include/__thread/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,16 @@ operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id) {
# ifndef _LIBCPP_CXX03_LANG

template <class _TSp, class _Fp, class... _Args, size_t... _Indices>
inline _LIBCPP_HIDE_FROM_ABI void __thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __tuple_indices<_Indices...>) {
std::__invoke(std::move(std::get<1>(__t)), std::move(std::get<_Indices>(__t))...);
inline _LIBCPP_HIDE_FROM_ABI void __thread_execute(tuple<_TSp, _Fp, _Args...>& __t, __index_sequence<_Indices...>) {
std::__invoke(std::move(std::get<_Indices + 1>(__t))...);
}

template <class _Fp>
_LIBCPP_HIDE_FROM_ABI void* __thread_proxy(void* __vp) {
// _Fp = tuple< unique_ptr<__thread_struct>, Functor, Args...>
unique_ptr<_Fp> __p(static_cast<_Fp*>(__vp));
__thread_local_data().set_pointer(std::get<0>(*__p.get()).release());
typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 2>::type _Index;
std::__thread_execute(*__p.get(), _Index());
std::__thread_execute(*__p.get(), __make_index_sequence<tuple_size<_Fp>::value - 1>());
return nullptr;
}

Expand Down
25 changes: 11 additions & 14 deletions libcxx/include/__tuple/make_tuple_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
#include <__fwd/array.h>
#include <__fwd/tuple.h>
#include <__tuple/tuple_element.h>
#include <__tuple/tuple_indices.h>
#include <__tuple/tuple_size.h>
#include <__tuple/tuple_types.h>
#include <__type_traits/copy_cvref.h>
#include <__type_traits/remove_cvref.h>
#include <__type_traits/remove_reference.h>
#include <__utility/integer_sequence.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
Expand All @@ -38,38 +38,35 @@ template <class _TupleTypes, class _TupleIndices>
struct __make_tuple_types_flat;

template <template <class...> class _Tuple, class... _Types, size_t... _Idx>
struct __make_tuple_types_flat<_Tuple<_Types...>, __tuple_indices<_Idx...>> {
struct __make_tuple_types_flat<_Tuple<_Types...>, __index_sequence<_Idx...>> {
// Specialization for pair, tuple, and __tuple_types
template <class _Tp>
using __apply_quals _LIBCPP_NODEBUG = __tuple_types<__copy_cvref_t<_Tp, __type_pack_element<_Idx, _Types...>>...>;
};

template <class _Vt, size_t _Np, size_t... _Idx>
struct __make_tuple_types_flat<array<_Vt, _Np>, __tuple_indices<_Idx...>> {
struct __make_tuple_types_flat<array<_Vt, _Np>, __index_sequence<_Idx...>> {
template <size_t>
using __value_type _LIBCPP_NODEBUG = _Vt;
template <class _Tp>
using __apply_quals _LIBCPP_NODEBUG = __tuple_types<__copy_cvref_t<_Tp, __value_type<_Idx>>...>;
};

template <class _Tp,
size_t _Ep = tuple_size<__libcpp_remove_reference_t<_Tp> >::value,
size_t _Sp = 0,
bool _SameSize = (_Ep == tuple_size<__libcpp_remove_reference_t<_Tp> >::value)>
template <class _Tp>
struct __make_tuple_types {
static_assert(_Sp <= _Ep, "__make_tuple_types input error");
using _RawTp _LIBCPP_NODEBUG = __remove_cvref_t<_Tp>;
using _Maker _LIBCPP_NODEBUG = __make_tuple_types_flat<_RawTp, typename __make_tuple_indices<_Ep, _Sp>::type>;
using type _LIBCPP_NODEBUG = typename _Maker::template __apply_quals<_Tp>;
using _Maker _LIBCPP_NODEBUG =
__make_tuple_types_flat<_RawTp, __make_index_sequence<tuple_size<__libcpp_remove_reference_t<_Tp>>::value>>;
using type _LIBCPP_NODEBUG = typename _Maker::template __apply_quals<_Tp>;
};

template <class... _Types, size_t _Ep>
struct __make_tuple_types<tuple<_Types...>, _Ep, 0, true> {
template <class... _Types>
struct __make_tuple_types<tuple<_Types...>> {
using type _LIBCPP_NODEBUG = __tuple_types<_Types...>;
};

template <class... _Types, size_t _Ep>
struct __make_tuple_types<__tuple_types<_Types...>, _Ep, 0, true> {
template <class... _Types>
struct __make_tuple_types<__tuple_types<_Types...>> {
using type _LIBCPP_NODEBUG = __tuple_types<_Types...>;
};

Expand Down
1 change: 0 additions & 1 deletion libcxx/include/__tuple/tuple_element.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include <__config>
#include <__cstddef/size_t.h>
#include <__tuple/tuple_indices.h>
#include <__tuple/tuple_types.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Expand Down
37 changes: 0 additions & 37 deletions libcxx/include/__tuple/tuple_indices.h

This file was deleted.

68 changes: 27 additions & 41 deletions libcxx/include/__utility/integer_sequence.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,74 +17,60 @@
# pragma GCC system_header
#endif

#ifndef _LIBCPP_CXX03_LANG

_LIBCPP_BEGIN_NAMESPACE_STD

template <size_t...>
struct __tuple_indices;
# if __has_builtin(__make_integer_seq)
template <template <class _Tp, _Tp...> class _BaseType, class _Tp, _Tp _SequenceSize>
using __make_integer_sequence_impl _LIBCPP_NODEBUG = __make_integer_seq<_BaseType, _Tp, _SequenceSize>;
# else
template <template <class _Tp, _Tp...> class _BaseType, class _Tp, _Tp _SequenceSize>
using __make_integer_sequence_impl _LIBCPP_NODEBUG = _BaseType<_Tp, __integer_pack(_SequenceSize)...>;
# endif

template <class _IdxType, _IdxType... _Values>
template <class _Tp, _Tp... _Indices>
struct __integer_sequence {
template <template <class _OIdxType, _OIdxType...> class _ToIndexSeq, class _ToIndexType>
using __convert _LIBCPP_NODEBUG = _ToIndexSeq<_ToIndexType, _Values...>;

template <size_t _Sp>
using __to_tuple_indices _LIBCPP_NODEBUG = __tuple_indices<(_Values + _Sp)...>;
using value_type = _Tp;
static_assert(is_integral<_Tp>::value, "std::integer_sequence can only be instantiated with an integral type");
static _LIBCPP_HIDE_FROM_ABI constexpr size_t size() noexcept { return sizeof...(_Indices); }
};

#if __has_builtin(__make_integer_seq)
template <size_t _Ep, size_t _Sp>
using __make_indices_imp _LIBCPP_NODEBUG =
typename __make_integer_seq<__integer_sequence, size_t, _Ep - _Sp>::template __to_tuple_indices<_Sp>;
#elif __has_builtin(__integer_pack)
template <size_t _Ep, size_t _Sp>
using __make_indices_imp _LIBCPP_NODEBUG =
typename __integer_sequence<size_t, __integer_pack(_Ep - _Sp)...>::template __to_tuple_indices<_Sp>;
#else
# error "No known way to get an integer pack from the compiler"
#endif
template <size_t... _Indices>
using __index_sequence _LIBCPP_NODEBUG = __integer_sequence<size_t, _Indices...>;

#if _LIBCPP_STD_VER >= 14
template <size_t _SequenceSize>
using __make_index_sequence _LIBCPP_NODEBUG = __make_integer_sequence_impl<__integer_sequence, size_t, _SequenceSize>;

template <class _Tp, _Tp... _Ip>
struct integer_sequence {
typedef _Tp value_type;
static_assert(is_integral<_Tp>::value, "std::integer_sequence can only be instantiated with an integral type");
static _LIBCPP_HIDE_FROM_ABI constexpr size_t size() noexcept { return sizeof...(_Ip); }
};
# if _LIBCPP_STD_VER >= 14

template <class _Tp, _Tp... _Indices>
struct integer_sequence : __integer_sequence<_Tp, _Indices...> {};

template <size_t... _Ip>
using index_sequence = integer_sequence<size_t, _Ip...>;

# if __has_builtin(__make_integer_seq)

template <class _Tp, _Tp _Ep>
using make_integer_sequence _LIBCPP_NODEBUG = __make_integer_seq<integer_sequence, _Tp, _Ep>;

# elif __has_builtin(__integer_pack)

template <class _Tp, _Tp _SequenceSize>
using make_integer_sequence _LIBCPP_NODEBUG = integer_sequence<_Tp, __integer_pack(_SequenceSize)...>;

# else
# error "No known way to get an integer pack from the compiler"
# endif
using make_integer_sequence _LIBCPP_NODEBUG = __make_integer_sequence_impl<integer_sequence, _Tp, _Ep>;

template <size_t _Np>
using make_index_sequence = make_integer_sequence<size_t, _Np>;

template <class... _Tp>
using index_sequence_for = make_index_sequence<sizeof...(_Tp)>;

# if _LIBCPP_STD_VER >= 20
# if _LIBCPP_STD_VER >= 20
// Executes __func for every element in an index_sequence.
template <size_t... _Index, class _Function>
_LIBCPP_HIDE_FROM_ABI constexpr void __for_each_index_sequence(index_sequence<_Index...>, _Function __func) {
(__func.template operator()<_Index>(), ...);
}
# endif // _LIBCPP_STD_VER >= 20
# endif // _LIBCPP_STD_VER >= 20

#endif // _LIBCPP_STD_VER >= 14
# endif // _LIBCPP_STD_VER >= 14

_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP_CXX03_LANG

#endif // _LIBCPP___UTILITY_INTEGER_SEQUENCE_H
Loading
Loading