This implements C++26 paper: P1317R2, "Remove return type deduction
in std::apply".
The traits are implemented in terms of __(noexcept_)?unpack_tuple_like_v
machinery, in consequence the definitions of corresponding primary templates
are moved to <type_traits>. Partial specializations remain defined in
bits/stl_pair.h and bits/range_utils.h, i.e. with of before definitions
of corresponding types, which guarantees that they are visible at point
of instantiation depending on them.
While query trait is_applicable, is_nothrow_applicable could be expressed
in terms of __(noexcept_)?unpack_tuple_like_v that produces boolean value,
apply_result requires unpacking tuple to invoke_result. To handle above,
__unpack_tuple_like_impl is introduced, whose value is object of trait
type (false_type for non tuple like), and thus does not require presence
of value member.
libstdc++-v3/ChangeLog:
* include/bits/version.def (apply): Bump to 202506.
* include/bits/version.h: Regenerate.
* include/std/type_traits (__cpp_lib_apply): Define.
(std::__unpack_tuple_like_impl, std::__unpack_tuple_like): Define
primary template.
(std::__unpack_tuple_like_v): Move from bits/stl_pair.h and define
in terms of __unpack_tuple_like.
(std::apply_result, std::apply_result_t, std::is_applicable)
(std::is_applicable_v, std::is_nothrow_applicable)
(std::is_nothrow_applicable_v): Define.
* include/bits/stl_pair.h (std::__noexcept_unpack_tuple_like_v):
Move to std/type_traits.
(std::__unpack_tuple_like_v): Move to std/type_traits, replace
partial specialization with...
(std::__unpack_tuple_like_impl): Define partial specializations.
include/bits/ranges_util.h (std::__unpack_tuple_like_v): Replace
partial specializations with...
(std::__unpack_tuple_like_impl): Define partial specializations.
* include/std/tuple (std::apply) [__glibcxx_apply >= 202506L]:
Use apply_result_t as return tyep.
* testsuite/std/ranges/subrange/tuple_like2.cc: Expand
test for apply.
* src/c++23/std.cc.in (std::apply_result, std::apply_result_t)
(std::is_applicable, std::is_applicable_v)
(std::is_nothrow_applicable, std::is_nothrow_applicable_v): Export.
* testsuite/20_util/is_applicable/1.cc: New test.
---
v2 adds export declarations for newly defined traits, as suggested by
Jakub.
*is_applicable* tested in all standard modes and modules this time.
OK for trunk?
libstdc++-v3/include/bits/ranges_util.h | 18 +-
libstdc++-v3/include/bits/stl_pair.h | 69 +++---
libstdc++-v3/include/bits/version.def | 9 +-
libstdc++-v3/include/bits/version.h | 7 +-
libstdc++-v3/include/std/tuple | 7 +-
libstdc++-v3/include/std/type_traits | 50 ++++
libstdc++-v3/src/c++23/std.cc.in | 8 +
.../testsuite/20_util/is_applicable/1.cc | 221 ++++++++++++++++++
.../std/ranges/subrange/tuple_like2.cc | 48 ++++
9 files changed, 386 insertions(+), 51 deletions(-)
create mode 100644 libstdc++-v3/testsuite/20_util/is_applicable/1.cc
diff --git a/libstdc++-v3/include/bits/ranges_util.h
b/libstdc++-v3/include/bits/ranges_util.h
index 03335335454..c22febf7ed2 100644
--- a/libstdc++-v3/include/bits/ranges_util.h
+++ b/libstdc++-v3/include/bits/ranges_util.h
@@ -487,22 +487,22 @@ namespace ranges
// Unpack a std::pair into a type trait and use its value. We also verify
that
// get is supported, i.e. iterator is copyable.
template<template<typename...> class _Trait, typename _Tp, typename _It,
typename _St>
- constexpr bool __unpack_tuple_like_v<_Trait, _Tp, ranges::subrange<_It,
_St>>
- = _Trait<_Tp, _It, _St>::value;
+ constexpr auto __unpack_tuple_like_impl<_Trait, _Tp, ranges::subrange<_It,
_St>>
+ = _Trait<_Tp, _It, _St>{};
template<template<typename...> class _Trait, typename _Tp, copyable _It,
typename _St>
- constexpr bool __unpack_tuple_like_v<_Trait, _Tp, ranges::subrange<_It,
_St>&>
- = _Trait<_Tp, _It, _St>::value;
+ constexpr auto __unpack_tuple_like_impl<_Trait, _Tp, ranges::subrange<_It,
_St>&>
+ = _Trait<_Tp, _It, _St>{};
template<template<typename...> class _Trait, typename _Tp, copyable _It,
typename _St>
- constexpr bool __unpack_tuple_like_v<_Trait, _Tp, const
ranges::subrange<_It, _St>>
- = _Trait<_Tp, _It, _St>::value;
+ constexpr auto __unpack_tuple_like_impl<_Trait, _Tp, const
ranges::subrange<_It, _St>>
+ = _Trait<_Tp, _It, _St>{};
template<template<typename...> class _Trait, typename _Tp, copyable _It,
typename _St>
- constexpr bool __unpack_tuple_like_v<_Trait, _Tp, const
ranges::subrange<_It, _St>&>
- = _Trait<_Tp, _It, _St>::value;
+ constexpr auto __unpack_tuple_like_impl<_Trait, _Tp, const
ranges::subrange<_It, _St>&>
+ = _Trait<_Tp, _It, _St>{};
- // Subrange get are not marked noexcept
+ // Subrange get are not marked noexcept, so this is always false.
template<template<typename...> class _Trait, typename _Tp, typename _It,
typename _St>
constexpr bool __noexcept_unpack_tuple_like_v<_Trait, _Tp,
ranges::subrange<_It, _St>>
= false;
diff --git a/libstdc++-v3/include/bits/stl_pair.h
b/libstdc++-v3/include/bits/stl_pair.h
index abe1ddc35b0..f93473cf415 100644
--- a/libstdc++-v3/include/bits/stl_pair.h
+++ b/libstdc++-v3/include/bits/stl_pair.h
@@ -235,72 +235,63 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#endif // lib concepts
#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;
-
- // The version of above that check if calls to get are noexcept.
- // Assume it is by default.
- template<template<typename...> class _Trait, typename _Tp, typename _Tuple>
- constexpr bool __noexcept_unpack_tuple_like_v =
__unpack_tuple_like_v<_Trait, _Tp, _Tuple>;
-
// 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;
+ constexpr auto __unpack_tuple_like_impl<_Trait, _Tp, tuple<_Up...>>
+ = _Trait<_Tp, _Up...>{};
template<template<typename...> class _Trait, typename _Tp, typename... _Up>
- constexpr bool __unpack_tuple_like_v<_Trait, _Tp, tuple<_Up...>&>
- = _Trait<_Tp, _Up&...>::value;
+ constexpr auto __unpack_tuple_like_impl<_Trait, _Tp, tuple<_Up...>&>
+ = _Trait<_Tp, _Up&...>{};
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;
+ constexpr auto __unpack_tuple_like_impl<_Trait, _Tp, const tuple<_Up...>>
+ = _Trait<_Tp, const _Up...>{};
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;
+ constexpr auto __unpack_tuple_like_impl<_Trait, _Tp, const tuple<_Up...>&>
+ = _Trait<_Tp, const _Up&...>{};
// 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;
+ constexpr auto __unpack_tuple_like_impl<_Trait, _Tp, pair<_Fp, _Sp>>
+ = _Trait<_Tp, _Fp, _Sp>{};
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;
+ constexpr auto __unpack_tuple_like_impl<_Trait, _Tp, pair<_Fp, _Sp>&>
+ = _Trait<_Tp, _Fp&, _Sp&>{};
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;
+ constexpr auto __unpack_tuple_like_impl<_Trait, _Tp, const pair<_Fp, _Sp>>
+ = _Trait<_Tp, const _Fp, const _Sp>{};
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;
+ constexpr auto __unpack_tuple_like_impl<_Trait, _Tp, const pair<_Fp, _Sp>&>
+ = _Trait<_Tp, const _Fp&, const _Sp&>{};
namespace __detail
{
template<template<typename...> class _Trait, typename _Tp, typename _Up,
size_t... _Ids>
- constexpr bool
+ constexpr auto
__apply_repeated_arg(index_sequence<_Ids...>)
- { return _Trait<_Tp, decltype(((void)_Ids,
std::declval<_Up>()))...>::value; }
+ { return _Trait<_Tp, decltype(((void)_Ids, std::declval<_Up>()))...>{}; }
}
// 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>>
+ constexpr auto __unpack_tuple_like_impl<_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>&>
+ constexpr auto __unpack_tuple_like_impl<_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>>
+ constexpr auto __unpack_tuple_like_impl<_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>&>
+ constexpr auto __unpack_tuple_like_impl<_Trait, _Tp, const array<_Up,
_Nm>&>
= __detail::__apply_repeated_arg<_Trait, _Tp, const
_Up&>(make_index_sequence<_Nm>{});
#endif
@@ -308,20 +299,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// 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;
+ constexpr auto __unpack_tuple_like_impl<_Trait, _Tp, complex<_Up>>
+ = _Trait<_Tp, _Up, _Up>{};
template<template<typename...> class _Trait, typename _Tp, typename _Up>
- constexpr bool __unpack_tuple_like_v<_Trait, _Tp, complex<_Up>&>
- = _Trait<_Tp, _Up&, _Up&>::value;
+ constexpr auto __unpack_tuple_like_impl<_Trait, _Tp, complex<_Up>&>
+ = _Trait<_Tp, _Up&, _Up&>{};
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;
+ constexpr auto __unpack_tuple_like_impl<_Trait, _Tp, const complex<_Up>>
+ = _Trait<_Tp, const _Up, const _Up>{};
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;
+ constexpr auto __unpack_tuple_like_impl<_Trait, _Tp, const complex<_Up>&>
+ = _Trait<_Tp, const _Up&, const _Up&>{};
#endif
// __is_tuple_like<subrange> is defined in <bits/ranges_util.h>.
diff --git a/libstdc++-v3/include/bits/version.def
b/libstdc++-v3/include/bits/version.def
index 340ef4203e1..3793efdfe35 100644
--- a/libstdc++-v3/include/bits/version.def
+++ b/libstdc++-v3/include/bits/version.def
@@ -91,7 +91,7 @@ ftms = {
ftms = {
// 202106 P0401R6 Providing size feedback in the Allocator interface
// 202302 P2652R2 Disallow user specialization of allocator_traits
- // LWG3887 Version macro for allocate_at_least
+ // LWG3887 Version macro for allocate_at_least
name = allocate_at_least;
values = {
v = 202302;
@@ -371,6 +371,13 @@ ftms = {
ftms = {
name = apply;
+ // 202506 P1317R2 Remove return type deduction in std::apply
+ // 202603 P3795R2 Miscellaneous Reflection Cleanup
+ // LWG4567 Feature test macro value for apply_result, is_applicable
+ values = {
+ v = 202506;
+ cxxmin = 26;
+ };
values = {
v = 201603;
cxxmin = 17;
diff --git a/libstdc++-v3/include/bits/version.h
b/libstdc++-v3/include/bits/version.h
index 3175bc1b65b..8651e2c56c5 100644
--- a/libstdc++-v3/include/bits/version.h
+++ b/libstdc++-v3/include/bits/version.h
@@ -396,7 +396,12 @@
#undef __glibcxx_want_any
#if !defined(__cpp_lib_apply)
-# if (__cplusplus >= 201703L)
+# if (__cplusplus > 202302L)
+# define __glibcxx_apply 202506L
+# if defined(__glibcxx_want_all) || defined(__glibcxx_want_apply)
+# define __cpp_lib_apply 202506L
+# endif
+# elif (__cplusplus >= 201703L)
# define __glibcxx_apply 201603L
# if defined(__glibcxx_want_all) || defined(__glibcxx_want_apply)
# define __cpp_lib_apply 201603L
diff --git a/libstdc++-v3/include/std/tuple b/libstdc++-v3/include/std/tuple
index fd89f6d2540..e273ed0a817 100644
--- a/libstdc++-v3/include/std/tuple
+++ b/libstdc++-v3/include/std/tuple
@@ -2926,9 +2926,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#else
template <typename _Fn, typename _Tuple>
#endif
- constexpr decltype(auto)
+ constexpr auto
apply(_Fn&& __f, _Tuple&& __t)
noexcept(__noexcept_unpack_tuple_like_v<is_nothrow_invocable, _Fn, _Tuple>)
+#if __glibcxx_apply >= 202506L // >= C++26
+ -> apply_result_t<_Fn, _Tuple>
+#else
+ -> decltype(auto)
+#endif
{
using _Indices
= make_index_sequence<tuple_size_v<remove_reference_t<_Tuple>>>;
diff --git a/libstdc++-v3/include/std/type_traits
b/libstdc++-v3/include/std/type_traits
index 83dbad17ade..44e119adc81 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -39,6 +39,7 @@
#include <bits/c++config.h>
+#define __glibcxx_want_apply
#define __glibcxx_want_bool_constant
#define __glibcxx_want_bounded_array_traits
#define __glibcxx_want_common_reference
@@ -3074,6 +3075,22 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct __is_tuple_like
: public __is_tuple_like_impl<__remove_cvref_t<_Tp>>::type
{ };
+
+#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 auto __unpack_tuple_like_impl = false_type{};
+
+ template<template<typename...> class _Trait, typename _Tp, typename _Tuple>
+ using __unpack_tuple_like =
remove_cv_t<decltype(__unpack_tuple_like_impl<_Trait, _Tp, _Tuple>)>;
+
+ template<template<typename...> class _Trait, typename _Tp, typename _Tuple>
+ constexpr bool __unpack_tuple_like_v = __unpack_tuple_like<_Trait, _Tp,
_Tuple>::value;
+
+ // The version of above that check if calls to get are noexcept. Assume it
is by default.
+ template<template<typename...> class _Trait, typename _Tp, typename _Tuple>
+ constexpr bool __noexcept_unpack_tuple_like_v =
__unpack_tuple_like<_Trait, _Tp, _Tuple>::value;
+#endif
/// @endcond
template<typename _Tp>
@@ -4412,6 +4429,39 @@ template<typename _Ret, typename _Fn, typename... _Args>
}
#endif
+#if __glibcxx_apply >= 202506L // C++26
+ template<typename _Fn, typename _Tuple>
+ struct apply_result
+ { };
+
+ template<typename _Fn, typename _Tuple>
+ requires (!is_same_v<__unpack_tuple_like<__invoke_result, _Fn, _Tuple>,
false_type>)
+ struct apply_result<_Fn, _Tuple>
+ : __unpack_tuple_like<__invoke_result, _Fn, _Tuple>
+ { };
+
+ template<typename _Fn, typename _Tuple>
+ using apply_result_t = typename apply_result<_Fn, _Tuple>::type;
+
+ template<typename _Fn, typename _Tuple>
+ struct is_applicable
+ : __unpack_tuple_like<is_invocable, _Fn, _Tuple>
+ { };
+
+ template<typename _Fn, typename _Tuple>
+ struct is_nothrow_applicable
+ : bool_constant<__noexcept_unpack_tuple_like_v<is_nothrow_invocable, _Fn,
_Tuple>>
+ { };
+
+ template<typename _Fn, typename _Tuple>
+ constexpr bool is_applicable_v
+ = __unpack_tuple_like_v<is_invocable, _Fn, _Tuple>;
+
+ template<typename _Fn, typename _Tuple>
+ constexpr bool is_nothrow_applicable_v
+ = __noexcept_unpack_tuple_like_v<is_nothrow_invocable, _Fn, _Tuple>;
+#endif
+
/// @} group metaprogramming
_GLIBCXX_END_NAMESPACE_VERSION
diff --git a/libstdc++-v3/src/c++23/std.cc.in b/libstdc++-v3/src/c++23/std.cc.in
index d05902466d9..51286d4c7a3 100644
--- a/libstdc++-v3/src/c++23/std.cc.in
+++ b/libstdc++-v3/src/c++23/std.cc.in
@@ -3626,6 +3626,14 @@ export namespace std
#if __cpp_lib_within_lifetime >= 202306L
using std::is_within_lifetime;
#endif
+#if __cpp_lib_apply >= 202506L
+ using std::apply_result;
+ using std::apply_result_t;
+ using std::is_applicable;
+ using std::is_applicable_v;
+ using std::is_nothrow_applicable;
+ using std::is_nothrow_applicable_v;
+#endif
}
// <typeindex>
diff --git a/libstdc++-v3/testsuite/20_util/is_applicable/1.cc
b/libstdc++-v3/testsuite/20_util/is_applicable/1.cc
new file mode 100644
index 00000000000..0a1073dad3b
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/is_applicable/1.cc
@@ -0,0 +1,221 @@
+// { dg-do compile { target c++26 } }
+
+#include <type_traits>
+
+#ifndef __cpp_lib_apply
+# error "Feature-test macro for is_applicable missing in <type_traits>"
+#elif __cpp_lib_apply < 202506L
+# error "Feature-test macro for is_applicable has wrong value in <type_traits>"
+#endif
+
+#include <array>
+#include <tuple>
+#include <utility>
+#include <complex>
+
+template<typename Fn, typename Tuple>
+concept can_apply = requires (Fn f, Tuple t)
+{ std::apply(std::forward<Fn>(f), std::forward<Tuple>(t)); };
+
+template<int V, typename Res, typename Fn, typename Tuple>
+constexpr void
+check_apply()
+{
+ constexpr bool callable = (V > 0);
+ constexpr bool noex = (V > 1);
+
+ static_assert(can_apply<Fn, Tuple> == callable);
+
+ static_assert(std::is_applicable<Fn, Tuple>::value == callable);
+ static_assert(std::is_applicable_v<Fn, Tuple> == callable);
+
+ static_assert(std::is_nothrow_applicable<Fn, Tuple>::value == noex);
+ static_assert(std::is_nothrow_applicable_v<Fn, Tuple> == noex);
+
+ if constexpr (callable)
+ {
+ static_assert(std::is_same_v<Res, typename std::apply_result<Fn,
Tuple>::type>);
+ static_assert(std::is_same_v<Res, std::apply_result_t<Fn, Tuple>>);
+ }
+ else
+ {
+ static_assert(!requires { typename std::apply_result<Fn, Tuple>::type; });
+ static_assert(!requires { typename std::apply_result_t<Fn, Tuple>; });
+ }
+}
+
+struct Throw
+{
+ Throw(float) {}
+};
+
+template<typename T, typename U = T, bool Noex = true>
+T f(T t, U) noexcept(Noex)
+{ return t; }
+
+template<template<typename> typename SamePair>
+void
+test_val()
+{
+ check_apply<2, float, decltype(&f<float>), SamePair<float>>();
+ check_apply<2, float, decltype(&f<float>), SamePair<int>>();
+ check_apply<1, Throw, decltype(&f<Throw>), SamePair<float>>();
+
+ check_apply<1, float, decltype(&f<float, float, false>), SamePair<float>>();
+ check_apply<1, Throw, decltype(&f<Throw, Throw, false>), SamePair<float>>();
+
+ check_apply<0, float, decltype(&f<float>), SamePair<int*>()>();
+}
+
+template<template<typename> typename SamePair>
+void
+test_ref()
+{
+ check_apply<2, float&&, decltype(&f<float&&>), SamePair<float>>();
+ check_apply<0, float&&, decltype(&f<float&&>), SamePair<float>&>();
+ check_apply<0, float&&, decltype(&f<float&&>), const SamePair<float>>();
+ check_apply<0, float&&, decltype(&f<float&&>), const SamePair<float>&>();
+
+ check_apply<0, float&, decltype(&f<float&>), SamePair<float>>();
+ check_apply<2, float&, decltype(&f<float&>), SamePair<float>&>();
+ check_apply<0, float&, decltype(&f<float&>), const SamePair<float>>();
+ check_apply<0, float&, decltype(&f<float&>), const SamePair<float>&>();
+
+ check_apply<2, const float&, decltype(&f<const float&>), SamePair<float>>();
+ check_apply<2, const float&, decltype(&f<const float&>), SamePair<float>&>();
+ check_apply<2, const float&, decltype(&f<const float&>), const
SamePair<float>>();
+ check_apply<2, const float&, decltype(&f<const float&>), const
SamePair<float>&>();
+}
+
+struct Func
+{
+ int f(Func) noexcept { return 1; }
+ int cf(Func) const noexcept { return 2; }
+
+ int rq(Func) && noexcept { return 1; }
+ int lq(Func) & noexcept { return 1; }
+ int crq(Func) const&& noexcept { return 1; }
+ int clq(Func) const& noexcept { return 1; }
+};
+
+template<template<typename> typename SamePair>
+void
+test_member()
+{
+ check_apply<2, int, decltype(&Func::f), SamePair<Func>>();
+ check_apply<2, int, decltype(&Func::f), SamePair<Func>&>();
+ check_apply<0, int, decltype(&Func::f), const SamePair<Func>>();
+ check_apply<0, int, decltype(&Func::f), const SamePair<Func>&>();
+
+ check_apply<2, int, decltype(&Func::cf), SamePair<Func>>();
+ check_apply<2, int, decltype(&Func::cf), SamePair<Func>&>();
+ check_apply<2, int, decltype(&Func::cf), const SamePair<Func>>();
+ check_apply<2, int, decltype(&Func::cf), const SamePair<Func>&>();
+
+ check_apply<2, int, decltype(&Func::rq), SamePair<Func>>();
+ check_apply<0, int, decltype(&Func::rq), SamePair<Func>&>();
+ check_apply<0, int, decltype(&Func::rq), const SamePair<Func>>();
+ check_apply<0, int, decltype(&Func::rq), const SamePair<Func>&>();
+
+ check_apply<2, int, decltype(&Func::crq), SamePair<Func>>();
+ check_apply<0, int, decltype(&Func::crq), SamePair<Func>&>();
+ check_apply<2, int, decltype(&Func::crq), const SamePair<Func>>();
+ check_apply<0, int, decltype(&Func::crq), const SamePair<Func>&>();
+
+ check_apply<0, int, decltype(&Func::lq), SamePair<Func>>();
+ check_apply<2, int, decltype(&Func::lq), SamePair<Func>&>();
+ check_apply<0, int, decltype(&Func::lq), const SamePair<Func>>();
+ check_apply<0, int, decltype(&Func::lq), const SamePair<Func>&>();
+
+ check_apply<2, int, decltype(&Func::clq), SamePair<Func>>();
+ check_apply<2, int, decltype(&Func::clq), SamePair<Func>&>();
+ check_apply<2, int, decltype(&Func::clq), const SamePair<Func>>();
+ check_apply<2, int, decltype(&Func::clq), const SamePair<Func>&>();
+}
+
+template<template<typename> typename SamePair>
+void
+test_quals()
+{
+ check_apply<0, float&&, decltype(&f<float&&>), SamePair<const float>>();
+ check_apply<0, float&&, decltype(&f<float&&>), SamePair<const float>>();
+
+ check_apply<0, float&, decltype(&f<float&>), SamePair<const float>&>();
+ check_apply<0, float&, decltype(&f<float&>), SamePair<const float>&>();
+}
+
+template<template<typename> typename SamePair>
+void
+test_ref_args()
+{
+ check_apply<2, float&&, decltype(&f<float&&>), SamePair<float&&>>();
+ check_apply<0, float&&, decltype(&f<float&&>), SamePair<float&&>&>();
+ check_apply<0, float&&, decltype(&f<float&&>), SamePair<float&>>();
+ check_apply<0, float&&, decltype(&f<float&&>), SamePair<const float&&>>();
+
+ check_apply<2, float&, decltype(&f<float&>), SamePair<float&>>();
+ check_apply<2, float&, decltype(&f<float&>), SamePair<float&>&>();
+ check_apply<0, float&, decltype(&f<float&>), SamePair<float&&>>();
+ check_apply<2, float&, decltype(&f<float&>), SamePair<float&&>&>();
+ check_apply<0, float&, decltype(&f<float&>), SamePair<const float&>>();
+}
+
+template<template<typename, typename> typename Pair>
+void
+test_mixed()
+{
+ check_apply<2, float, decltype(&f<float, int*>), Pair<float, int*>>();
+ check_apply<0, float, decltype(&f<float, int*>), Pair<int*, float>>();
+
+ check_apply<2, float&, decltype(&f<float&, const float&>), Pair<float, const
float>&>();
+ check_apply<0, float&, decltype(&f<float&, const float&>), Pair<const float,
float>&>();
+
+ check_apply<2, float&, decltype(&f<float&, const float&>), Pair<float&,
const float&>>();
+ check_apply<0, float&, decltype(&f<float&, const float&>), Pair<const
float&, float&>>();
+}
+
+template<typename T>
+using TuplePair = std::tuple<T, T>;
+
+template<typename T>
+using PairPair = std::pair<T, T>;
+
+template<typename T>
+using ArrayPair = std::array<T, 2>;
+
+template<typename T>
+using ComplexPair = std::complex<T>;
+
+void
+test_by_val()
+{
+ test_val<TuplePair>();
+ test_val<PairPair>();
+ test_val<ArrayPair>();
+ test_val<ComplexPair>();
+
+ test_ref<TuplePair>();
+ test_ref<PairPair>();
+ test_ref<ArrayPair>();
+ test_ref<ComplexPair>();
+
+ test_member<TuplePair>();
+ test_member<PairPair>();
+ test_member<ArrayPair>();
+
+ test_quals<TuplePair>();
+ test_quals<PairPair>();
+ test_ref_args<TuplePair>();
+ test_ref_args<PairPair>();
+
+ test_mixed<std::tuple>();
+ test_mixed<std::pair>();
+
+ check_apply<0, float, decltype(&f<float>), std::tuple<>>();
+ check_apply<0, float, decltype(&f<float>), std::array<float, 0>>();
+ check_apply<0, float, decltype(&f<float>), std::tuple<float>>();
+ check_apply<0, float, decltype(&f<float>), std::array<float, 1>>();
+ check_apply<0, float, decltype(&f<float>), std::tuple<float, float,
float>>();
+ check_apply<0, float, decltype(&f<float>), std::array<float, 3>>();
+}
+
diff --git a/libstdc++-v3/testsuite/std/ranges/subrange/tuple_like2.cc
b/libstdc++-v3/testsuite/std/ranges/subrange/tuple_like2.cc
index 614e690d0e5..0f9c29609e4 100644
--- a/libstdc++-v3/testsuite/std/ranges/subrange/tuple_like2.cc
+++ b/libstdc++-v3/testsuite/std/ranges/subrange/tuple_like2.cc
@@ -85,10 +85,45 @@ test_tuple_cat()
VERIFY( std::get<3>(r) == "hello" );
}
+template<typename Fn, typename Tuple>
+concept can_apply = requires (Fn f, Tuple t)
+{ std::apply(std::forward<Fn>(f), std::forward<Tuple>(t)); };
+
template<typename Dest, typename Tuple>
concept can_make_from_tuple = requires (Tuple t)
{ std::make_from_tuple<Dest>(std::forward<Tuple>(t)); };
+template<int V, typename Res, typename Fn, typename Tuple>
+constexpr void
+check_apply()
+{
+#if __cpp_lib_apply >= 202506L
+ constexpr bool callable = (V > 0);
+ constexpr bool noex = (V > 1);
+
+ static_assert(can_apply<Fn, Tuple> == callable);
+
+ static_assert(std::is_applicable<Fn, Tuple>::value == callable);
+ static_assert(std::is_applicable_v<Fn, Tuple> == callable);
+
+ static_assert(std::is_nothrow_applicable<Fn, Tuple>::value == noex);
+ static_assert(std::is_nothrow_applicable_v<Fn, Tuple> == noex);
+
+ if constexpr (callable)
+ {
+ static_assert(std::is_same_v<Res, typename std::apply_result<Fn,
Tuple>::type>);
+ static_assert(std::is_same_v<Res, std::apply_result_t<Fn, Tuple>>);
+ }
+ else
+ {
+ static_assert(!requires { typename std::apply_result<Fn, Tuple>::type; });
+ static_assert(!requires { typename std::apply_result_t<Fn, Tuple>; });
+ }
+#else
+ static_assert(can_apply<F, Tuple>);
+#endif
+}
+
struct Cont
{
template<typename T, typename U>
@@ -115,15 +150,28 @@ test_apply()
auto r1 = std::apply(f, rng);
VERIFY( r1 );
static_assert( noexcept(std::apply(f, rng)) == noexcept_get<R&> );
+ check_apply<1, bool, decltype((f)), R&>();
auto r2 = std::apply(f, crng);
VERIFY( r2 );
static_assert( noexcept(std::apply(f, crng)) == noexcept_get<const R&> );
+ check_apply<1, bool, decltype((f)), const R&>();
}
+ else
+ {
+ check_apply<0, bool, decltype((f)), R&>();
+ check_apply<0, bool, decltype((f)), const R&>();
+ }
+
+ auto g = [](int, int) noexcept { };
+ check_apply<0, void, decltype((g)), R>();
+ check_apply<0, void, decltype((g)), R&>();
+ check_apply<0, void, decltype((g)), const R&>();
auto r = std::apply(f, std::move(rng));
VERIFY( r );
static_assert( noexcept(std::apply(f, std::move(rng))) == noexcept_get<R> );
+ check_apply<1, bool, decltype((f)), R>();
if constexpr (std::copyable<It>)
{
--
2.56.0