Hi,

Clang 21 added `__builtin_invoke` (llvm/llvm-project#116709), and libc++
already uses it for std::invoke. Clang doesn't implement GCC's
`__is_invocable`/`__is_nothrow_invocable` built-ins, so with Clang
libstdc++ still computes INVOKE through __result_of_impl tag dispatch and
`std::invoke` -> `std::__invoke` -> `std::__invoke_impl`. That makes
`std::invoke` and the invocable traits several times slower to compile than
a direct call.

The attached patch:
- Uses `__builtin_invoke`, when available, for `__invoke_result`,
`std::__invoke`, `std::__invoke_r`, `std::invoke`, and the nothrow check.
Declared return types are unchanged, so mangled names don't change.
- Makes the completeness checks in the invocable traits cheaper. This
  part also speeds up GCC.

Compile time for a file with 2000 distinct callables (-std=c++20
-fsyntax-only, median of 5 runs):

                                 Clang 21       GCC trunk
  std::invoke(f, ...)            4.3x faster    1.3x faster
  std::invoke(&C::m, ...)        4.4x faster    1.3x faster
  is_invocable_v, is_nothrow_
    invocable_v, invoke_result_t 2.6x faster    1.6x faster

We've tested against GCC trunk and, since the new code path is only used
with Clang, against Clang 21 as well (x86_64-pc-linux-gnu). The libstdc++
tests for 20_util, 24_iterators, 25_algorithms, 30_threads and std/ranges
show no new failures with either compiler. The patch adds two tests,
20_util/function_objects/invoke/builtin.cc and builtin2.cc.

Thanks,
Richard
From dc2ad252aa41d332499a8c0b31f8e9cc0ea93644 Mon Sep 17 00:00:00 2001
From: Richard Barnes <[email protected]>
Date: Wed, 18 Nov 2026 12:17:50 -0800
Subject: [PATCH] libstdc++: Use __builtin_invoke to implement INVOKE when
 available

Clang 21 added __builtin_invoke, which libc++ uses to implement
std::invoke (llvm/llvm-project#116709).  Clang does not support the
__is_invocable and __is_nothrow_invocable built-ins, so with Clang
libstdc++ finds the type of an INVOKE expression by tag dispatching in
__result_of_impl, and std::invoke goes through std::__invoke and
std::__invoke_impl.  Each step instantiates more templates, which makes
std::invoke and the invocable traits several times slower to compile
than calling the function object directly.

When __builtin_invoke is available, use it to implement __invoke_result,
std::__invoke, std::__invoke_r, std::invoke, and the nothrow check used
by std::is_nothrow_invocable and friends.  The declared return types are
unchanged, so mangled names do not change.  Those functions forward
their arguments with static_cast instead of std::forward, to avoid
instantiating std::forward for every argument type.

Also make the completeness checks cheaper, which helps GCC too:
__maybe_complete_object_type uses the __is_object and
__is_unbounded_array built-ins when available; the less specialized
__is_complete_or_unbounded overload no longer returns a nested ::type,
so its return type is not instantiated when the other overload is
chosen; and the C++17 invocable traits check completeness with a new
variable template instead of overload resolution.

Compile times for a translation unit with 2000 distinct function
objects, each used once, with -std=c++20 -fsyntax-only (median user+sys
seconds of 5 runs):

                            Clang 21          GCC trunk
                         before   after    before   after
  no INVOKE (baseline)     0.21    0.21      0.53    0.54
  std::invoke(f, ...)      2.78    0.64      3.39    2.55
  std::invoke(&C::m, ...)  3.60    0.82      5.08    3.83
  invocable traits (*)     2.13    0.83      2.13    1.32

  (*) is_invocable_v, is_nothrow_invocable_v and invoke_result_t

With Clang, a minimal invoke written directly with __builtin_invoke
takes 0.34 and 0.40 seconds for the two std::invoke cases.

Tested on x86_64-pc-linux-gnu with GCC (20_util, 24_iterators,
25_algorithms, 30_threads and std/ranges, also with
GLIBCXX_TESTSUITE_STDS=11,14,17,23 for 20_util and std/ranges).  The
same tests were also compiled with Clang 21 against the installed
headers for -std=gnu++11 to gnu++26, and the run tests run for gnu++17
and gnu++20, with the same results before and after this change, except
that 20_util/shared_ptr/cons/126120.cc now compiles with -std=gnu++11.

libstdc++-v3/ChangeLog:

        * include/bits/invoke.h (__invoke, __invoke_r): Use
        __builtin_invoke if available.
        * include/std/functional (invoke): Likewise.
        * include/std/type_traits (__maybe_complete_object_type): Use
        __is_object and __is_unbounded_array built-ins if available.
        (__is_complete_or_unbounded): Do not use a nested type as the
        return type of the less specialized overload.
        (__invoke_declval, __invoke_result_builtin): New.
        (__invoke_result): Use __invoke_result_builtin if __builtin_invoke
        is available.
        (__call_is_nothrow): Use __builtin_invoke if available.
        (__is_complete_or_unbounded_v): New variable template.
        (invoke_result, is_invocable, is_invocable_r, is_nothrow_invocable)
        (is_nothrow_invocable_r): Use __is_complete_or_unbounded_v in
        static assertions.
        * testsuite/20_util/function_objects/invoke/builtin.cc: New test.
        * testsuite/20_util/function_objects/invoke/builtin2.cc: New test.

Signed-off-by: Richard Barnes <[email protected]>
---
 libstdc++-v3/include/bits/invoke.h            |  31 +++
 libstdc++-v3/include/std/functional           |  10 +
 libstdc++-v3/include/std/type_traits          |  93 +++++++--
 .../function_objects/invoke/builtin.cc        | 181 ++++++++++++++++++
 .../function_objects/invoke/builtin2.cc       | 104 ++++++++++
 5 files changed, 400 insertions(+), 19 deletions(-)
 create mode 100644 
libstdc++-v3/testsuite/20_util/function_objects/invoke/builtin.cc
 create mode 100644 
libstdc++-v3/testsuite/20_util/function_objects/invoke/builtin2.cc

diff --git a/libstdc++-v3/include/bits/invoke.h 
b/libstdc++-v3/include/bits/invoke.h
index 4e000d56..8ee557fb 100644
--- a/libstdc++-v3/include/bits/invoke.h
+++ b/libstdc++-v3/include/bits/invoke.h
@@ -86,10 +86,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     __invoke_impl(__invoke_memobj_deref, _MemPtr&& __f, _Tp&& __t)
     { return (*std::forward<_Tp>(__t)).*__f; }
 
+  // When __builtin_invoke is used below, arguments are forwarded with
+  // static_cast instead of std::forward, to avoid instantiating it.
+
   /// Invoke a callable object.
   template<typename _Callable, typename... _Args>
     constexpr typename __invoke_result<_Callable, _Args...>::type
     __invoke(_Callable&& __fn, _Args&&... __args)
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_invoke)
+    noexcept(noexcept(__builtin_invoke(static_cast<_Callable&&>(__fn),
+                                      static_cast<_Args&&>(__args)...)))
+    {
+      return __builtin_invoke(static_cast<_Callable&&>(__fn),
+                             static_cast<_Args&&>(__args)...);
+    }
+#else
     noexcept(__is_nothrow_invocable<_Callable, _Args...>::value)
     {
       using __result = __invoke_result<_Callable, _Args...>;
@@ -98,6 +109,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       return std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn),
                                        std::forward<_Args>(__args)...);
     }
