[libcxx-commits] [libcxx] [libc++] Simplify <tuple> further (PR #156351)

via libcxx-commits libcxx-commits at lists.llvm.org
Mon Sep 1 23:43:10 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Nikolas Klauser (philnik777)

<details>
<summary>Changes</summary>

This essentially inlines `__make_tuple_types` and simplifies the support code. This significantly simplifies the implementation, since `__make_tuple_types` has multiple features, but the different places that use it only make use of a subset of the features. Inlining it separates concerns better and leads to less code in total.

---
Full diff: https://github.com/llvm/llvm-project/pull/156351.diff


8 Files Affected:

- (modified) libcxx/include/CMakeLists.txt (-1) 
- (modified) libcxx/include/__fwd/tuple.h (+3) 
- (removed) libcxx/include/__tuple/make_tuple_types.h (-77) 
- (modified) libcxx/include/__tuple/tuple_element.h (-11) 
- (modified) libcxx/include/__tuple/tuple_like_ext.h (-11) 
- (modified) libcxx/include/__tuple/tuple_size.h (-3) 
- (modified) libcxx/include/module.modulemap.in (-1) 
- (modified) libcxx/include/tuple (+35-43) 


``````````diff
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 1f91b4828d4d3..f2f1ec50d5b75 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -778,7 +778,6 @@ set(files
   __tree
   __tuple/find_index.h
   __tuple/ignore.h
-  __tuple/make_tuple_types.h
   __tuple/sfinae_helpers.h
   __tuple/tuple_element.h
   __tuple/tuple_like.h
diff --git a/libcxx/include/__fwd/tuple.h b/libcxx/include/__fwd/tuple.h
index b3cac4e2a633d..dc96c03e2024f 100644
--- a/libcxx/include/__fwd/tuple.h
+++ b/libcxx/include/__fwd/tuple.h
@@ -21,6 +21,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 template <size_t, class>
 struct tuple_element;
 
+template <size_t _Np, class _Tp>
+using __tuple_element_t _LIBCPP_NODEBUG = typename tuple_element<_Np, _Tp>::type;
+
 #ifndef _LIBCPP_CXX03_LANG
 
 template <class...>
diff --git a/libcxx/include/__tuple/make_tuple_types.h b/libcxx/include/__tuple/make_tuple_types.h
deleted file mode 100644
index 3c22ec85dc9c7..0000000000000
--- a/libcxx/include/__tuple/make_tuple_types.h
+++ /dev/null
@@ -1,77 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef _LIBCPP___TUPLE_MAKE_TUPLE_TYPES_H
-#define _LIBCPP___TUPLE_MAKE_TUPLE_TYPES_H
-
-#include <__config>
-#include <__cstddef/size_t.h>
-#include <__fwd/array.h>
-#include <__fwd/tuple.h>
-#include <__tuple/tuple_element.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
-#endif
-
-#ifndef _LIBCPP_CXX03_LANG
-
-_LIBCPP_BEGIN_NAMESPACE_STD
-
-// __make_tuple_types<_Tuple<_Types...>, _Ep, _Sp>::type is a
-// __tuple_types<_Types...> using only those _Types in the range [_Sp, _Ep).
-// _Sp defaults to 0 and _Ep defaults to tuple_size<_Tuple>.  If _Tuple is a
-// lvalue_reference type, then __tuple_types<_Types&...> is the result.
-
-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...>, __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>, __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>
-struct __make_tuple_types {
-  using _RawTp _LIBCPP_NODEBUG = __remove_cvref_t<_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>
-struct __make_tuple_types<tuple<_Types...>> {
-  using type _LIBCPP_NODEBUG = __tuple_types<_Types...>;
-};
-
-template <class... _Types>
-struct __make_tuple_types<__tuple_types<_Types...>> {
-  using type _LIBCPP_NODEBUG = __tuple_types<_Types...>;
-};
-
-_LIBCPP_END_NAMESPACE_STD
-
-#endif // _LIBCPP_CXX03_LANG
-
-#endif // _LIBCPP___TUPLE_MAKE_TUPLE_TYPES_H
diff --git a/libcxx/include/__tuple/tuple_element.h b/libcxx/include/__tuple/tuple_element.h
index 607ac3a453de5..50a98079ccf89 100644
--- a/libcxx/include/__tuple/tuple_element.h
+++ b/libcxx/include/__tuple/tuple_element.h
@@ -11,7 +11,6 @@
 
 #include <__config>
 #include <__cstddef/size_t.h>
-#include <__tuple/tuple_types.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -37,21 +36,11 @@ struct tuple_element<_Ip, const volatile _Tp> {
   using type _LIBCPP_NODEBUG = const volatile typename tuple_element<_Ip, _Tp>::type;
 };
 
-#ifndef _LIBCPP_CXX03_LANG
-
-template <size_t _Ip, class... _Types>
-struct tuple_element<_Ip, __tuple_types<_Types...> > {
-  static_assert(_Ip < sizeof...(_Types), "tuple_element index out of range");
-  using type _LIBCPP_NODEBUG = __type_pack_element<_Ip, _Types...>;
-};
-
 #  if _LIBCPP_STD_VER >= 14
 template <size_t _Ip, class... _Tp>
 using tuple_element_t _LIBCPP_NODEBUG = typename tuple_element<_Ip, _Tp...>::type;
 #  endif
 
-#endif // _LIBCPP_CXX03_LANG
-
 _LIBCPP_END_NAMESPACE_STD
 
 #endif // _LIBCPP___TUPLE_TUPLE_ELEMENT_H
diff --git a/libcxx/include/__tuple/tuple_like_ext.h b/libcxx/include/__tuple/tuple_like_ext.h
index 45c0e65d62ff3..5a6748a9cc79d 100644
--- a/libcxx/include/__tuple/tuple_like_ext.h
+++ b/libcxx/include/__tuple/tuple_like_ext.h
@@ -14,7 +14,6 @@
 #include <__fwd/array.h>
 #include <__fwd/pair.h>
 #include <__fwd/tuple.h>
-#include <__tuple/tuple_types.h>
 #include <__type_traits/integral_constant.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -26,13 +25,6 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 template <class _Tp>
 struct __tuple_like_ext : false_type {};
 
-template <class _Tp>
-struct __tuple_like_ext<const _Tp> : public __tuple_like_ext<_Tp> {};
-template <class _Tp>
-struct __tuple_like_ext<volatile _Tp> : public __tuple_like_ext<_Tp> {};
-template <class _Tp>
-struct __tuple_like_ext<const volatile _Tp> : public __tuple_like_ext<_Tp> {};
-
 #ifndef _LIBCPP_CXX03_LANG
 template <class... _Tp>
 struct __tuple_like_ext<tuple<_Tp...> > : true_type {};
@@ -44,9 +36,6 @@ struct __tuple_like_ext<pair<_T1, _T2> > : true_type {};
 template <class _Tp, size_t _Size>
 struct __tuple_like_ext<array<_Tp, _Size> > : true_type {};
 
-template <class... _Tp>
-struct __tuple_like_ext<__tuple_types<_Tp...> > : true_type {};
-
 _LIBCPP_END_NAMESPACE_STD
 
 #endif // _LIBCPP___TUPLE_TUPLE_LIKE_EXT_H
diff --git a/libcxx/include/__tuple/tuple_size.h b/libcxx/include/__tuple/tuple_size.h
index 3308c000dc117..60f2a667a1ba3 100644
--- a/libcxx/include/__tuple/tuple_size.h
+++ b/libcxx/include/__tuple/tuple_size.h
@@ -59,9 +59,6 @@ struct tuple_size<const volatile _Tp> : public tuple_size<_Tp> {};
 template <class... _Tp>
 struct tuple_size<tuple<_Tp...> > : public integral_constant<size_t, sizeof...(_Tp)> {};
 
-template <class... _Tp>
-struct tuple_size<__tuple_types<_Tp...> > : public integral_constant<size_t, sizeof...(_Tp)> {};
-
 #  if _LIBCPP_STD_VER >= 17
 template <class _Tp>
 inline constexpr size_t tuple_size_v = tuple_size<_Tp>::value;
diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in
index c50c4dd73d4bb..1d2d51275704b 100644
--- a/libcxx/include/module.modulemap.in
+++ b/libcxx/include/module.modulemap.in
@@ -2109,7 +2109,6 @@ module std [system] {
   module tuple {
     module find_index               { header "__tuple/find_index.h" }
     module ignore                   { header "__tuple/ignore.h" }
-    module make_tuple_types         { header "__tuple/make_tuple_types.h" }
     module sfinae_helpers           { header "__tuple/sfinae_helpers.h" }
     module tuple_element            { header "__tuple/tuple_element.h" }
     module tuple_like_ext           { header "__tuple/tuple_like_ext.h" }
diff --git a/libcxx/include/tuple b/libcxx/include/tuple
index d2cc0f55cb2c8..b0d0c38b115a2 100644
--- a/libcxx/include/tuple
+++ b/libcxx/include/tuple
@@ -232,7 +232,6 @@ template <class... Types>
 #  include <__memory/uses_allocator.h>
 #  include <__tuple/find_index.h>
 #  include <__tuple/ignore.h>
-#  include <__tuple/make_tuple_types.h>
 #  include <__tuple/tuple_element.h>
 #  include <__tuple/tuple_like.h>
 #  include <__tuple/tuple_like_ext.h>
@@ -534,11 +533,10 @@ struct _LIBCPP_DECLSPEC_EMPTY_BASES
 
   template <class _Tuple>
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __tuple_impl(__from_tuple, _Tuple&& __t) noexcept(
-      (__all<is_nothrow_constructible<
-           _Tp,
-           typename tuple_element<_Indx, typename __make_tuple_types<_Tuple>::type>::type>::value...>::value))
+      (__all<is_nothrow_constructible<_Tp, __copy_cvref_t<_Tuple, __tuple_element_t<_Indx, __remove_cvref_t<_Tuple>>>>::
+                 value...>::value))
       : __tuple_leaf<_Indx, _Tp>(
-            std::forward<typename tuple_element<_Indx, typename __make_tuple_types<_Tuple>::type>::type>(
+            std::forward<__copy_cvref_t<_Tuple, __tuple_element_t<_Indx, __remove_cvref_t<_Tuple>>>>(
                 std::get<_Indx>(__t)))... {}
 
   template <class _Alloc, class _Tuple>
@@ -547,9 +545,9 @@ struct _LIBCPP_DECLSPEC_EMPTY_BASES
       : __tuple_leaf<_Indx, _Tp>(
             __uses_alloc_ctor<_Tp,
                               _Alloc,
-                              typename tuple_element<_Indx, typename __make_tuple_types<_Tuple>::type>::type>(),
+                              __copy_cvref_t<_Tuple, __tuple_element_t<_Indx, __remove_cvref_t<_Tuple>>>>(),
             __a,
-            std::forward<typename tuple_element<_Indx, typename __make_tuple_types<_Tuple>::type>::type>(
+            std::forward<__copy_cvref_t<_Tuple, __tuple_element_t<_Indx, __remove_cvref_t<_Tuple>>>>(
                 std::get<_Indx>(__t)))... {}
 
   __tuple_impl(const __tuple_impl&) = default;
@@ -1272,47 +1270,37 @@ operator<=(const tuple<_Tp...>& __x, const tuple<_Up...>& __y) {
 
 // tuple_cat
 
-template <class _Tp, class _Up>
-struct __tuple_cat_type;
+template <class... _Tuples>
+struct __tuple_cat_return_impl;
 
-template <class... _Ttypes, class... _Utypes>
-struct __tuple_cat_type<tuple<_Ttypes...>, __tuple_types<_Utypes...> > {
-  using type _LIBCPP_NODEBUG = tuple<_Ttypes..., _Utypes...>;
+template <class _Tuple>
+struct __tuple_cat_return_impl<_Tuple> {
+  using type _LIBCPP_NODEBUG = _Tuple;
 };
 
-template <class _ResultTuple, bool _Is_Tuple0TupleLike, class... _Tuples>
-struct __tuple_cat_return_1 {};
+template <class... _Types0, template <class...> class _Tuple, class... _Types1, class... _Tuples>
+struct __tuple_cat_return_impl<tuple<_Types0...>, _Tuple<_Types1...>, _Tuples...>
+    : __tuple_cat_return_impl<tuple<_Types0..., _Types1...>, _Tuples...> {};
 
-template <class... _Types, class _Tuple0>
-struct __tuple_cat_return_1<tuple<_Types...>, true, _Tuple0> {
-  using type _LIBCPP_NODEBUG =
-      typename __tuple_cat_type< tuple<_Types...>,
-                                 typename __make_tuple_types<__remove_cvref_t<_Tuple0> >::type >::type;
-};
+template <class, class, class>
+struct __tuple_cat_array;
 
-template <class... _Types, class _Tuple0, class _Tuple1, class... _Tuples>
-struct __tuple_cat_return_1<tuple<_Types...>, true, _Tuple0, _Tuple1, _Tuples...>
-    : public __tuple_cat_return_1<
-          typename __tuple_cat_type< tuple<_Types...>,
-                                     typename __make_tuple_types<__remove_cvref_t<_Tuple0> >::type >::type,
-          __tuple_like_ext<__libcpp_remove_reference_t<_Tuple1> >::value,
-          _Tuple1,
-          _Tuples...> {};
+template <class... _Types, class _ValueT, size_t... _Indices>
+struct __tuple_cat_array<tuple<_Types...>, _ValueT, __index_sequence<_Indices...>> {
+  template <size_t>
+  using __value_type _LIBCPP_NODEBUG = _ValueT;
 
-template <class... _Tuples>
-struct __tuple_cat_return;
+  using type _LIBCPP_NODEBUG = tuple<_Types..., __value_type<_Indices>...>;
+};
 
-template <class _Tuple0, class... _Tuples>
-struct __tuple_cat_return<_Tuple0, _Tuples...>
-    : public __tuple_cat_return_1<tuple<>,
-                                  __tuple_like_ext<__libcpp_remove_reference_t<_Tuple0> >::value,
-                                  _Tuple0,
-                                  _Tuples...> {};
+template <class... _Types, class _ValueT, size_t _Np, class... _Tuples>
+struct __tuple_cat_return_impl<tuple<_Types...>, array<_ValueT, _Np>, _Tuples...>
+    : __tuple_cat_return_impl<typename __tuple_cat_array<tuple<_Types...>, _ValueT, __make_index_sequence<_Np>>::type,
+                              _Tuples...> {};
 
-template <>
-struct __tuple_cat_return<> {
-  using type _LIBCPP_NODEBUG = tuple<>;
-};
+template <class... _Tuples>
+using __tuple_cat_return_t _LIBCPP_NODEBUG =
+    typename __tuple_cat_return_impl<tuple<>, __remove_cvref_t<_Tuples>...>::type;
 
 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 tuple<> tuple_cat() { return tuple<>(); }
 
@@ -1381,11 +1369,15 @@ __tuple_cat_select_element_wise(_TupleSrc&& __src, __index_sequence<_Indices...>
   return _TupleDst(std::get<_Indices>(std::forward<_TupleSrc>(__src))...);
 }
 
-template <class _Tuple0, class... _Tuples>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename __tuple_cat_return<_Tuple0, _Tuples...>::type
+template <class _Tuple0,
+          class... _Tuples,
+          __enable_if_t<
+              _And<__tuple_like_ext<__remove_cvref_t<_Tuple0>>, __tuple_like_ext<__remove_cvref_t<_Tuples>>...>::value,
+              int> = 0>
+inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __tuple_cat_return_t<_Tuple0, _Tuples...>
 tuple_cat(_Tuple0&& __t0, _Tuples&&... __tpls) {
   using _T0 _LIBCPP_NODEBUG          = __libcpp_remove_reference_t<_Tuple0>;
-  using _TRet _LIBCPP_NODEBUG        = typename __tuple_cat_return<_Tuple0, _Tuples...>::type;
+  using _TRet _LIBCPP_NODEBUG        = __tuple_cat_return_t<_Tuple0, _Tuples...>;
   using _T0Indices _LIBCPP_NODEBUG   = __make_index_sequence<tuple_size<_T0>::value>;
   using _TRetIndices _LIBCPP_NODEBUG = __make_index_sequence<tuple_size<_TRet>::value>;
   return std::__tuple_cat_select_element_wise<_TRet>(

``````````

</details>


https://github.com/llvm/llvm-project/pull/156351


More information about the libcxx-commits mailing list