On Thu, Oct 29, 2026 at 6:18 PM Tomasz Kamiński <[email protected]> wrote:

> The __unpack_std_tuple is used in the noexcept specification std::apply
> and std::make_from_t, to determine if function is nothrow invocable or
> type is nothrow constructible respectively. This trait however was
> specialized only for tuple, and such functions were other consider
> potentially throwing from remaining tuple-like types.

The above should be replaced with:
This trait however was specialized only for tuple, and in consequence above
functions
were considered potentially throwing from remaining tuple-like types (pair,
array, ...).


> As the standard
> defines noexcept specification for std::apply this is non-conformance.
>
> This patch provides additional partial specializations for above trait
> for std::pair, std::array and std::complex, for which get is always
> noexcept. To reflect that the strait is renamed to __unpack_tuple_like_v.
>
> libstdc++-v3/ChangeLog:
>
>         * include/bits/stl_pair.h (std::__unpack_tuple_like_v): Moved
>         from <tuple> and renamed to std::__unpack_std_tuple. Additional
>         specialization defined for pair, array and complex.
>         * include/std/tuple (std::__unpack_tuple_like): Moved to
>         bits/stl_pair.h.
>         (std::apply, std::make_from_tuple): Update noexcept specification.
>         * testsuite/20_util/tuple/apply/1.cc: Add test for pair and array.
>         * testsuite/20_util/tuple/apply/2.cc: Likewise.
>         * testsuite/20_util/tuple/make_from_tuple/tuple_like.cc: Test
>         noexcept and constrains.
>         * testsuite/26_numerics/complex/tuple_like.cc: Likewise.
> ---
>  libstdc++-v3/include/bits/stl_pair.h          | 89 ++++++++++++++++++-
>  libstdc++-v3/include/std/tuple                | 29 +-----
>  .../testsuite/20_util/tuple/apply/1.cc        | 31 +++++++
>  .../testsuite/20_util/tuple/apply/2.cc        | 12 +++
>  .../tuple/make_from_tuple/tuple_like.cc       | 39 ++++++--
>  .../26_numerics/complex/tuple_like.cc         | 14 ++-
>  6 files changed, 178 insertions(+), 36 deletions(-)
>
> diff --git a/libstdc++-v3/include/bits/stl_pair.h
> b/libstdc++-v3/include/bits/stl_pair.h
> index ccff8691206..b18e1b74b77 100644
> --- a/libstdc++-v3/include/bits/stl_pair.h
> +++ b/libstdc++-v3/include/bits/stl_pair.h
> @@ -233,7 +233,94 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>        }
>      };
>  #endif // lib concepts
> -  /// @endcond
> +
> +#if defined(__glibcxx_apply) || defined(__glibcxx_make_from_tuple) // C++
> >= 17
> +  // Unpack tuple_like into a type trait and use its value.
> +  template<template<typename...> class _Trait, typename _Tp, typename
> _Tuple>
> +    constexpr bool __unpack_tuple_like_v = false;
> +
> +  // Unpack a std::tuple into a type trait and use its value.
> +  template<template<typename...> class _Trait, typename _Tp, typename...
> _Up>
> +    constexpr bool __unpack_tuple_like_v<_Trait, _Tp, tuple<_Up...>>
> +      = _Trait<_Tp, _Up...>::value;
> +
> +  template<template<typename...> class _Trait, typename _Tp, typename...
> _Up>
> +    constexpr bool __unpack_tuple_like_v<_Trait, _Tp, tuple<_Up...>&>
> +      = _Trait<_Tp, _Up&...>::value;
> +
> +  template<template<typename...> class _Trait, typename _Tp, typename...
> _Up>
> +    constexpr bool __unpack_tuple_like_v<_Trait, _Tp, const tuple<_Up...>>
> +      = _Trait<_Tp, const _Up...>::value;
> +
> +  template<template<typename...> class _Trait, typename _Tp, typename...
> _Up>
> +    constexpr bool __unpack_tuple_like_v<_Trait, _Tp, const
> tuple<_Up...>&>
> +      = _Trait<_Tp, const _Up&...>::value;
> +
> +  // Unpack a std::pair into a type trait and use its value.
> +  template<template<typename...> class _Trait, typename _Tp, typename
> _Fp, typename _Sp>
> +    constexpr bool __unpack_tuple_like_v<_Trait, _Tp, pair<_Fp, _Sp>>
> +      = _Trait<_Tp, _Fp, _Sp>::value;
> +
> +  template<template<typename...> class _Trait, typename _Tp, typename
> _Fp, typename _Sp>
> +    constexpr bool __unpack_tuple_like_v<_Trait, _Tp, pair<_Fp, _Sp>&>
> +      = _Trait<_Tp, _Fp&, _Sp&>::value;
> +
> +  template<template<typename...> class _Trait, typename _Tp, typename
> _Fp, typename _Sp>
> +    constexpr bool __unpack_tuple_like_v<_Trait, _Tp, const pair<_Fp,
> _Sp>>
> +      = _Trait<_Tp, const _Fp, const _Sp>::value;
> +
> +  template<template<typename...> class _Trait, typename _Tp, typename
> _Fp, typename _Sp>
> +    constexpr bool __unpack_tuple_like_v<_Trait, _Tp, const pair<_Fp,
> _Sp>&>
> +      = _Trait<_Tp, const _Fp&, const _Sp&>::value;
> +
> +  namespace __detail
> +  {
> +    template<template<typename...> class _Trait, typename _Tp, typename
> _Up, size_t... _Ids>
> +      constexpr bool
> +      __apply_repeated_arg(index_sequence<_Ids...>)
> +      { return _Trait<_Tp, decltype(((void)_Ids,
> std::declval<_Up>()))...>::value; }
> +  }
> +
> +  // Unpack a std::array into a type trait and use its value.
> +  template<template<typename...> class _Trait, typename _Tp, typename
> _Up, size_t _Nm>
> +    constexpr bool __unpack_tuple_like_v<_Trait, _Tp, array<_Up, _Nm>>
> +      = __detail::__apply_repeated_arg<_Trait, _Tp,
> _Up>(make_index_sequence<_Nm>{});
> +
> +  template<template<typename...> class _Trait, typename _Tp, typename
> _Up, size_t _Nm>
> +    constexpr bool __unpack_tuple_like_v<_Trait, _Tp, array<_Up, _Nm>&>
> +      = __detail::__apply_repeated_arg<_Trait, _Tp,
> _Up&>(make_index_sequence<_Nm>{});
> +
> +  template<template<typename...> class _Trait, typename _Tp, typename
> _Up, size_t _Nm>
> +    constexpr bool __unpack_tuple_like_v<_Trait, _Tp, const array<_Up,
> _Nm>>
> +      = __detail::__apply_repeated_arg<_Trait, _Tp, const
> _Up>(make_index_sequence<_Nm>{});
> +
> +  template<template<typename...> class _Trait, typename _Tp, typename
> _Up, size_t _Nm>
> +    constexpr bool __unpack_tuple_like_v<_Trait, _Tp, const array<_Up,
> _Nm>&>
> +      = __detail::__apply_repeated_arg<_Trait, _Tp, const
> _Up&>(make_index_sequence<_Nm>{});
> +#endif
> +
> +#if __glibcxx_tuple_like >= 202311 // >= C++26
> +  // Unpack a std::complex into type trait and use it value.
> +  // Assume that real()/imag() does not throw.
> +  template<template<typename...> class _Trait, typename _Tp, typename _Up>
> +    constexpr bool __unpack_tuple_like_v<_Trait, _Tp, complex<_Up>>
> +      = _Trait<_Tp, _Up, _Up>::value;
> +
> +  template<template<typename...> class _Trait, typename _Tp, typename _Up>
> +    constexpr bool __unpack_tuple_like_v<_Trait, _Tp, complex<_Up>&>
> +      = _Trait<_Tp, _Up&, _Up&>::value;
> +
> +  template<template<typename...> class _Trait, typename _Tp, typename _Up>
> +    constexpr bool __unpack_tuple_like_v<_Trait, _Tp, const complex<_Up>>
> +      = _Trait<_Tp, const _Up, const _Up>::value;
> +
> +  template<template<typename...> class _Trait, typename _Tp, typename _Up>
> +    constexpr bool __unpack_tuple_like_v<_Trait, _Tp, const complex<_Up>&>
> +      = _Trait<_Tp, const _Up&, const _Up&>::value;
> +#endif
> +
> +  // __is_tuple_like<subrange> is defined in <bits/ranges_util.h>.
> +/// @endcond
>  #endif // C++11
>
>    /// @cond undocumented
> diff --git a/libstdc++-v3/include/std/tuple
> b/libstdc++-v3/include/std/tuple
> index d73cefcbca8..7d6039ceb38 100644
> --- a/libstdc++-v3/include/std/tuple
> +++ b/libstdc++-v3/include/std/tuple
> @@ -2912,31 +2912,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>         second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
>        { }
>
> -#if defined(__cpp_lib_apply) || defined(__cpp_lib_make_from_tuple) // C++
> >= 17
> -  // Unpack a std::tuple into a type trait and use its value.
> -  // For cv std::tuple<_Up> the result is _Trait<_Tp, cv _Up...>::value.
> -  // For cv std::tuple<_Up>& the result is _Trait<_Tp, cv _Up&...>::value.
> -  // Otherwise the result is false (because we don't know if std::get
> throws).
> -  template<template<typename...> class _Trait, typename _Tp, typename
> _Tuple>
> -    inline constexpr bool __unpack_std_tuple = false;
> -
> -  template<template<typename...> class _Trait, typename _Tp, typename...
> _Up>
> -    inline constexpr bool __unpack_std_tuple<_Trait, _Tp, tuple<_Up...>>
> -      = _Trait<_Tp, _Up...>::value;
> -
> -  template<template<typename...> class _Trait, typename _Tp, typename...
> _Up>
> -    inline constexpr bool __unpack_std_tuple<_Trait, _Tp, tuple<_Up...>&>
> -      = _Trait<_Tp, _Up&...>::value;
> -
> -  template<template<typename...> class _Trait, typename _Tp, typename...
> _Up>
> -    inline constexpr bool __unpack_std_tuple<_Trait, _Tp, const
> tuple<_Up...>>
> -      = _Trait<_Tp, const _Up...>::value;
> -
> -  template<template<typename...> class _Trait, typename _Tp, typename...
> _Up>
> -    inline constexpr bool __unpack_std_tuple<_Trait, _Tp, const
> tuple<_Up...>&>
> -      = _Trait<_Tp, const _Up&...>::value;
> -#endif
> -
>  #ifdef __cpp_lib_apply // C++ >= 17
>    template <typename _Fn, typename _Tuple, size_t... _Idx>
>      constexpr decltype(auto)
> @@ -2953,7 +2928,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>  #endif
>      constexpr decltype(auto)
>      apply(_Fn&& __f, _Tuple&& __t)
> -    noexcept(__unpack_std_tuple<is_nothrow_invocable, _Fn, _Tuple>)
> +    noexcept(__unpack_tuple_like_v<is_nothrow_invocable, _Fn, _Tuple>)
>      {
>        using _Indices
>         = make_index_sequence<tuple_size_v<remove_reference_t<_Tuple>>>;
> @@ -2990,7 +2965,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
>  #endif
>      constexpr auto
>      make_from_tuple(_Tuple&& __t)
> -    noexcept(__unpack_std_tuple<is_nothrow_constructible, _Tp, _Tuple>)
> +    noexcept(__unpack_tuple_like_v<is_nothrow_constructible, _Tp, _Tuple>)
>  #ifdef __cpp_concepts // >= C++20
>      -> _Tp
>      requires __can_make_from_tuple<_Tp, _Tuple>
> diff --git a/libstdc++-v3/testsuite/20_util/tuple/apply/1.cc
> b/libstdc++-v3/testsuite/20_util/tuple/apply/1.cc
> index 05d552f7ebf..a64a5eeea8b 100644
> --- a/libstdc++-v3/testsuite/20_util/tuple/apply/1.cc
> +++ b/libstdc++-v3/testsuite/20_util/tuple/apply/1.cc
> @@ -19,6 +19,7 @@
>  // { dg-add-options no_pch }
>
>  #include <tuple>
> +#include <array>
>
>  #if  __cpp_lib_apply < 201603
>  #  error "__cpp_lib_apply < 201603"
> @@ -35,6 +36,18 @@ test01()
>        VERIFY(&c == &std::get<char>(t));
>        VERIFY(&d == &std::get<double>(t));
>      }, t);
> +
> +  auto p = std::make_pair(1, '2');
> +  std::apply( [&](int& i, char& c) {
> +      VERIFY(&i == &p.first);
> +      VERIFY(&c == &p.second);
> +    }, p);
> +
> +  auto a = std::array<int, 2>{1, 2};
> +  std::apply( [&](int& i, int& j) {
> +      VERIFY(&i == &a[0]);
> +      VERIFY(&j == &a[1]);
> +    }, a);
>  }
>
>  constexpr int func(int i, int j) { return i + j; }
> @@ -45,11 +58,21 @@ test02()
>    constexpr auto t = std::make_tuple(1, 2);
>    constexpr int i = std::apply(func, t);
>    VERIFY( i == 3 );
> +
> +  constexpr auto p = std::make_pair(2, 4);
> +  constexpr int j = std::apply(func, p);
> +  VERIFY( j == 6 );
> +
> +  constexpr auto a = std::array<int, 2>{3, 5};
> +  constexpr int k = std::apply(func, a);
> +  VERIFY( k == 8 );
>  }
>
>  struct F
>  {
>    int f(int i, int j) const { return i + j; }
> +  int g(int i) const { return i + 2; }
> +  int h() const { return 3; }
>  };
>
>  void
> @@ -58,6 +81,14 @@ test03()
>    auto t = std::make_tuple(F{}, 1, 2);
>    int r = std::apply(&F::f, t);
>    VERIFY( r == 3 );
> +
> +  auto p = std::make_pair(F{}, 4);
> +  int j = std::apply(&F::g, p);
> +  VERIFY( j == 6 );
> +
> +  auto a = std::array<F, 1>{};
> +  int k = std::apply(&F::h, a);
> +  VERIFY( k == 3 );
>  }
>
>  int
> diff --git a/libstdc++-v3/testsuite/20_util/tuple/apply/2.cc
> b/libstdc++-v3/testsuite/20_util/tuple/apply/2.cc
> index 0f2e1620b9f..552b1b97112 100644
> --- a/libstdc++-v3/testsuite/20_util/tuple/apply/2.cc
> +++ b/libstdc++-v3/testsuite/20_util/tuple/apply/2.cc
> @@ -20,8 +20,11 @@
>  // Test noexcept-specifier on std::apply
>
>  #include <tuple>
> +#include <array>
>
>  using std::tuple;
> +using std::pair;
> +using std::array;
>  using std::declval;
>
>  void f1();
> @@ -59,3 +62,12 @@ static_assert( noexcept(apply(f4, declval<tuple<int,
> int>>())) );
>  static_assert( !noexcept(apply(f4, declval<tuple<int, int>&>())) );
>  static_assert( !noexcept(apply(f4, declval<tuple<int&, const int>>())) );
>  static_assert( !noexcept(apply(f4, declval<tuple<int, const int>&>())) );
> +
> +static_assert( noexcept(apply(f4, declval<pair<int, int>>())) );
> +static_assert( !noexcept(apply(f4, declval<pair<int, int>&>())) );
> +static_assert( !noexcept(apply(f4, declval<pair<int&, const int>>())) );
> +static_assert( !noexcept(apply(f4, declval<pair<int, const int>&>())) );
> +
> +static_assert( noexcept(apply(f4, declval<array<int, 2>>())) );
> +static_assert( !noexcept(apply(f4, declval<array<int, 2>&>())) );
> +
> diff --git
> a/libstdc++-v3/testsuite/20_util/tuple/make_from_tuple/tuple_like.cc
> b/libstdc++-v3/testsuite/20_util/tuple/make_from_tuple/tuple_like.cc
> index de694554d86..91adea29fd9 100644
> --- a/libstdc++-v3/testsuite/20_util/tuple/make_from_tuple/tuple_like.cc
> +++ b/libstdc++-v3/testsuite/20_util/tuple/make_from_tuple/tuple_like.cc
> @@ -2,42 +2,69 @@
>
>  #include <tuple>
>  #include <utility>
> +#include <array>
> +
> +template<typename Dest, typename Tuple, typename = void>
> +constexpr bool can_make_from_tuple = false;
> +
> +template<typename Dest, typename Tuple>
> +constexpr bool can_make_from_tuple<Dest, Tuple,
> decltype(void(std::make_from_tuple<Dest>(std::declval<Tuple>())))> = true;
>
>  struct Two
>  {
> -  Two(const char*, int);
> +  Two(const char*, int) noexcept;
>  };
>
>  void
>  test_pair()
>  {
> -  auto two = std::make_from_tuple<Two>(std::pair("one", 2));
> +  std::pair p("one", 2);
> +  auto two = std::make_from_tuple<Two>(p);
>    static_assert(std::is_same_v<decltype(two), Two>, "make from pair");
> +  static_assert(noexcept(std::make_from_tuple<Two>(p)), "noexcept for
> pair");
> +  static_assert(!can_make_from_tuple<Two, std::pair<long, long>>);
>  }
>
> -#include <array>
>
>  struct Three
>  {
> -  Three(int, int, int);
> +  Three(int, int, int) noexcept;
>  };
>
>  void
>  test_array()
>  {
> -  Three three = std::make_from_tuple<Three>(std::array<int, 3>{{1, 2,
> 3}});
> +  std::array<int, 3> a{{1, 2, 3}};
> +  auto three = std::make_from_tuple<Three>(a);
>    static_assert(std::is_same_v<decltype(three), Three>, "make from
> array");
> +  static_assert(noexcept(std::make_from_tuple<Three>(a)), "noexcept for
> array");
> +  static_assert(!can_make_from_tuple<Three, std::array<int, 2>>);
> +  static_assert(!can_make_from_tuple<Three, std::array<int*, 3>>);
>  }
>
>  #if __cplusplus >= 202002L
>  #include <vector>
>  #include <ranges>
>
> +struct Cont
> +{
> +  template<std::forward_iterator Iter>
> +  Cont(Iter, Iter) noexcept
> +  { };
> +};
> +
>  void
>  test_subrange() // PR libstdc++/102301
>  {
>    auto r = std::views::iota(0, 5);
> -  auto v =
> std::make_from_tuple<std::vector<int>>(std::ranges::subrange(r));
> +  std::ranges::subrange sr(r);
> +  auto v = std::make_from_tuple<std::vector<int>>(sr);
>    static_assert(std::is_same_v<decltype(v), std::vector<int>>, "from
> subrange");
> +
> +  auto cont = std::make_from_tuple<Cont>(sr);
> +  static_assert(std::is_same_v<decltype(cont), Cont>, "from subrange");
> +  // TODO fix
> +  static_assert(!noexcept(std::make_from_tuple<Cont>(sr)), "noexcept for
> subrange");
> +  static_assert(!can_make_from_tuple<Cont,
> std::ranges::subrange<std::istream_iterator<int>>>);
>  }
>  #endif
> diff --git a/libstdc++-v3/testsuite/26_numerics/complex/tuple_like.cc
> b/libstdc++-v3/testsuite/26_numerics/complex/tuple_like.cc
> index 1150861ae0a..1dcabd0b779 100644
> --- a/libstdc++-v3/testsuite/26_numerics/complex/tuple_like.cc
> +++ b/libstdc++-v3/testsuite/26_numerics/complex/tuple_like.cc
> @@ -116,6 +116,10 @@ test_element_view()
>    VERIFY(imag_reduction == T(25));
>  }
>
> +template<typename Dest, typename Tuple>
> +concept can_make_from_tuple = requires (Tuple t)
> +{ std::make_from_tuple<Dest>(std::forward<Tuple>(t)); };
> +
>  template <typename T>
>  constexpr
>  void
> @@ -123,10 +127,16 @@ test_apply()
>  {
>    std::complex<T> cpx(T(1), T(2));
>
> -  auto f = [](T a, T b) { return a + b; };
> +  auto f = [](T a, T b) noexcept { return a + b; };
>    auto result = std::apply(f, cpx);
> -
>    VERIFY(result == T(3));
> +  static_assert(noexcept(std::apply(f, cpx)));
> +
> +  auto p = std::make_from_tuple<std::pair<T, T>>(cpx);
> +  VERIFY( p.first == T(1) );
> +  VERIFY( p.second == T(2) );
> +  static_assert(noexcept(std::make_from_tuple<std::pair<T, T>>(cpx)));
> +  static_assert(!can_make_from_tuple<std::pair<int*, int*>,
> std::complex<T>>);
>  }
>
>  template <typename T>
> --
> 2.56.0
>
>

Reply via email to