+#endif
 
 #if __cplusplus >= 201703L
   // INVOKE<R>: Invoke a callable object and convert the result to R.
@@ -106,6 +118,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     __invoke_r(_Callable&& __fn, _Args&&... __args)
     noexcept(is_nothrow_invocable_r_v<_Res, _Callable, _Args...>)
     {
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_invoke)
+      if constexpr (is_void_v<_Res>)
+       __builtin_invoke(static_cast<_Callable&&>(__fn),
+                        static_cast<_Args&&>(__args)...);
+      else
+       return __builtin_invoke(static_cast<_Callable&&>(__fn),
+                               static_cast<_Args&&>(__args)...);
+#else
       using __result = __invoke_result<_Callable, _Args...>;
       using __type = typename __result::type;
       using __tag = typename __result::__invoke_type;
@@ -116,6 +136,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        return std::__invoke_impl<__type>(__tag{},
                                          std::forward<_Callable>(__fn),
                                          std::forward<_Args>(__args)...);
+#endif
     }
 #else // C++11 or C++14
   // This is a non-SFINAE-friendly std::invoke_r<R>(fn, args...) for C++11/14.
@@ -136,9 +157,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       static_assert(!__reference_converts_from_temporary(_Res, __type),
                    "INVOKE<R> must not create a dangling reference");
 #endif
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_invoke)
+      return __builtin_invoke(static_cast<_Callable&&>(__fn),
+                             static_cast<_Args&&>(__args)...);
+#else
       using __tag = typename __result::__invoke_type;
       return std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn),
                                        std::forward<_Args>(__args)...);
