https://gcc.gnu.org/g:db7ef921c00622e868cb61fec87d85dfe4c12e7d
commit r17-6841-gdb7ef921c00622e868cb61fec87d85dfe4c12e7d Author: Tomasz KamiĆski <[email protected]> Date: Thu Oct 29 17:24:15 2026 +0100 libstdc++: Implement apply_result, is_applicable, and is_nothrow_applicable from P1317R2 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 tests 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. Reviewed-by: Jonathan Wakely <[email protected]> Diff: --- libstdc++-v3/include/bits/ranges_util.h | 20 +- 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 + libstdc++-v3/testsuite/20_util/is_applicable/1.cc | 221 +++++++++++++++++++++ .../testsuite/std/ranges/subrange/tuple_like2.cc | 48 +++++ 9 files changed, 387 insertions(+), 52 deletions(-) diff --git a/libstdc++-v3/include/bits/ranges_util.h b/libstdc++-v3/include/bits/ranges_util.h index 03335335454d..48113aff033e 100644 --- a/libstdc++-v3/include/bits/ranges_util.h +++ b/libstdc++-v3/include/bits/ranges_util.h @@ -484,25 +484,25 @@ namespace ranges inline constexpr bool __detail::__is_subrange<subrange<_Iter, _Sent, _Kind>> = true; } // namespace ranges - // Unpack a std::pair into a type trait and use its value. We also verify that + // Unpack a ranges::subrange 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 + // get(ranges::ubrange) are not marked noexcept, and thus always potentially throwing 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 8d01c22a4d04..d6ea613b20c6 100644 --- a/libstdc++-v3/include/bits/stl_pair.h +++ b/libstdc++-v3/include/bits/stl_pair.h @@ -242,72 +242,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 // >= C++17 @@ -315,20 +306,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 // >= C++26 // __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 e0a73ea3b205..02ca15caba68 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 700f7f776849..a9d19fb15fdc 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 45f77189fdf7..13970b456804 100644 --- a/libstdc++-v3/include/std/tuple +++ b/libstdc++-v3/include/std/tuple @@ -2922,9 +2922,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 d4199dd5a33b..9369834306fb 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 @@ -3060,6 +3061,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> @@ -4398,6 +4415,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 15349adf1be0..299dd8f39073 100644 --- a/libstdc++-v3/src/c++23/std.cc.in +++ b/libstdc++-v3/src/c++23/std.cc.in @@ -3631,6 +3631,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 000000000000..0a1073dad3b7 --- /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 614e690d0e52..0f9c29609e4d 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>) {