+#endif
     }
 
   // INVOKE<R> when R is cv void
@@ -146,11 +172,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     _GLIBCXX14_CONSTEXPR __enable_if_t<is_void<_Res>::value, _Res>
     __invoke_r(_Callable&& __fn, _Args&&... __args)
     {
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_invoke)
+      __builtin_invoke(static_cast<_Callable&&>(__fn),
+                      static_cast<_Args&&>(__args)...);
+#else
       using __result = __invoke_result<_Callable, _Args...>;
       using __type = typename __result::type;
       using __tag = typename __result::__invoke_type;
       std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn),
                                 std::forward<_Args>(__args)...);
+#endif
     }
 #endif // C++11 or C++14
 
diff --git a/libstdc++-v3/include/std/functional 
b/libstdc++-v3/include/std/functional
index 91478d3e..c15589f2 100644
--- a/libstdc++-v3/include/std/functional
+++ b/libstdc++-v3/include/std/functional
@@ -121,12 +121,22 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _Callable, typename... _Args>
     inline _GLIBCXX20_CONSTEXPR invoke_result_t<_Callable, _Args...>
     invoke(_Callable&& __fn, _Args&&... __args)
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_invoke)
+    // Use static_cast instead of std::forward to avoid instantiating it.
+    noexcept(noexcept(__builtin_invoke(static_cast<_Callable&&>(__fn),
+                                      static_cast<_Args&&>(__args)...)))
+    {
+      return __builtin_invoke(static_cast<_Callable&&>(__fn),
+                             static_cast<_Args&&>(__args)...);
+    }
+#else
     noexcept(is_nothrow_invocable_v<_Callable, _Args...>)
     {
       return std::__invoke(std::forward<_Callable>(__fn),
                           std::forward<_Args>(__args)...);
     }
 #endif
+#endif
 
 #ifdef __cpp_lib_invoke_r // C++ >= 23
 
diff --git a/libstdc++-v3/include/std/type_traits 
b/libstdc++-v3/include/std/type_traits
index cfdf9edf..f9d32756 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -321,9 +321,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   // An object type which is not an unbounded array.
   // It might still be an incomplete type, but if this is false_type
   // then we can be certain it's not a complete object type.
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__is_object) \
+  && _GLIBCXX_USE_BUILTIN_TRAIT(__is_unbounded_array)
+  template<typename _Tp>
+    struct __maybe_complete_object_type
+    : __bool_constant<__is_object(_Tp) && !__is_unbounded_array(_Tp)>
+    { };
+#else
   template<typename _Tp>
     using __maybe_complete_object_type
       = __and_<is_object<_Tp>, __not_<__is_array_unknown_bounds<_Tp>>>;
+#endif
 
   // Helper functions that return false_type for incomplete classes,
   // incomplete unions and arrays of known bound from those.
@@ -337,10 +345,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     { return {}; };
 
   // Less specialized overload for reference and unknown-bound array types
-  // (returning true_type), and incomplete types (returning false_type).
+  // (returning true), and incomplete types (returning false).
+  // The return type is not a nested ::type, so that overload resolution
+  // does not instantiate it when the overload above is selected.
   template<typename _TypeIdentity,
           typename _NestedType = typename _TypeIdentity::type>
-    constexpr typename __not_<__maybe_complete_object_type<_NestedType>>::type
+    constexpr __not_<__maybe_complete_object_type<_NestedType>>
     __is_complete_or_unbounded(_TypeIdentity)
     { return {}; }
 
@@ -2928,6 +2938,33 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       using type = decltype(_S_test<_Functor, _ArgTypes...>(0));
     };
 
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_invoke)
+  // Like std::declval, but cheaper to instantiate.  Only for use in
+  // unevaluated __builtin_invoke expressions.
+  template<typename _Tp>
+    _Tp&& __invoke_declval() noexcept;
+
+  template<typename _Void, typename _Functor, typename... _ArgTypes>
+    struct __invoke_result_builtin
+    { };
+
+  template<typename _Functor, typename... _ArgTypes>
+    struct __invoke_result_builtin<
+       __void_t<decltype(__builtin_invoke(std::__invoke_declval<_Functor>(),
+                               std::__invoke_declval<_ArgTypes>()...))>,
+       _Functor, _ArgTypes...>
+    {
+      using type = decltype(__builtin_invoke(
+                             std::__invoke_declval<_Functor>(),
+                             std::__invoke_declval<_ArgTypes>()...));
+    };
+
+  // __invoke_result (std::invoke_result for C++11)
+  template<typename _Functor, typename... _ArgTypes>
+    struct __invoke_result
+    : public __invoke_result_builtin<void, _Functor, _ArgTypes...>
+    { };
+#else
   // __invoke_result (std::invoke_result for C++11)
   template<typename _Functor, typename... _ArgTypes>
     struct __invoke_result
@@ -2941,6 +2978,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        _Functor, _ArgTypes...
       >::type
     { };
+#endif
 
   // __invoke_result_t (std::invoke_result_t for C++11)
   template<typename _Fn, typename... _Args>
@@ -3390,9 +3428,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
   template<typename _Result, typename _Fn, typename... _Args>
     struct __call_is_nothrow
+#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_invoke)
+    : __bool_constant<noexcept(__builtin_invoke(
+                       std::__invoke_declval<_Fn>(),
+                       std::__invoke_declval<_Args>()...))>
+#else
     : __bool_constant<
        std::__call_is_nt<_Fn, _Args...>(typename _Result::__invoke_type{})
       >
+#endif
     { };
 
   template<typename _Fn, typename... _Args>
@@ -3422,15 +3466,30 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   /// @endcond
 
 #ifdef __cpp_lib_is_invocable // C++ >= 17
+  /// @cond undocumented
+  // A cheaper std::__is_complete_or_unbounded(__type_identity<_Tp>{}) for
+  // the static assertions below.  Unlike that function the result is
+  // memoized, which is harmless here because a false result is an error.
+  // sizeof is only used for possibly complete object types, so that e.g.
+  // the referenced type of a reference is not instantiated.
+  template<typename _Tp, typename = void>
+    constexpr bool __is_complete_or_unbounded_v = false;
+
+  template<typename _Tp>
+    constexpr bool
+    __is_complete_or_unbounded_v<_Tp, decltype(void(sizeof(
+       __conditional_t<__maybe_complete_object_type<_Tp>::value, _Tp, char>)))>
+      = true;
+  /// @endcond
+
   /// std::invoke_result
   template<typename _Functor, typename... _ArgTypes>
     struct invoke_result
     : public __invoke_result<_Functor, _ArgTypes...>
     {
-      
static_assert(std::__is_complete_or_unbounded(__type_identity<_Functor>{}),
+      static_assert(std::__is_complete_or_unbounded_v<_Functor>,
        "_Functor must be a complete class or an unbounded array");
-      static_assert((std::__is_complete_or_unbounded(
-       __type_identity<_ArgTypes>{}) && ...),
+      static_assert((std::__is_complete_or_unbounded_v<_ArgTypes> && ...),
        "each argument type must be a complete class or an unbounded array");
     };
 
@@ -3447,10 +3506,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     : __is_invocable_impl<__invoke_result<_Fn, _ArgTypes...>, void>::type
 #endif
     {
-      static_assert(std::__is_complete_or_unbounded(__type_identity<_Fn>{}),
+      static_assert(std::__is_complete_or_unbounded_v<_Fn>,
        "_Fn must be a complete class or an unbounded array");
-      static_assert((std::__is_complete_or_unbounded(
-       __type_identity<_ArgTypes>{}) && ...),
+      static_assert((std::__is_complete_or_unbounded_v<_ArgTypes> && ...),
        "each argument type must be a complete class or an unbounded array");
     };
 
@@ -3459,12 +3517,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     struct is_invocable_r
     : __is_invocable_impl<__invoke_result<_Fn, _ArgTypes...>, _Ret>::type
     {
-      static_assert(std::__is_complete_or_unbounded(__type_identity<_Fn>{}),
+      static_assert(std::__is_complete_or_unbounded_v<_Fn>,
        "_Fn must be a complete class or an unbounded array");
-      static_assert((std::__is_complete_or_unbounded(
-       __type_identity<_ArgTypes>{}) && ...),
+      static_assert((std::__is_complete_or_unbounded_v<_ArgTypes> && ...),
        "each argument type must be a complete class or an unbounded array");
-      static_assert(std::__is_complete_or_unbounded(__type_identity<_Ret>{}),
+      static_assert(std::__is_complete_or_unbounded_v<_Ret>,
        "_Ret must be a complete class or an unbounded array");
     };
 
@@ -3478,10 +3535,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
             __call_is_nothrow_<_Fn, _ArgTypes...>>::type
 #endif
     {
-      static_assert(std::__is_complete_or_unbounded(__type_identity<_Fn>{}),
+      static_assert(std::__is_complete_or_unbounded_v<_Fn>,
        "_Fn must be a complete class or an unbounded array");
-      static_assert((std::__is_complete_or_unbounded(
-       __type_identity<_ArgTypes>{}) && ...),
+      static_assert((std::__is_complete_or_unbounded_v<_ArgTypes> && ...),
        "each argument type must be a complete class or an unbounded array");
     };
 
@@ -3500,12 +3556,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     : __and_<__is_nt_invocable_impl<__invoke_result<_Fn, _ArgTypes...>, _Ret>,
              __call_is_nothrow_<_Fn, _ArgTypes...>>::type
     {
-      static_assert(std::__is_complete_or_unbounded(__type_identity<_Fn>{}),
+      static_assert(std::__is_complete_or_unbounded_v<_Fn>,
        "_Fn must be a complete class or an unbounded array");
-      static_assert((std::__is_complete_or_unbounded(
-       __type_identity<_ArgTypes>{}) && ...),
+      static_assert((std::__is_complete_or_unbounded_v<_ArgTypes> && ...),
        "each argument type must be a complete class or an unbounded array");
-      static_assert(std::__is_complete_or_unbounded(__type_identity<_Ret>{}),
+      static_assert(std::__is_complete_or_unbounded_v<_Ret>,
        "_Ret must be a complete class or an unbounded array");
     };
 #endif // __cpp_lib_is_invocable
diff --git a/libstdc++-v3/testsuite/20_util/function_objects/invoke/builtin.cc 
b/libstdc++-v3/testsuite/20_util/function_objects/invoke/builtin.cc
new file mode 100644
index 00000000..728d563e
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/function_objects/invoke/builtin.cc
@@ -0,0 +1,181 @@
+// { dg-do run { target c++17 } }
+
+// INVOKE semantics that must hold whether or not the library implements
+// INVOKE with __builtin_invoke.
+
+#include <functional>
+#include <memory>
+#include <tuple>
+#include <type_traits>
+#include <utility>
+#include <testsuite_hooks.h>
+
+struct B
+{
+  int d = 1;
+  int f(int) & noexcept { return 1; }
+  int f(int) && { return 2; }
+  int g(int) const volatile { return 3; }
+  constexpr int h(int x) const noexcept { return x; }
+};
+
+struct D : B { };
+
+struct Abs
+{
+  virtual int operator()(int) const = 0;
+};
+
+struct Throws
+{
+  int operator()() { return 0; }
+};
+
+struct Conv
+{
+  operator int() const noexcept(false) { return 0; }
+};
+
+using std::invoke_result_t;
+using std::is_invocable_v;
+using std::is_invocable_r_v;
+using std::is_nothrow_invocable_v;
+using std::is_nothrow_invocable_r_v;
+using std::is_same_v;
+
+// Functions and function objects.
+static_assert( is_invocable_v<void(int), int> );
+static_assert( is_invocable_v<void(&)(int), int> );
+static_assert( is_invocable_v<void(*)(int), int> );
+static_assert( ! is_invocable_v<void(int)> );
+static_assert( ! is_invocable_v<int> );
+static_assert( ! is_invocable_v<void> );
+static_assert( ! is_invocable_v<void(*)(int), void> );
+static_assert( is_invocable_v<const Abs&, int> );
+static_assert( is_same_v<invoke_result_t<const Abs&, int>, int> );
+static_assert( is_invocable_v<Throws> );
+static_assert( ! is_nothrow_invocable_v<Throws> );
+static_assert( ! is_invocable_v<const Throws> );
+static_assert( is_nothrow_invocable_v<void(*)() noexcept> );
+static_assert( ! is_nothrow_invocable_v<void(*)()> );
+
+// Pointers to member functions: ref-qualifiers, cv-qualifiers, derived
+// classes, pointers, smart pointers and reference_wrapper.
+using F = int (B::*)(int) & noexcept;
+using FR = int (B::*)(int) &&;
+static_assert( is_invocable_v<F, B&, int> );
+static_assert( ! is_invocable_v<F, B&&, int> );
+static_assert( is_invocable_v<FR, B&&, int> );
+static_assert( ! is_invocable_v<FR, B&, int> );
+static_assert( is_invocable_v<F, D&, int> );
+static_assert( is_invocable_v<F, B*, int> );
+static_assert( is_invocable_v<F, D*, int> );
+static_assert( is_invocable_v<F, std::unique_ptr<B>&, int> );
+static_assert( is_invocable_v<F, const std::shared_ptr<D>&, int> );
+static_assert( is_invocable_v<F, std::reference_wrapper<B>, int> );
+static_assert( is_invocable_v<F, std::reference_wrapper<D>, int> );
+static_assert( ! is_invocable_v<F, std::reference_wrapper<const B>, int> );
+static_assert( is_nothrow_invocable_v<F, B&, int> );
+static_assert( is_nothrow_invocable_v<F, std::reference_wrapper<B>, int> );
+static_assert( ! is_nothrow_invocable_v<FR, B&&, int> );
+static_assert( is_invocable_v<decltype(&B::g), const volatile B&, int> );
+static_assert( is_invocable_v<decltype(&B::g) const volatile&, B&, int> );
+static_assert( ! is_invocable_v<F, const B&, int> );
+static_assert( ! is_invocable_v<F, int*, int> );
+static_assert( ! is_invocable_v<F, B&> );
+static_assert( ! is_invocable_v<F> );
+
+// Pointers to data members.
+using M = decltype(&B::d);
+static_assert( is_same_v<invoke_result_t<M, B&>, int&> );
+static_assert( is_same_v<invoke_result_t<M, const B&>, const int&> );
+static_assert( is_same_v<invoke_result_t<M, B&&>, int&&> );
+static_assert( is_same_v<invoke_result_t<M, B>, int&&> );
+static_assert( is_same_v<invoke_result_t<M, B*>, int&> );
+static_assert( is_same_v<invoke_result_t<M, const B*>, const int&> );
+static_assert( is_same_v<invoke_result_t<M, D&>, int&> );
+static_assert( is_same_v<invoke_result_t<M, std::reference_wrapper<B>>,
+                        int&> );
+static_assert( is_same_v<invoke_result_t<M, std::reference_wrapper<const B>>,
+                        const int&> );
+static_assert( is_same_v<invoke_result_t<M, std::unique_ptr<B>>, int&> );
+static_assert( ! is_invocable_v<M, B&, int> );
+static_assert( ! is_invocable_v<M> );
+static_assert( is_nothrow_invocable_v<M, B&> );
+
+// LWG 3655. The INVOKE operation and union types
+union U { int i; long l; };
+static_assert( is_same_v<invoke_result_t<int U::*, U&>, int&> );
+static_assert( is_same_v<invoke_result_t<int U::*, const U*>, const int&> );
+static_assert( is_invocable_v<int U::*, std::reference_wrapper<U>> );
+
+// INVOKE<R>.
+static_assert( is_invocable_r_v<long, F, B&, int> );
+static_assert( is_invocable_r_v<void, F, B&, int> );
+static_assert( ! is_invocable_r_v<B, F, B&, int> );
+static_assert( is_invocable_r_v<int, Conv(*)()> );
+static_assert( ! is_nothrow_invocable_r_v<int, Conv(*)() noexcept> );
+static_assert( is_nothrow_invocable_r_v<void, Conv(*)() noexcept> );
+
+// std::invoke: value categories, noexcept, constexpr and SFINAE.
+constexpr B cb{};
+#if __cplusplus > 201703L
+static_assert( std::invoke(&B::h, cb, 7) == 7 );
+static_assert( std::invoke(&B::d, cb) == 1 );
+static_assert( std::invoke([](int x) { return x * 2; }, 21) == 42 );
+#endif
+static_assert( noexcept(std::invoke(&B::h, cb, 7)) );
+static_assert( ! noexcept(std::invoke(Throws{})) );
+static_assert( is_same_v<decltype(std::invoke(&B::d, std::declval<B&>())),
+                        int&> );
+static_assert( is_same_v<decltype(std::invoke(&B::d, std::declval<B&&>())),
+                        int&&> );
+
+template<typename... A>
+  constexpr auto
+  try_invoke(int) -> decltype(std::invoke(std::declval<A>()...), true)
+  { return true; }
+
+template<typename...>
+  constexpr bool
+  try_invoke(...)
+  { return false; }
+
+static_assert( try_invoke<F, B&, int>(0) );
+static_assert( ! try_invoke<F, const B&, int>(0) );
+static_assert( ! try_invoke<int>(0) );
+
+// Library components that use std::__invoke.
+static_assert( is_same_v<decltype(std::mem_fn(&B::d)(std::declval<B&>())),
+                        int&> );
+
+void
+test01()
+{
+  B b;
+  D d;
+  auto up = std::make_unique<B>();
+  auto volatile vf = &B::h;
+  std::function<int(B&, int)> fn = static_cast<F>(&B::f);
+  auto bound = std::bind(&B::h, std::ref(b), std::placeholders::_1);
+  auto lam = [](int x) { return x; };
+  std::function<int(int)> fr = std::ref(lam);
+
+  VERIFY( std::invoke(static_cast<F>(&B::f), std::ref(d), 0) == 1 );
+  VERIFY( std::invoke(static_cast<FR>(&B::f), std::move(b), 0) == 2 );
+  VERIFY( std::invoke(&B::g, up, 0) == 3 );
+  VERIFY( std::invoke(&B::d, up) == 1 );
+  VERIFY( std::invoke(vf, b, 5) == 5 );
+  VERIFY( fn(b, 0) == 1 );
+  VERIFY( bound(4) == 4 );
+  VERIFY( fr(5) == 5 );
+  VERIFY( std::apply(&B::h, std::make_tuple(b, 6)) == 6 );
+  std::invoke(&B::d, b) = 7;
+  VERIFY( b.d == 7 );
+}
+
+int
+main()
+{
+  test01();
+}
diff --git a/libstdc++-v3/testsuite/20_util/function_objects/invoke/builtin2.cc 
b/libstdc++-v3/testsuite/20_util/function_objects/invoke/builtin2.cc
new file mode 100644
index 00000000..c5c66982
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/function_objects/invoke/builtin2.cc
@@ -0,0 +1,104 @@
+// { dg-do run { target c++11 } }
+
+// INVOKE and INVOKE<R> as used by C++11 library components.  These must
+// behave the same whether or not the library implements INVOKE with
+// __builtin_invoke.
+
+#include <functional>
+#include <memory>
+#include <type_traits>
+#include <testsuite_hooks.h>
+
+struct B
+{
+  int d = 1;
+  int f(int) & noexcept { return 1; }
+  int f(int) && { return 2; }
+  long g(int) const { return 3; }
+};
+
+struct D : B { };
+
+struct Fn
+{
+  int operator()(int) noexcept { return 4; }
+  int operator()(int, int) { return 5; }
+};
+
+using F = int (B::*)(int) &;
+using FR = int (B::*)(int) &&;
+using M = int B::*;
+
+static_assert( std::is_same<std::__invoke_result<F, B&, int>::type,
+                           int>::value, "" );
+static_assert( std::is_same<std::__invoke_result<FR, B&&, int>::type,
+                           int>::value, "" );
+static_assert( std::is_same<std::__invoke_result<M, const B&>::type,
+                           const int&>::value, "" );
+static_assert( std::is_same<std::__invoke_result<M, B&&>::type,
+                           int&&>::value, "" );
+static_assert( std::is_same<std::__invoke_result<M,
+                                       std::reference_wrapper<B>>::type,
+                           int&>::value, "" );
+static_assert( std::is_same<std::__invoke_result<M, B*>::type,
+                           int&>::value, "" );
+
+static_assert( noexcept(std::__invoke(std::declval<Fn&>(), 1)), "" );
+static_assert( ! noexcept(std::__invoke(std::declval<Fn&>(), 1, 2)), "" );
+static_assert( ! noexcept(std::__invoke(std::declval<FR>(),
+                                       std::declval<B>(), 0)), "" );
+#if __cpp_noexcept_function_type
+using FN = int (B::*)(int) & noexcept;
+static_assert( noexcept(std::__invoke(std::declval<FN>(),
+                                     std::declval<B&>(), 0)), "" );
+static_assert( noexcept(std::__invoke(std::declval<FN>(),
+                                     std::declval<std::reference_wrapper<D>>(),
+                                     0)), "" );
+#endif
+
+// reference_wrapper::operator() uses __is_nothrow_invocable.
+static_assert( noexcept(std::declval<std::reference_wrapper<Fn>>()(1)), "" );
+static_assert( ! noexcept(std::declval<std::reference_wrapper<Fn>>()(1, 2)),
+              "" );
+
+// std::function is constrained on INVOKE<R>.
+static_assert( std::is_constructible<std::function<long(B&, int)>, F>::value,
+              "" );
+static_assert( ! std::is_constructible<std::function<int(const B&, int)>,
+                                      F>::value, "" );
+static_assert( std::is_constructible<std::function<void(B&&, int)>,
+                                    FR>::value, "" );
+
+void
+test01()
+{
+  B b;
+  D d;
+  std::unique_ptr<B> up(new B);
+
+  std::function<long(B&, int)> f1 = static_cast<F>(&B::f);
+  VERIFY( f1(d, 0) == 1 );
+  std::function<int(B&&, int)> f2 = static_cast<FR>(&B::f);
+  VERIFY( f2(std::move(b), 0) == 2 );
+  std::function<void(const B&, int)> f3 = &B::g;
+  f3(b, 0);
+  std::function<int&(B*)> f4 = &B::d;
+  f4(&b) = 6;
+  VERIFY( b.d == 6 );
+
+  VERIFY( std::mem_fn(&B::g)(up, 0) == 3 );
+  VERIFY( std::mem_fn(&B::d)(*up) == 1 );
+  VERIFY( std::bind(&B::g, std::ref(d), std::placeholders::_1)(0) == 3 );
+  VERIFY( std::bind<long>(Fn(), 0)() == 4 );
+  std::bind<void>(Fn(), 0, 1)();
+
+  Fn fn;
+  VERIFY( std::ref(fn)(1) == 4 );
+  VERIFY( std::ref(fn)(1, 2) == 5 );
+}
+
+int
+main()
+{
+  test01();
+}
-- 
2.53.1-Meta

Reply via email to