[v23,32/33] c++: Implement __is_invocable built-in trait

Message ID 20231020135748.1846670-33-kmatsui@gcc.gnu.org
State Unresolved
Headers
Series Optimize type traits performance |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Ken Matsui Oct. 20, 2023, 1:53 p.m. UTC
  This patch implements built-in trait for std::is_invocable.

gcc/cp/ChangeLog:

	* cp-trait.def: Define __is_invocable.
	* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_INVOCABLE.
	* semantics.cc (trait_expr_value): Likewise.
	(finish_trait_expr): Likewise.
	(is_invocable_p): New function.
	* method.h: New file to export build_trait_object in method.cc.

gcc/testsuite/ChangeLog:

	* g++.dg/ext/has-builtin-1.C: Test existence of __is_invocable.
	* g++.dg/ext/is_invocable1.C: New test.
	* g++.dg/ext/is_invocable2.C: New test.
	* g++.dg/ext/is_invocable3.C: New test.
	* g++.dg/ext/is_invocable4.C: New test.

Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
 gcc/cp/constraint.cc                     |   6 +
 gcc/cp/cp-trait.def                      |   1 +
 gcc/cp/method.h                          |  28 ++
 gcc/cp/semantics.cc                      | 135 +++++++++
 gcc/testsuite/g++.dg/ext/has-builtin-1.C |   3 +
 gcc/testsuite/g++.dg/ext/is_invocable1.C | 337 +++++++++++++++++++++++
 gcc/testsuite/g++.dg/ext/is_invocable2.C | 139 ++++++++++
 gcc/testsuite/g++.dg/ext/is_invocable3.C |  51 ++++
 gcc/testsuite/g++.dg/ext/is_invocable4.C |  33 +++
 9 files changed, 733 insertions(+)
 create mode 100644 gcc/cp/method.h
 create mode 100644 gcc/testsuite/g++.dg/ext/is_invocable1.C
 create mode 100644 gcc/testsuite/g++.dg/ext/is_invocable2.C
 create mode 100644 gcc/testsuite/g++.dg/ext/is_invocable3.C
 create mode 100644 gcc/testsuite/g++.dg/ext/is_invocable4.C
  

Comments

Patrick Palka Oct. 20, 2023, 9:29 p.m. UTC | #1
On Fri, 20 Oct 2023, Ken Matsui wrote:

> This patch implements built-in trait for std::is_invocable.

Nice!  My email client unfortunately ate my first review attempt, so
apologies for my brevity this time around.

> gcc/cp/ChangeLog:
> 
> 	* cp-trait.def: Define __is_invocable.
> 	* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_INVOCABLE.
> 	* semantics.cc (trait_expr_value): Likewise.
> 	(finish_trait_expr): Likewise.
> 	(is_invocable_p): New function.
> 	* method.h: New file to export build_trait_object in method.cc.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/ext/has-builtin-1.C: Test existence of __is_invocable.
> 	* g++.dg/ext/is_invocable1.C: New test.
> 	* g++.dg/ext/is_invocable2.C: New test.
> 	* g++.dg/ext/is_invocable3.C: New test.
> 	* g++.dg/ext/is_invocable4.C: New test.
> 
> Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
> ---
>  gcc/cp/constraint.cc                     |   6 +
>  gcc/cp/cp-trait.def                      |   1 +
>  gcc/cp/method.h                          |  28 ++
>  gcc/cp/semantics.cc                      | 135 +++++++++
>  gcc/testsuite/g++.dg/ext/has-builtin-1.C |   3 +
>  gcc/testsuite/g++.dg/ext/is_invocable1.C | 337 +++++++++++++++++++++++
>  gcc/testsuite/g++.dg/ext/is_invocable2.C | 139 ++++++++++
>  gcc/testsuite/g++.dg/ext/is_invocable3.C |  51 ++++
>  gcc/testsuite/g++.dg/ext/is_invocable4.C |  33 +++
>  9 files changed, 733 insertions(+)
>  create mode 100644 gcc/cp/method.h
>  create mode 100644 gcc/testsuite/g++.dg/ext/is_invocable1.C
>  create mode 100644 gcc/testsuite/g++.dg/ext/is_invocable2.C
>  create mode 100644 gcc/testsuite/g++.dg/ext/is_invocable3.C
>  create mode 100644 gcc/testsuite/g++.dg/ext/is_invocable4.C
> 
> diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
> index 9fce36e12d1..29bf548d30a 100644
> --- a/gcc/cp/constraint.cc
> +++ b/gcc/cp/constraint.cc
> @@ -3754,6 +3754,12 @@ diagnose_trait_expr (tree expr, tree args)
>      case CPTK_IS_FUNCTION:
>        inform (loc, "  %qT is not a function", t1);
>        break;
> +    case CPTK_IS_INVOCABLE:
> +      if (!t2)
> +    inform (loc, "  %qT is not invocable", t1);
> +      else
> +    inform (loc, "  %qT is not invocable by %qE", t1, t2);
> +      break;
>      case CPTK_IS_LAYOUT_COMPATIBLE:
>        inform (loc, "  %qT is not layout compatible with %qT", t1, t2);
>        break;
> diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
> index 05514a51c21..b8b7608c122 100644
> --- a/gcc/cp/cp-trait.def
> +++ b/gcc/cp/cp-trait.def
> @@ -71,6 +71,7 @@ DEFTRAIT_EXPR (IS_EMPTY, "__is_empty", 1)
>  DEFTRAIT_EXPR (IS_ENUM, "__is_enum", 1)
>  DEFTRAIT_EXPR (IS_FINAL, "__is_final", 1)
>  DEFTRAIT_EXPR (IS_FUNCTION, "__is_function", 1)
> +DEFTRAIT_EXPR (IS_INVOCABLE, "__is_invocable", -1)
>  DEFTRAIT_EXPR (IS_LAYOUT_COMPATIBLE, "__is_layout_compatible", 2)
>  DEFTRAIT_EXPR (IS_LITERAL_TYPE, "__is_literal_type", 1)
>  DEFTRAIT_EXPR (IS_MEMBER_FUNCTION_POINTER, "__is_member_function_pointer", 1)
> diff --git a/gcc/cp/method.h b/gcc/cp/method.h
> new file mode 100644
> index 00000000000..1aec8ec5cfd
> --- /dev/null
> +++ b/gcc/cp/method.h
> @@ -0,0 +1,28 @@
> +/* Functions exported by method.cc.
> +   Copyright (C) 2023 Free Software Foundation, Inc.
> +
> +This file is part of GCC.
> +
> +GCC is free software; you can redistribute it and/or modify
> +it under the terms of the GNU General Public License as published by
> +the Free Software Foundation; either version 3, or (at your option)
> +any later version.
> +
> +GCC is distributed in the hope that it will be useful,
> +but WITHOUT ANY WARRANTY; without even the implied warranty of
> +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +GNU General Public License for more details.
> +
> +You should have received a copy of the GNU General Public License
> +along with GCC; see the file COPYING3.  If not see
> +<http://www.gnu.org/licenses/>.  */
> +
> +#ifndef GCC_CP_METHOD_H
> +#define GCC_CP_METHOD_H 1
> +
> +#include "tree.h"
> +
> +/* In method.cc  */
> +extern tree build_trait_object (tree type);

Since other method.cc exports are already declared in cp-tree.h, for now
let's just declare this in cp-tree.h as well (under build_stub_object)
instead of creating a new header file.

> +
> +#endif  /* GCC_CP_METHOD_H  */
> diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
> index 7cccbae5287..cc2e400531a 100644
> --- a/gcc/cp/semantics.cc
> +++ b/gcc/cp/semantics.cc
> @@ -45,6 +45,10 @@ along with GCC; see the file COPYING3.  If not see
>  #include "gomp-constants.h"
>  #include "predict.h"
>  #include "memmodel.h"
> +#include "method.h"
> +
> +#include "print-tree.h"
> +#include "tree-pretty-print.h"
>  
>  /* There routines provide a modular interface to perform many parsing
>     operations.  They may therefore be used during actual parsing, or
> @@ -11714,6 +11718,133 @@ classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
>    return saw_copy;
>  }
>  
> +/* Return true if FN_TYPE is invocable with the given ARG_TYPES.  */
> +
> +static bool
> +is_invocable_p (tree fn_type, tree arg_types)
> +{
> +  /* ARG_TYPES must be a TREE_VEC.  */
> +  gcc_assert (TREE_CODE (arg_types) == TREE_VEC);
> +
> +  /* Access check is required to determine if the given is invocable.  */
> +  deferring_access_check_sentinel acs (dk_no_deferred);
> +
> +  /* std::is_invocable is an unevaluated context.  */
> +  cp_unevaluated cp_uneval_guard;
> +
> +  bool is_ptrdatamem;
> +  bool is_ptrmemfunc;
> +  if (TREE_CODE (fn_type) == REFERENCE_TYPE)
> +    {
> +      tree deref_fn_type = TREE_TYPE (fn_type);
> +      is_ptrdatamem = TYPE_PTRDATAMEM_P (deref_fn_type);
> +      is_ptrmemfunc = TYPE_PTRMEMFUNC_P (deref_fn_type);
> +
> +      /* Dereference fn_type if it is a pointer to member.  */
> +      if (is_ptrdatamem || is_ptrmemfunc)
> +	fn_type = deref_fn_type;
> +    }
> +  else
> +    {
> +      is_ptrdatamem = TYPE_PTRDATAMEM_P (fn_type);
> +      is_ptrmemfunc = TYPE_PTRMEMFUNC_P (fn_type);
> +    }
> +
> +  if (is_ptrdatamem && TREE_VEC_LENGTH (arg_types) != 1)
> +    /* A pointer to data member with non-one argument is not invocable.  */
> +    return false;
> +
> +  if (is_ptrmemfunc && TREE_VEC_LENGTH (arg_types) == 0)
> +    /* A pointer to member function with no arguments is not invocable.  */
> +    return false;
> +
> +  /* Construct an expression of a pointer to member.  */
> +  tree datum;
> +  if (is_ptrdatamem || is_ptrmemfunc)
> +    {
> +      tree datum_type = TREE_VEC_ELT (arg_types, 0);
> +
> +      /* Dereference datum.  */
> +      if (CLASS_TYPE_P (datum_type))
> +	{
> +	  bool is_refwrap = false;
> +
> +	  tree datum_decl = TYPE_NAME (TYPE_MAIN_VARIANT (datum_type));
> +	  if (decl_in_std_namespace_p (datum_decl))
> +	    {
> +	      tree name = DECL_NAME (datum_decl);
> +	      if (name && (id_equal (name, "reference_wrapper")))
> +		{
> +		  /* Handle std::reference_wrapper.  */
> +		  is_refwrap = true;
> +		  datum_type = cp_build_reference_type (datum_type, false);
> +		}
> +	    }
> +
> +	  datum = build_trait_object (datum_type);
> +
> +	  /* If datum_type was not std::reference_wrapper, check if it has
> +	     operator*() overload.  If datum_type was std::reference_wrapper,
> +	     avoid dereferencing the datum twice.  */
> +	  if (!is_refwrap)
> +	    if (get_class_binding (datum_type, get_identifier ("operator*")))

We probably should use lookup_member instead of get_class_binding since
IIUC the latter doesn't look into bases:

  struct A { int m; };
  struct B { A& operator*(): };
  struct C : B { };
  static_assert(std::is_invocable_v<int A::*, C>);

However, I notice that the specification of INVOKE
(https://eel.is/c++draft/func.require#lib:INVOKE) doesn't mention name
lookup at all so it strikes me as suspicious that we'd perform name
lookup here.  I think this would misbehave for:

  struct A { };
  struct B : A { A& operator*() = delete; };
  static_assert(std::is_invocable_v<int A::*, B>);

  struct C : private A { A& operator*(); };
  static_assert(std::is_invocable_v<int A::*, C>);

ultimately because we end up choosing the dereference form of INVOKE,
but according to 1.1/1.4 we should choose the non-dereference form?

> +	      /* Handle operator*().  */
> +	      datum = build_x_indirect_ref (UNKNOWN_LOCATION, datum,
> +					    RO_UNARY_STAR, NULL_TREE,
> +					    tf_none);
> +	}
> +      else if (POINTER_TYPE_P (datum_type))
> +	datum = build_trait_object (TREE_TYPE (datum_type));
> +      else
> +	datum = build_trait_object (datum_type);
> +    }
> +
> +  /* Build a function expression.  */
> +  tree fn;
> +  if (is_ptrdatamem)
> +    fn = build_m_component_ref (datum, build_trait_object (fn_type), tf_none);

Maybe exit early for the is_ptrdatamem case here (and simplify the rest
of the function accordingly)?

> +  else if (is_ptrmemfunc)
> +    fn = build_trait_object (TYPE_PTRMEMFUNC_FN_TYPE (fn_type));
> +  else
> +    fn = build_trait_object (fn_type);
> +
> +  /* Construct arguments to the function and an expression of a call.  */
> +  if (!is_ptrdatamem)
> +    {
> +      releasing_vec args;
> +
> +      if (is_ptrmemfunc)
> +	{
> +	  /* A pointer to member function is internally converted to a pointer
> +	     to function that takes a pointer to the dereferenced datum type
> +	     as its first argument and original arguments afterward.  If the
> +	     function is a const member function, the first argument also
> +	     requires a const datum pointer and vice-versa.  */
> +
> +	  tree datum_type = TREE_TYPE (datum);
> +	  if (TYPE_REF_P (datum_type))
> +	    datum_type = TREE_TYPE (datum_type);
> +
> +	  datum = build_trait_object (build_pointer_type (datum_type));
> +	  vec_safe_push (args, datum);
> +	}
> +
> +      for (int i = is_ptrmemfunc ? 1 : 0; i < TREE_VEC_LENGTH (arg_types); ++i)
> +	{
> +	  tree arg_type = TREE_VEC_ELT (arg_types, i);
> +	  tree arg = build_trait_object (arg_type);
> +	  vec_safe_push (args, arg);
> +	}
> +
> +      fn = finish_call_expr (fn, &args, false, false, tf_none);
> +    }
> +
> +  if (error_operand_p (fn))
> +    return false;
> +
> +  return true;
> +}
> +
>  /* Return true if DERIVED is pointer interconvertible base of BASE.  */
>  
>  static bool
> @@ -12181,6 +12312,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
>      case CPTK_IS_FUNCTION:
>        return type_code1 == FUNCTION_TYPE;
>  
> +    case CPTK_IS_INVOCABLE:
> +      return is_invocable_p (type1, type2);
> +
>      case CPTK_IS_LAYOUT_COMPATIBLE:
>        return layout_compatible_type_p (type1, type2);
>  
> @@ -12390,6 +12524,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
>        break;
>  
>      case CPTK_IS_CONVERTIBLE:
> +    case CPTK_IS_INVOCABLE:
>      case CPTK_IS_NOTHROW_ASSIGNABLE:
>      case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
>      case CPTK_IS_NOTHROW_CONVERTIBLE:
> diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> index b1430e9bd8b..3a9bda1ee03 100644
> --- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> +++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> @@ -92,6 +92,9 @@
>  #if !__has_builtin (__is_function)
>  # error "__has_builtin (__is_function) failed"
>  #endif
> +#if !__has_builtin (__is_invocable)
> +# error "__has_builtin (__is_invocable) failed"
> +#endif
>  #if !__has_builtin (__is_layout_compatible)
>  # error "__has_builtin (__is_layout_compatible) failed"
>  #endif
> diff --git a/gcc/testsuite/g++.dg/ext/is_invocable1.C b/gcc/testsuite/g++.dg/ext/is_invocable1.C
> new file mode 100644
> index 00000000000..2fd3906b571
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/ext/is_invocable1.C
> @@ -0,0 +1,337 @@
> +// { dg-do compile { target c++11 } }
> +
> +#define SA(X) static_assert((X),#X)
> +
> +using func_type_v0 = void(*)();
> +
> +SA(   __is_invocable( func_type_v0 ) );
> +SA( ! __is_invocable( func_type_v0, int ) );
> +
> +using func_type_i0 = int(*)();
> +
> +SA(   __is_invocable( func_type_i0 ) );
> +SA( ! __is_invocable( func_type_i0, int ) );
> +
> +using func_type_l0 = int&(*)();
> +
> +SA(   __is_invocable( func_type_l0 ) );
> +SA( ! __is_invocable( func_type_l0(int) ) );
> +
> +using func_type_ii = int(*)(int);
> +
> +SA( ! __is_invocable( func_type_ii ) );
> +SA(   __is_invocable( func_type_ii, int ) );
> +
> +using func_type_il = int(*)(int&);
> +
> +SA( ! __is_invocable( func_type_il ) );
> +SA( ! __is_invocable( func_type_il, int ) );
> +SA(   __is_invocable( func_type_il, int& ) );
> +
> +using func_type_ir = int(*)(int&&);
> +
> +SA( ! __is_invocable( func_type_ir ) );
> +SA( ! __is_invocable( func_type_ir, int& ) );
> +SA(   __is_invocable( func_type_ir, int ) );
> +SA(   __is_invocable( func_type_ir, int&& ) );
> +
> +struct A { };
> +
> +using mem_type_i = int A::*;
> +
> +SA( ! __is_invocable( mem_type_i ) );
> +SA( ! __is_invocable( mem_type_i, int ) );
> +SA( ! __is_invocable( mem_type_i, int* ) );
> +SA( ! __is_invocable( mem_type_i, int& ) );
> +SA( ! __is_invocable( mem_type_i, int&& ) );
> +SA(   __is_invocable( mem_type_i, A ) );
> +SA(   __is_invocable( mem_type_i, A* ) );
> +SA(   __is_invocable( mem_type_i, A& ) );
> +SA(   __is_invocable( mem_type_i, A&& ) );
> +SA(   __is_invocable( mem_type_i, const A& ) );
> +SA( ! __is_invocable( mem_type_i, A&, int ) );
> +
> +using memfun_type_i = int (A::*)();
> +
> +SA( ! __is_invocable( memfun_type_i ) );
> +SA( ! __is_invocable( memfun_type_i, int ) );
> +SA( ! __is_invocable( memfun_type_i, int* ) );
> +SA( ! __is_invocable( memfun_type_i, int& ) );
> +SA( ! __is_invocable( memfun_type_i, int&& ) );
> +SA(   __is_invocable( memfun_type_i, A ) );
> +SA(   __is_invocable( memfun_type_i, A* ) );
> +SA(   __is_invocable( memfun_type_i, A& ) );
> +SA(   __is_invocable( memfun_type_i, A&& ) );
> +SA( ! __is_invocable( memfun_type_i, const A& ) );
> +SA( ! __is_invocable( memfun_type_i, A&, int ) );
> +
> +using memfun_type_ic = int (A::*)() const;
> +
> +SA( ! __is_invocable( memfun_type_ic ) );
> +SA( ! __is_invocable( memfun_type_ic, int ) );
> +SA( ! __is_invocable( memfun_type_ic, int& ) );
> +SA(   __is_invocable( memfun_type_ic, A& ) );
> +SA(   __is_invocable( memfun_type_ic, A* ) );
> +SA( ! __is_invocable( memfun_type_ic, A&, int ) );
> +SA( ! __is_invocable( memfun_type_ic, A*, int& ) );
> +SA(   __is_invocable( memfun_type_ic, const A& ) );
> +SA(   __is_invocable( memfun_type_ic, const A* ) );
> +SA( ! __is_invocable( memfun_type_ic, const A&, int& ) );
> +SA( ! __is_invocable( memfun_type_ic, const A*, int ) );
> +
> +using memfun_type_iic = int& (A::*)(int&) const;
> +
> +SA( ! __is_invocable( memfun_type_iic ) );
> +SA( ! __is_invocable( memfun_type_iic, int ) );
> +SA( ! __is_invocable( memfun_type_iic, int& ) );
> +SA( ! __is_invocable( memfun_type_iic, A&, int ) );
> +SA(   __is_invocable( memfun_type_iic, A&, int& ) );
> +SA( ! __is_invocable( memfun_type_iic, A*, int ) );
> +SA(   __is_invocable( memfun_type_iic, A*, int& ) );
> +SA( ! __is_invocable( memfun_type_iic, const A&, int ) );
> +SA( ! __is_invocable( memfun_type_iic, const A&, int&, int ) );
> +SA(   __is_invocable( memfun_type_iic, const A&, int& ) );
> +SA(   __is_invocable( memfun_type_iic, const A*, int& ) );
> +
> +struct B {
> +  int& operator()();
> +  long& operator()() const;
> +  bool& operator()(int);
> +private:
> +  void operator()(int, int);
> +};
> +using CB = const B;
> +
> +SA(   __is_invocable( B ) );
> +SA(   __is_invocable( B& ) );
> +SA(   __is_invocable( B&& ) );
> +SA( ! __is_invocable( B* ) );
> +SA(   __is_invocable( CB ) );
> +SA(   __is_invocable( CB& ) );
> +SA( ! __is_invocable( CB* ) );
> +
> +SA(   __is_invocable( B, int ) );
> +SA(   __is_invocable( B&, int ) );
> +SA(   __is_invocable( B&&, int ) );
> +SA( ! __is_invocable( B*, int ) );
> +SA( ! __is_invocable( CB, int ) );
> +SA( ! __is_invocable( CB&, int ) );
> +SA( ! __is_invocable( CB*, int ) );
> +
> +SA( ! __is_invocable( B, int, int ) );
> +SA( ! __is_invocable( B&, int, int ) );
> +SA( ! __is_invocable( B&&, int, int ) );
> +SA( ! __is_invocable( B*, int, int ) );
> +SA( ! __is_invocable( CB, int, int ) );
> +SA( ! __is_invocable( CB&, int, int ) );
> +SA( ! __is_invocable( CB*, int, int ) );
> +
> +struct C : B { int& operator()() = delete; };
> +using CC = const C;
> +
> +SA( ! __is_invocable( C ) );
> +SA( ! __is_invocable( C& ) );
> +SA( ! __is_invocable( C&& ) );
> +SA( ! __is_invocable( C* ) );
> +SA( ! __is_invocable( CC ) );
> +SA( ! __is_invocable( CC& ) );
> +SA( ! __is_invocable( CC* ) );
> +
> +struct D { B operator*(); };
> +using CD = const D;
> +
> +SA( ! __is_invocable( D ) );
> +
> +struct E { void v(); };
> +using CE = const E;
> +
> +SA( ! __is_invocable( E ) );
> +SA( ! __is_invocable( void (E::*)() ) );
> +SA(   __is_invocable( void (E::*)(), E ) );
> +SA(   __is_invocable( void (E::*)(), E* ) );
> +SA( ! __is_invocable( void (E::*)(), CE ) );
> +
> +struct F : E {};
> +using CF = const F;
> +
> +SA( ! __is_invocable( F ) );
> +SA(   __is_invocable( void (E::*)(), F ) );
> +SA(   __is_invocable( void (E::*)(), F* ) );
> +SA( ! __is_invocable( void (E::*)(), CF ) );
> +
> +struct G { E operator*(); };
> +using CG = const G;
> +
> +SA( ! __is_invocable( G ) );
> +SA(   __is_invocable( void (E::*)(), G ) );
> +SA( ! __is_invocable( void (E::*)(), G* ) );
> +SA( ! __is_invocable( void (E::*)(), CG ) );
> +
> +struct H { E& operator*(); };
> +using CH = const H;
> +
> +SA( ! __is_invocable( H ) );
> +SA(   __is_invocable( void (E::*)(), H ) );
> +SA( ! __is_invocable( void (E::*)(), H* ) );
> +SA( ! __is_invocable( void (E::*)(), CH ) );
> +
> +struct I { E&& operator*(); };
> +using CI = const I;
> +
> +SA( ! __is_invocable( I ) );
> +SA(   __is_invocable( void (E::*)(), I ) );
> +SA( ! __is_invocable( void (E::*)(), I* ) );
> +SA( ! __is_invocable( void (E::*)(), CI ) );
> +
> +struct K { E* operator*(); };
> +using CK = const K;
> +
> +SA( ! __is_invocable( K ) );
> +SA( ! __is_invocable( void (E::*)(), K ) );
> +SA( ! __is_invocable( void (E::*)(), K* ) );
> +SA( ! __is_invocable( void (E::*)(), CK ) );
> +
> +struct L { CE operator*(); };
> +using CL = const L;
> +
> +SA( ! __is_invocable( L ) );
> +SA( ! __is_invocable( void (E::*)(), L ) );
> +SA( ! __is_invocable( void (E::*)(), L* ) );
> +SA( ! __is_invocable( void (E::*)(), CL ) );
> +
> +struct M {
> +  int i;
> +private:
> +  long l;
> +};
> +using CM = const M;
> +
> +SA( ! __is_invocable( M ) );
> +SA( ! __is_invocable( M& ) );
> +SA( ! __is_invocable( M&& ) );
> +SA( ! __is_invocable( M* ) );
> +SA( ! __is_invocable( CM ) );
> +SA( ! __is_invocable( CM& ) );
> +SA( ! __is_invocable( CM* ) );
> +
> +SA( ! __is_invocable( int M::* ) );
> +SA(   __is_invocable( int M::*, M ) );
> +SA(   __is_invocable( int M::*, M& ) );
> +SA(   __is_invocable( int M::*, M&& ) );
> +SA(   __is_invocable( int M::*, M* ) );
> +SA(   __is_invocable( int M::*, CM ) );
> +SA(   __is_invocable( int M::*, CM& ) );
> +SA(   __is_invocable( int M::*, CM* ) );
> +SA( ! __is_invocable( int M::*, int ) );
> +
> +SA( ! __is_invocable( int CM::* ) );
> +SA(   __is_invocable( int CM::*, M ) );
> +SA(   __is_invocable( int CM::*, M& ) );
> +SA(   __is_invocable( int CM::*, M&& ) );
> +SA(   __is_invocable( int CM::*, M* ) );
> +SA(   __is_invocable( int CM::*, CM ) );
> +SA(   __is_invocable( int CM::*, CM& ) );
> +SA(   __is_invocable( int CM::*, CM* ) );
> +SA( ! __is_invocable( int CM::*, int ) );
> +
> +SA( ! __is_invocable( long M::* ) );
> +SA(   __is_invocable( long M::*, M ) );
> +SA(   __is_invocable( long M::*, M& ) );
> +SA(   __is_invocable( long M::*, M&& ) );
> +SA(   __is_invocable( long M::*, M* ) );
> +SA(   __is_invocable( long M::*, CM ) );
> +SA(   __is_invocable( long M::*, CM& ) );
> +SA(   __is_invocable( long M::*, CM* ) );
> +SA( ! __is_invocable( long M::*, long ) );
> +
> +SA( ! __is_invocable( long CM::* ) );
> +SA(   __is_invocable( long CM::*, M ) );
> +SA(   __is_invocable( long CM::*, M& ) );
> +SA(   __is_invocable( long CM::*, M&& ) );
> +SA(   __is_invocable( long CM::*, M* ) );
> +SA(   __is_invocable( long CM::*, CM ) );
> +SA(   __is_invocable( long CM::*, CM& ) );
> +SA(   __is_invocable( long CM::*, CM* ) );
> +SA( ! __is_invocable( long CM::*, long ) );
> +
> +SA( ! __is_invocable( short M::* ) );
> +SA(   __is_invocable( short M::*, M ) );
> +SA(   __is_invocable( short M::*, M& ) );
> +SA(   __is_invocable( short M::*, M&& ) );
> +SA(   __is_invocable( short M::*, M* ) );
> +SA(   __is_invocable( short M::*, CM ) );
> +SA(   __is_invocable( short M::*, CM& ) );
> +SA(   __is_invocable( short M::*, CM* ) );
> +SA( ! __is_invocable( short M::*, short ) );
> +
> +SA( ! __is_invocable( short CM::* ) );
> +SA(   __is_invocable( short CM::*, M ) );
> +SA(   __is_invocable( short CM::*, M& ) );
> +SA(   __is_invocable( short CM::*, M&& ) );
> +SA(   __is_invocable( short CM::*, M* ) );
> +SA(   __is_invocable( short CM::*, CM ) );
> +SA(   __is_invocable( short CM::*, CM& ) );
> +SA(   __is_invocable( short CM::*, CM* ) );
> +SA( ! __is_invocable( short CM::*, short ) );
> +
> +struct N { M operator*(); };
> +SA(   __is_invocable( int M::*, N ) );
> +SA( ! __is_invocable( int M::*, N* ) );
> +
> +struct O { M& operator*(); };
> +SA(   __is_invocable( int M::*, O ) );
> +SA( ! __is_invocable( int M::*, O* ) );
> +
> +struct P { M&& operator*(); };
> +SA(   __is_invocable( int M::*, P ) );
> +SA( ! __is_invocable( int M::*, P* ) );
> +
> +struct Q { M* operator*(); };
> +SA( ! __is_invocable( int M::*, Q ) );
> +SA( ! __is_invocable( int M::*, Q* ) );
> +
> +struct R { void operator()(int = 0); };
> +
> +SA(   __is_invocable( R ) );
> +SA(   __is_invocable( R, int ) );
> +SA( ! __is_invocable( R, int, int ) );
> +
> +struct S { void operator()(int, ...); };
> +
> +SA( ! __is_invocable( S ) );
> +SA(   __is_invocable( S, int ) );
> +SA(   __is_invocable( S, int, int ) );
> +SA(   __is_invocable( S, int, int, int ) );
> +
> +void fn1() {}
> +
> +SA(   __is_invocable( decltype(fn1) ) );
> +
> +void fn2(int arr[10]);
> +
> +SA(   __is_invocable( decltype(fn2), int[10] ) );
> +SA(   __is_invocable( decltype(fn2), int(&)[10] ) );
> +SA(   __is_invocable( decltype(fn2), int(&&)[10] ) );
> +SA( ! __is_invocable( decltype(fn2), int(*)[10] ) );
> +SA( ! __is_invocable( decltype(fn2), int(*&)[10] ) );
> +SA( ! __is_invocable( decltype(fn2), int(*&&)[10] ) );
> +SA(   __is_invocable( decltype(fn2), int[] ) );
> +
> +auto lambda = []() {};
> +
> +SA(   __is_invocable( decltype(lambda) ) );
> +
> +template <typename Func, typename... Args>
> +struct can_invoke {
> +    static constexpr bool value = __is_invocable( Func, Args... );
> +};
> +
> +SA( can_invoke<decltype(lambda)>::value );
> +
> +struct T {
> +  void func() const {}
> +  int data;
> +};
> +
> +SA(   __is_invocable( decltype(&T::func)&, T& ) );
> +SA(   __is_invocable( decltype(&T::data)&, T& ) );
> diff --git a/gcc/testsuite/g++.dg/ext/is_invocable2.C b/gcc/testsuite/g++.dg/ext/is_invocable2.C
> new file mode 100644
> index 00000000000..a68aefd3e13
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/ext/is_invocable2.C
> @@ -0,0 +1,139 @@
> +// { dg-do compile { target c++11 } }
> +// __is_invocable should handle std::reference_wrapper correctly.
> +
> +#include <functional>
> +
> +#define SA(X) static_assert((X),#X)
> +
> +using std::reference_wrapper;
> +
> +using func_type_v0 = void(*)();
> +
> +SA(   __is_invocable( reference_wrapper<func_type_v0> ) );
> +SA( ! __is_invocable( reference_wrapper<func_type_v0>, int ) );
> +
> +using func_type_i0 = int(*)();
> +
> +SA(   __is_invocable( reference_wrapper<func_type_i0> ) );
> +SA( ! __is_invocable( reference_wrapper<func_type_i0>, int ) );
> +
> +using func_type_l0 = int&(*)();
> +
> +SA(   __is_invocable( reference_wrapper<func_type_l0> ) );
> +SA( ! __is_invocable( reference_wrapper<func_type_l0(int)> ) );
> +
> +using func_type_ii = int(*)(int);
> +
> +SA( ! __is_invocable( reference_wrapper<func_type_ii> ) );
> +SA(   __is_invocable( reference_wrapper<func_type_ii>, int ) );
> +
> +using func_type_il = int(*)(int&);
> +
> +SA( ! __is_invocable( reference_wrapper<func_type_il> ) );
> +SA( ! __is_invocable( reference_wrapper<func_type_il>, int ) );
> +SA(   __is_invocable( reference_wrapper<func_type_il>, int& ) );
> +
> +using func_type_ir = int(*)(int&&);
> +
> +SA( ! __is_invocable( reference_wrapper<func_type_ir> ) );
> +SA( ! __is_invocable( reference_wrapper<func_type_ir>, int& ) );
> +SA(   __is_invocable( reference_wrapper<func_type_ir>, int ) );
> +SA(   __is_invocable( reference_wrapper<func_type_ir>, int&& ) );
> +
> +struct A { };
> +
> +using mem_type_i = int A::*;
> +
> +SA( ! __is_invocable( reference_wrapper<mem_type_i> ) );
> +SA( ! __is_invocable( reference_wrapper<mem_type_i>, int ) );
> +SA( ! __is_invocable( reference_wrapper<mem_type_i>, int* ) );
> +SA( ! __is_invocable( reference_wrapper<mem_type_i>, int& ) );
> +SA( ! __is_invocable( reference_wrapper<mem_type_i>, int&& ) );
> +SA(   __is_invocable( reference_wrapper<mem_type_i>, A ) );
> +SA(   __is_invocable( reference_wrapper<mem_type_i>, A* ) );
> +SA(   __is_invocable( reference_wrapper<mem_type_i>, A& ) );
> +SA(   __is_invocable( reference_wrapper<mem_type_i>, A&& ) );
> +
> +using memfun_type_i = int (A::*)();
> +
> +SA( ! __is_invocable( reference_wrapper<memfun_type_i> ) );
> +SA( ! __is_invocable( reference_wrapper<memfun_type_i>, int ) );
> +SA( ! __is_invocable( reference_wrapper<memfun_type_i>, int* ) );
> +SA( ! __is_invocable( reference_wrapper<memfun_type_i>, int& ) );
> +SA( ! __is_invocable( reference_wrapper<memfun_type_i>, int&& ) );
> +SA(   __is_invocable( reference_wrapper<memfun_type_i>, A ) );
> +SA(   __is_invocable( reference_wrapper<memfun_type_i>, A* ) );
> +SA(   __is_invocable( reference_wrapper<memfun_type_i>, A& ) );
> +SA(   __is_invocable( reference_wrapper<memfun_type_i>, A&& ) );
> +SA( ! __is_invocable( reference_wrapper<memfun_type_i>, const A& ) );
> +SA( ! __is_invocable( reference_wrapper<memfun_type_i>, A&, int ) );
> +
> +using memfun_type_ic = int (A::*)() const;
> +
> +SA( ! __is_invocable( reference_wrapper<memfun_type_ic> ) );
> +SA( ! __is_invocable( reference_wrapper<memfun_type_ic>, int ) );
> +SA( ! __is_invocable( reference_wrapper<memfun_type_ic>, int& ) );
> +SA(   __is_invocable( reference_wrapper<memfun_type_ic>, A& ) );
> +SA(   __is_invocable( reference_wrapper<memfun_type_ic>, A* ) );
> +SA( ! __is_invocable( reference_wrapper<memfun_type_ic>, A&, int ) );
> +SA( ! __is_invocable( reference_wrapper<memfun_type_ic>, A*, int& ) );
> +SA(   __is_invocable( reference_wrapper<memfun_type_ic>, const A& ) );
> +SA(   __is_invocable( reference_wrapper<memfun_type_ic>, const A* ) );
> +SA( ! __is_invocable( reference_wrapper<memfun_type_ic>, const A&, int& ) );
> +SA( ! __is_invocable( reference_wrapper<memfun_type_ic>, const A*, int ) );
> +
> +using memfun_type_iic = int& (A::*)(int&) const;
> +
> +SA( ! __is_invocable( reference_wrapper<memfun_type_iic> ) );
> +SA( ! __is_invocable( reference_wrapper<memfun_type_iic>, int ) );
> +SA( ! __is_invocable( reference_wrapper<memfun_type_iic>, int& ) );
> +SA( ! __is_invocable( reference_wrapper<memfun_type_iic>, A&, int ) );
> +SA(   __is_invocable( reference_wrapper<memfun_type_iic>, A&, int& ) );
> +SA( ! __is_invocable( reference_wrapper<memfun_type_iic>, A*, int ) );
> +SA(   __is_invocable( reference_wrapper<memfun_type_iic>, A*, int& ) );
> +SA( ! __is_invocable( reference_wrapper<memfun_type_iic>, const A&, int ) );
> +SA( ! __is_invocable( reference_wrapper<memfun_type_iic>, const A&, int&, int ) );
> +SA(   __is_invocable( reference_wrapper<memfun_type_iic>, const A&, int& ) );
> +SA(   __is_invocable( reference_wrapper<memfun_type_iic>, const A*, int& ) );
> +
> +struct B {
> +  int& operator()();
> +  long& operator()() const;
> +  bool& operator()(int);
> +private:
> +  void operator()(int, int);
> +};
> +using CB = const B;
> +
> +SA(   __is_invocable( reference_wrapper<B> ) );
> +SA(   __is_invocable( reference_wrapper<B>& ) );
> +SA(   __is_invocable( reference_wrapper<B>&& ) );
> +SA(   __is_invocable( reference_wrapper<CB> ) );
> +SA(   __is_invocable( reference_wrapper<CB>& ) );
> +SA(   __is_invocable( reference_wrapper<B>, int ) );
> +SA( ! __is_invocable( reference_wrapper<B>&, int, int ) );
> +
> +struct C : B { int& operator()() = delete; };
> +using CC = const C;
> +
> +SA( ! __is_invocable( reference_wrapper<C> ) );
> +SA( ! __is_invocable( reference_wrapper<C>& ) );
> +SA( ! __is_invocable( reference_wrapper<C>&& ) );
> +SA( ! __is_invocable( reference_wrapper<CC> ) );
> +SA( ! __is_invocable( reference_wrapper<CC>& ) );
> +
> +struct D { B operator*(); };
> +using CD = const D;
> +
> +SA( ! __is_invocable( reference_wrapper<D> ) );
> +SA( ! __is_invocable( reference_wrapper<D>& ) );
> +SA( ! __is_invocable( reference_wrapper<D>&& ) );
> +SA( ! __is_invocable( reference_wrapper<D>* ) );
> +SA( ! __is_invocable( reference_wrapper<D*> ) );
> +SA( ! __is_invocable( reference_wrapper<D*>* ) );
> +
> +std::function<void()> fn = []() {};
> +auto refwrap = std::ref(fn);
> +
> +SA(   __is_invocable( decltype(fn) ) );
> +SA(   __is_invocable( decltype(refwrap) ) );
> diff --git a/gcc/testsuite/g++.dg/ext/is_invocable3.C b/gcc/testsuite/g++.dg/ext/is_invocable3.C
> new file mode 100644
> index 00000000000..e2b0c5ef406
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/ext/is_invocable3.C
> @@ -0,0 +1,51 @@
> +// { dg-do compile { target c++11 } }
> +// __is_invocable should handle incomplete class correctly.
> +
> +#define SA(X) static_assert((X),#X)
> +
> +struct Incomplete;
> +
> +SA( ! __is_invocable( Incomplete ) ); // { dg-error "incomplete type" }
> +SA( ! __is_invocable( Incomplete, int ) ); // { dg-error "incomplete type" }
> +
> +SA( ! __is_invocable( int, Incomplete, int ) ); // { dg-error "incomplete type" }
> +SA( ! __is_invocable( int, Incomplete ) ); // { dg-error "incomplete type" }
> +
> +SA( ! __is_invocable( Incomplete, Incomplete() ) ); // { dg-error "incomplete type" }
> +SA( ! __is_invocable( Incomplete, Incomplete(int), int ) ); // { dg-error "incomplete type" }
> +SA( ! __is_invocable( Incomplete, Incomplete(int, int), int, int ) ); // { dg-error "incomplete type" }
> +
> +SA( ! __is_invocable( Incomplete, Incomplete(), int, int ) ); // { dg-error "incomplete type" }
> +
> +SA( ! __is_invocable( int(Incomplete), Incomplete ) ); // { dg-error "incomplete type" }
> +SA( ! __is_invocable( int(int, Incomplete), int, Incomplete ) ); // { dg-error "incomplete type" }
> +SA( ! __is_invocable( int(int, Incomplete), Incomplete, int ) ); // { dg-error "incomplete type" }
> +
> +SA(   __is_invocable( int(Incomplete&), Incomplete& ) ); // { dg-bogus "incomplete type" }
> +SA(   __is_invocable( int(int, Incomplete&), int, Incomplete& ) ); // { dg-bogus "incomplete type" }
> +
> +SA(   __is_invocable( int(Incomplete&&), Incomplete&& ) ); // { dg-bogus "incomplete type" }
> +SA(   __is_invocable( int(int, Incomplete&&), int, Incomplete&& ) ); // { dg-bogus "incomplete type" }
> +
> +SA(   __is_invocable( int(const Incomplete&&), const Incomplete&& ) ); // { dg-bogus "incomplete type" }
> +SA(   __is_invocable( int(int, const Incomplete&&), int, const Incomplete&& ) ); // { dg-bogus "incomplete type" }
> +
> +SA(   __is_invocable( int(const Incomplete&), const Incomplete& ) ); // { dg-bogus "incomplete type" }
> +SA(   __is_invocable( int(int, const Incomplete&), int, const Incomplete& ) ); // { dg-bogus "incomplete type" }
> +
> +SA(   __is_invocable( int(const Incomplete&), Incomplete& ) ); // { dg-bogus "incomplete type" }
> +SA(   __is_invocable( int(int, const Incomplete&), int, Incomplete& ) ); // { dg-bogus "incomplete type" }
> +
> +SA(   __is_invocable( int Incomplete::*, const Incomplete& ) ); // { dg-bogus "incomplete type" }
> +SA( ! __is_invocable( void (Incomplete::*)(long&), const Incomplete*, long& ) ); // { dg-bogus "incomplete type" }
> +SA(   __is_invocable( void (Incomplete::*)(long&) const, Incomplete*, long& ) ); // { dg-bogus "incomplete type" }
> +
> +template <typename T>
> +struct Holder { T t; };
> +
> +SA(   __is_invocable( int(Holder<Incomplete>&), Holder<Incomplete>& ) ); // { dg-bogus "incomplete type" }
> +
> +// Define Incomplete, which is now not incomplete.
> +struct Incomplete { void operator()(); };
> +
> +SA( __is_invocable( Incomplete ) ); // { dg-bogus "incomplete type" }
> diff --git a/gcc/testsuite/g++.dg/ext/is_invocable4.C b/gcc/testsuite/g++.dg/ext/is_invocable4.C
> new file mode 100644
> index 00000000000..d1efccf08f8
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/ext/is_invocable4.C
> @@ -0,0 +1,33 @@
> +// { dg-do compile { target c++11 } }
> +// Failed access check should be a substitution failure, not an error.
> +
> +#define SA(X) static_assert((X),#X)
> +
> +template<bool B>
> +struct bool_constant { static constexpr bool value = B; };
> +
> +template<typename _Fn, typename... _ArgTypes>
> +struct is_invocable
> +: public bool_constant<__is_invocable(_Fn, _ArgTypes...)>
> +{ };
> +
> +#if __cpp_variable_templates
> +template<typename _Fn, typename... _ArgTypes>
> +constexpr bool is_invocable_v = __is_invocable(_Fn, _ArgTypes...);
> +#endif
> +
> +class Private
> +{
> +  void operator()() const
> +  {
> +    SA( ! is_invocable<Private>::value );
> +#if __cpp_variable_templates
> +    SA( ! is_invocable_v<Private> );
> +#endif
> +  }
> +};
> +
> +SA( ! is_invocable<Private>::value );
> +#if __cpp_variable_templates
> +SA( ! is_invocable_v<Private> );
> +#endif
> -- 
> 2.42.0
> 
>
  
Patrick Palka Oct. 20, 2023, 9:31 p.m. UTC | #2
On Fri, 20 Oct 2023, Patrick Palka wrote:

> On Fri, 20 Oct 2023, Ken Matsui wrote:
> 
> > This patch implements built-in trait for std::is_invocable.
> 
> Nice!  My email client unfortunately ate my first review attempt, so
> apologies for my brevity this time around.
> 
> > gcc/cp/ChangeLog:
> > 
> > 	* cp-trait.def: Define __is_invocable.
> > 	* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_INVOCABLE.
> > 	* semantics.cc (trait_expr_value): Likewise.
> > 	(finish_trait_expr): Likewise.
> > 	(is_invocable_p): New function.
> > 	* method.h: New file to export build_trait_object in method.cc.
> > 
> > gcc/testsuite/ChangeLog:
> > 
> > 	* g++.dg/ext/has-builtin-1.C: Test existence of __is_invocable.
> > 	* g++.dg/ext/is_invocable1.C: New test.
> > 	* g++.dg/ext/is_invocable2.C: New test.
> > 	* g++.dg/ext/is_invocable3.C: New test.
> > 	* g++.dg/ext/is_invocable4.C: New test.
> > 
> > Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
> > ---
> >  gcc/cp/constraint.cc                     |   6 +
> >  gcc/cp/cp-trait.def                      |   1 +
> >  gcc/cp/method.h                          |  28 ++
> >  gcc/cp/semantics.cc                      | 135 +++++++++
> >  gcc/testsuite/g++.dg/ext/has-builtin-1.C |   3 +
> >  gcc/testsuite/g++.dg/ext/is_invocable1.C | 337 +++++++++++++++++++++++
> >  gcc/testsuite/g++.dg/ext/is_invocable2.C | 139 ++++++++++
> >  gcc/testsuite/g++.dg/ext/is_invocable3.C |  51 ++++
> >  gcc/testsuite/g++.dg/ext/is_invocable4.C |  33 +++
> >  9 files changed, 733 insertions(+)
> >  create mode 100644 gcc/cp/method.h
> >  create mode 100644 gcc/testsuite/g++.dg/ext/is_invocable1.C
> >  create mode 100644 gcc/testsuite/g++.dg/ext/is_invocable2.C
> >  create mode 100644 gcc/testsuite/g++.dg/ext/is_invocable3.C
> >  create mode 100644 gcc/testsuite/g++.dg/ext/is_invocable4.C
> > 
> > diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
> > index 9fce36e12d1..29bf548d30a 100644
> > --- a/gcc/cp/constraint.cc
> > +++ b/gcc/cp/constraint.cc
> > @@ -3754,6 +3754,12 @@ diagnose_trait_expr (tree expr, tree args)
> >      case CPTK_IS_FUNCTION:
> >        inform (loc, "  %qT is not a function", t1);
> >        break;
> > +    case CPTK_IS_INVOCABLE:
> > +      if (!t2)
> > +    inform (loc, "  %qT is not invocable", t1);
> > +      else
> > +    inform (loc, "  %qT is not invocable by %qE", t1, t2);
> > +      break;
> >      case CPTK_IS_LAYOUT_COMPATIBLE:
> >        inform (loc, "  %qT is not layout compatible with %qT", t1, t2);
> >        break;
> > diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
> > index 05514a51c21..b8b7608c122 100644
> > --- a/gcc/cp/cp-trait.def
> > +++ b/gcc/cp/cp-trait.def
> > @@ -71,6 +71,7 @@ DEFTRAIT_EXPR (IS_EMPTY, "__is_empty", 1)
> >  DEFTRAIT_EXPR (IS_ENUM, "__is_enum", 1)
> >  DEFTRAIT_EXPR (IS_FINAL, "__is_final", 1)
> >  DEFTRAIT_EXPR (IS_FUNCTION, "__is_function", 1)
> > +DEFTRAIT_EXPR (IS_INVOCABLE, "__is_invocable", -1)
> >  DEFTRAIT_EXPR (IS_LAYOUT_COMPATIBLE, "__is_layout_compatible", 2)
> >  DEFTRAIT_EXPR (IS_LITERAL_TYPE, "__is_literal_type", 1)
> >  DEFTRAIT_EXPR (IS_MEMBER_FUNCTION_POINTER, "__is_member_function_pointer", 1)
> > diff --git a/gcc/cp/method.h b/gcc/cp/method.h
> > new file mode 100644
> > index 00000000000..1aec8ec5cfd
> > --- /dev/null
> > +++ b/gcc/cp/method.h
> > @@ -0,0 +1,28 @@
> > +/* Functions exported by method.cc.
> > +   Copyright (C) 2023 Free Software Foundation, Inc.
> > +
> > +This file is part of GCC.
> > +
> > +GCC is free software; you can redistribute it and/or modify
> > +it under the terms of the GNU General Public License as published by
> > +the Free Software Foundation; either version 3, or (at your option)
> > +any later version.
> > +
> > +GCC is distributed in the hope that it will be useful,
> > +but WITHOUT ANY WARRANTY; without even the implied warranty of
> > +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > +GNU General Public License for more details.
> > +
> > +You should have received a copy of the GNU General Public License
> > +along with GCC; see the file COPYING3.  If not see
> > +<http://www.gnu.org/licenses/>.  */
> > +
> > +#ifndef GCC_CP_METHOD_H
> > +#define GCC_CP_METHOD_H 1
> > +
> > +#include "tree.h"
> > +
> > +/* In method.cc  */
> > +extern tree build_trait_object (tree type);
> 
> Since other method.cc exports are already declared in cp-tree.h, for now
> let's just declare this in cp-tree.h as well (under build_stub_object)
> instead of creating a new header file.
> 
> > +
> > +#endif  /* GCC_CP_METHOD_H  */
> > diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
> > index 7cccbae5287..cc2e400531a 100644
> > --- a/gcc/cp/semantics.cc
> > +++ b/gcc/cp/semantics.cc
> > @@ -45,6 +45,10 @@ along with GCC; see the file COPYING3.  If not see
> >  #include "gomp-constants.h"
> >  #include "predict.h"
> >  #include "memmodel.h"
> > +#include "method.h"
> > +
> > +#include "print-tree.h"
> > +#include "tree-pretty-print.h"
> >  
> >  /* There routines provide a modular interface to perform many parsing
> >     operations.  They may therefore be used during actual parsing, or
> > @@ -11714,6 +11718,133 @@ classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
> >    return saw_copy;
> >  }
> >  
> > +/* Return true if FN_TYPE is invocable with the given ARG_TYPES.  */
> > +
> > +static bool
> > +is_invocable_p (tree fn_type, tree arg_types)
> > +{
> > +  /* ARG_TYPES must be a TREE_VEC.  */
> > +  gcc_assert (TREE_CODE (arg_types) == TREE_VEC);
> > +
> > +  /* Access check is required to determine if the given is invocable.  */
> > +  deferring_access_check_sentinel acs (dk_no_deferred);
> > +
> > +  /* std::is_invocable is an unevaluated context.  */
> > +  cp_unevaluated cp_uneval_guard;
> > +
> > +  bool is_ptrdatamem;
> > +  bool is_ptrmemfunc;
> > +  if (TREE_CODE (fn_type) == REFERENCE_TYPE)
> > +    {
> > +      tree deref_fn_type = TREE_TYPE (fn_type);
> > +      is_ptrdatamem = TYPE_PTRDATAMEM_P (deref_fn_type);
> > +      is_ptrmemfunc = TYPE_PTRMEMFUNC_P (deref_fn_type);
> > +
> > +      /* Dereference fn_type if it is a pointer to member.  */
> > +      if (is_ptrdatamem || is_ptrmemfunc)
> > +	fn_type = deref_fn_type;
> > +    }
> > +  else
> > +    {
> > +      is_ptrdatamem = TYPE_PTRDATAMEM_P (fn_type);
> > +      is_ptrmemfunc = TYPE_PTRMEMFUNC_P (fn_type);
> > +    }
> > +
> > +  if (is_ptrdatamem && TREE_VEC_LENGTH (arg_types) != 1)
> > +    /* A pointer to data member with non-one argument is not invocable.  */
> > +    return false;
> > +
> > +  if (is_ptrmemfunc && TREE_VEC_LENGTH (arg_types) == 0)
> > +    /* A pointer to member function with no arguments is not invocable.  */
> > +    return false;
> > +
> > +  /* Construct an expression of a pointer to member.  */
> > +  tree datum;
> > +  if (is_ptrdatamem || is_ptrmemfunc)
> > +    {
> > +      tree datum_type = TREE_VEC_ELT (arg_types, 0);
> > +
> > +      /* Dereference datum.  */
> > +      if (CLASS_TYPE_P (datum_type))
> > +	{
> > +	  bool is_refwrap = false;
> > +
> > +	  tree datum_decl = TYPE_NAME (TYPE_MAIN_VARIANT (datum_type));
> > +	  if (decl_in_std_namespace_p (datum_decl))
> > +	    {
> > +	      tree name = DECL_NAME (datum_decl);
> > +	      if (name && (id_equal (name, "reference_wrapper")))
> > +		{
> > +		  /* Handle std::reference_wrapper.  */
> > +		  is_refwrap = true;
> > +		  datum_type = cp_build_reference_type (datum_type, false);
> > +		}
> > +	    }
> > +
> > +	  datum = build_trait_object (datum_type);
> > +
> > +	  /* If datum_type was not std::reference_wrapper, check if it has
> > +	     operator*() overload.  If datum_type was std::reference_wrapper,
> > +	     avoid dereferencing the datum twice.  */
> > +	  if (!is_refwrap)
> > +	    if (get_class_binding (datum_type, get_identifier ("operator*")))
> 
> We probably should use lookup_member instead of get_class_binding since
> IIUC the latter doesn't look into bases:
> 
>   struct A { int m; };
>   struct B { A& operator*(): };
>   struct C : B { };
>   static_assert(std::is_invocable_v<int A::*, C>);
> 
> However, I notice that the specification of INVOKE
> (https://eel.is/c++draft/func.require#lib:INVOKE) doesn't mention name
> lookup at all so it strikes me as suspicious that we'd perform name
> lookup here.  I think this would misbehave for:
> 
>   struct A { };
>   struct B : A { A& operator*() = delete; };
>   static_assert(std::is_invocable_v<int A::*, B>);
> 
>   struct C : private A { A& operator*(); };
>   static_assert(std::is_invocable_v<int A::*, C>);

Oops, this static_assert is missing a !

> 
> ultimately because we end up choosing the dereference form of INVOKE,
> but according to 1.1/1.4 we should choose the non-dereference form?
> 
> > +	      /* Handle operator*().  */
> > +	      datum = build_x_indirect_ref (UNKNOWN_LOCATION, datum,
> > +					    RO_UNARY_STAR, NULL_TREE,
> > +					    tf_none);
> > +	}
> > +      else if (POINTER_TYPE_P (datum_type))
> > +	datum = build_trait_object (TREE_TYPE (datum_type));
> > +      else
> > +	datum = build_trait_object (datum_type);
> > +    }
> > +
> > +  /* Build a function expression.  */
> > +  tree fn;
> > +  if (is_ptrdatamem)
> > +    fn = build_m_component_ref (datum, build_trait_object (fn_type), tf_none);
> 
> Maybe exit early for the is_ptrdatamem case here (and simplify the rest
> of the function accordingly)?
> 
> > +  else if (is_ptrmemfunc)
> > +    fn = build_trait_object (TYPE_PTRMEMFUNC_FN_TYPE (fn_type));
> > +  else
> > +    fn = build_trait_object (fn_type);
> > +
> > +  /* Construct arguments to the function and an expression of a call.  */
> > +  if (!is_ptrdatamem)
> > +    {
> > +      releasing_vec args;
> > +
> > +      if (is_ptrmemfunc)
> > +	{
> > +	  /* A pointer to member function is internally converted to a pointer
> > +	     to function that takes a pointer to the dereferenced datum type
> > +	     as its first argument and original arguments afterward.  If the
> > +	     function is a const member function, the first argument also
> > +	     requires a const datum pointer and vice-versa.  */
> > +
> > +	  tree datum_type = TREE_TYPE (datum);
> > +	  if (TYPE_REF_P (datum_type))
> > +	    datum_type = TREE_TYPE (datum_type);
> > +
> > +	  datum = build_trait_object (build_pointer_type (datum_type));
> > +	  vec_safe_push (args, datum);
> > +	}
> > +
> > +      for (int i = is_ptrmemfunc ? 1 : 0; i < TREE_VEC_LENGTH (arg_types); ++i)
> > +	{
> > +	  tree arg_type = TREE_VEC_ELT (arg_types, i);
> > +	  tree arg = build_trait_object (arg_type);
> > +	  vec_safe_push (args, arg);
> > +	}
> > +
> > +      fn = finish_call_expr (fn, &args, false, false, tf_none);
> > +    }
> > +
> > +  if (error_operand_p (fn))
> > +    return false;
> > +
> > +  return true;
> > +}
> > +
> >  /* Return true if DERIVED is pointer interconvertible base of BASE.  */
> >  
> >  static bool
> > @@ -12181,6 +12312,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
> >      case CPTK_IS_FUNCTION:
> >        return type_code1 == FUNCTION_TYPE;
> >  
> > +    case CPTK_IS_INVOCABLE:
> > +      return is_invocable_p (type1, type2);
> > +
> >      case CPTK_IS_LAYOUT_COMPATIBLE:
> >        return layout_compatible_type_p (type1, type2);
> >  
> > @@ -12390,6 +12524,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
> >        break;
> >  
> >      case CPTK_IS_CONVERTIBLE:
> > +    case CPTK_IS_INVOCABLE:
> >      case CPTK_IS_NOTHROW_ASSIGNABLE:
> >      case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
> >      case CPTK_IS_NOTHROW_CONVERTIBLE:
> > diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> > index b1430e9bd8b..3a9bda1ee03 100644
> > --- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> > +++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> > @@ -92,6 +92,9 @@
> >  #if !__has_builtin (__is_function)
> >  # error "__has_builtin (__is_function) failed"
> >  #endif
> > +#if !__has_builtin (__is_invocable)
> > +# error "__has_builtin (__is_invocable) failed"
> > +#endif
> >  #if !__has_builtin (__is_layout_compatible)
> >  # error "__has_builtin (__is_layout_compatible) failed"
> >  #endif
> > diff --git a/gcc/testsuite/g++.dg/ext/is_invocable1.C b/gcc/testsuite/g++.dg/ext/is_invocable1.C
> > new file mode 100644
> > index 00000000000..2fd3906b571
> > --- /dev/null
> > +++ b/gcc/testsuite/g++.dg/ext/is_invocable1.C
> > @@ -0,0 +1,337 @@
> > +// { dg-do compile { target c++11 } }
> > +
> > +#define SA(X) static_assert((X),#X)
> > +
> > +using func_type_v0 = void(*)();
> > +
> > +SA(   __is_invocable( func_type_v0 ) );
> > +SA( ! __is_invocable( func_type_v0, int ) );
> > +
> > +using func_type_i0 = int(*)();
> > +
> > +SA(   __is_invocable( func_type_i0 ) );
> > +SA( ! __is_invocable( func_type_i0, int ) );
> > +
> > +using func_type_l0 = int&(*)();
> > +
> > +SA(   __is_invocable( func_type_l0 ) );
> > +SA( ! __is_invocable( func_type_l0(int) ) );
> > +
> > +using func_type_ii = int(*)(int);
> > +
> > +SA( ! __is_invocable( func_type_ii ) );
> > +SA(   __is_invocable( func_type_ii, int ) );
> > +
> > +using func_type_il = int(*)(int&);
> > +
> > +SA( ! __is_invocable( func_type_il ) );
> > +SA( ! __is_invocable( func_type_il, int ) );
> > +SA(   __is_invocable( func_type_il, int& ) );
> > +
> > +using func_type_ir = int(*)(int&&);
> > +
> > +SA( ! __is_invocable( func_type_ir ) );
> > +SA( ! __is_invocable( func_type_ir, int& ) );
> > +SA(   __is_invocable( func_type_ir, int ) );
> > +SA(   __is_invocable( func_type_ir, int&& ) );
> > +
> > +struct A { };
> > +
> > +using mem_type_i = int A::*;
> > +
> > +SA( ! __is_invocable( mem_type_i ) );
> > +SA( ! __is_invocable( mem_type_i, int ) );
> > +SA( ! __is_invocable( mem_type_i, int* ) );
> > +SA( ! __is_invocable( mem_type_i, int& ) );
> > +SA( ! __is_invocable( mem_type_i, int&& ) );
> > +SA(   __is_invocable( mem_type_i, A ) );
> > +SA(   __is_invocable( mem_type_i, A* ) );
> > +SA(   __is_invocable( mem_type_i, A& ) );
> > +SA(   __is_invocable( mem_type_i, A&& ) );
> > +SA(   __is_invocable( mem_type_i, const A& ) );
> > +SA( ! __is_invocable( mem_type_i, A&, int ) );
> > +
> > +using memfun_type_i = int (A::*)();
> > +
> > +SA( ! __is_invocable( memfun_type_i ) );
> > +SA( ! __is_invocable( memfun_type_i, int ) );
> > +SA( ! __is_invocable( memfun_type_i, int* ) );
> > +SA( ! __is_invocable( memfun_type_i, int& ) );
> > +SA( ! __is_invocable( memfun_type_i, int&& ) );
> > +SA(   __is_invocable( memfun_type_i, A ) );
> > +SA(   __is_invocable( memfun_type_i, A* ) );
> > +SA(   __is_invocable( memfun_type_i, A& ) );
> > +SA(   __is_invocable( memfun_type_i, A&& ) );
> > +SA( ! __is_invocable( memfun_type_i, const A& ) );
> > +SA( ! __is_invocable( memfun_type_i, A&, int ) );
> > +
> > +using memfun_type_ic = int (A::*)() const;
> > +
> > +SA( ! __is_invocable( memfun_type_ic ) );
> > +SA( ! __is_invocable( memfun_type_ic, int ) );
> > +SA( ! __is_invocable( memfun_type_ic, int& ) );
> > +SA(   __is_invocable( memfun_type_ic, A& ) );
> > +SA(   __is_invocable( memfun_type_ic, A* ) );
> > +SA( ! __is_invocable( memfun_type_ic, A&, int ) );
> > +SA( ! __is_invocable( memfun_type_ic, A*, int& ) );
> > +SA(   __is_invocable( memfun_type_ic, const A& ) );
> > +SA(   __is_invocable( memfun_type_ic, const A* ) );
> > +SA( ! __is_invocable( memfun_type_ic, const A&, int& ) );
> > +SA( ! __is_invocable( memfun_type_ic, const A*, int ) );
> > +
> > +using memfun_type_iic = int& (A::*)(int&) const;
> > +
> > +SA( ! __is_invocable( memfun_type_iic ) );
> > +SA( ! __is_invocable( memfun_type_iic, int ) );
> > +SA( ! __is_invocable( memfun_type_iic, int& ) );
> > +SA( ! __is_invocable( memfun_type_iic, A&, int ) );
> > +SA(   __is_invocable( memfun_type_iic, A&, int& ) );
> > +SA( ! __is_invocable( memfun_type_iic, A*, int ) );
> > +SA(   __is_invocable( memfun_type_iic, A*, int& ) );
> > +SA( ! __is_invocable( memfun_type_iic, const A&, int ) );
> > +SA( ! __is_invocable( memfun_type_iic, const A&, int&, int ) );
> > +SA(   __is_invocable( memfun_type_iic, const A&, int& ) );
> > +SA(   __is_invocable( memfun_type_iic, const A*, int& ) );
> > +
> > +struct B {
> > +  int& operator()();
> > +  long& operator()() const;
> > +  bool& operator()(int);
> > +private:
> > +  void operator()(int, int);
> > +};
> > +using CB = const B;
> > +
> > +SA(   __is_invocable( B ) );
> > +SA(   __is_invocable( B& ) );
> > +SA(   __is_invocable( B&& ) );
> > +SA( ! __is_invocable( B* ) );
> > +SA(   __is_invocable( CB ) );
> > +SA(   __is_invocable( CB& ) );
> > +SA( ! __is_invocable( CB* ) );
> > +
> > +SA(   __is_invocable( B, int ) );
> > +SA(   __is_invocable( B&, int ) );
> > +SA(   __is_invocable( B&&, int ) );
> > +SA( ! __is_invocable( B*, int ) );
> > +SA( ! __is_invocable( CB, int ) );
> > +SA( ! __is_invocable( CB&, int ) );
> > +SA( ! __is_invocable( CB*, int ) );
> > +
> > +SA( ! __is_invocable( B, int, int ) );
> > +SA( ! __is_invocable( B&, int, int ) );
> > +SA( ! __is_invocable( B&&, int, int ) );
> > +SA( ! __is_invocable( B*, int, int ) );
> > +SA( ! __is_invocable( CB, int, int ) );
> > +SA( ! __is_invocable( CB&, int, int ) );
> > +SA( ! __is_invocable( CB*, int, int ) );
> > +
> > +struct C : B { int& operator()() = delete; };
> > +using CC = const C;
> > +
> > +SA( ! __is_invocable( C ) );
> > +SA( ! __is_invocable( C& ) );
> > +SA( ! __is_invocable( C&& ) );
> > +SA( ! __is_invocable( C* ) );
> > +SA( ! __is_invocable( CC ) );
> > +SA( ! __is_invocable( CC& ) );
> > +SA( ! __is_invocable( CC* ) );
> > +
> > +struct D { B operator*(); };
> > +using CD = const D;
> > +
> > +SA( ! __is_invocable( D ) );
> > +
> > +struct E { void v(); };
> > +using CE = const E;
> > +
> > +SA( ! __is_invocable( E ) );
> > +SA( ! __is_invocable( void (E::*)() ) );
> > +SA(   __is_invocable( void (E::*)(), E ) );
> > +SA(   __is_invocable( void (E::*)(), E* ) );
> > +SA( ! __is_invocable( void (E::*)(), CE ) );
> > +
> > +struct F : E {};
> > +using CF = const F;
> > +
> > +SA( ! __is_invocable( F ) );
> > +SA(   __is_invocable( void (E::*)(), F ) );
> > +SA(   __is_invocable( void (E::*)(), F* ) );
> > +SA( ! __is_invocable( void (E::*)(), CF ) );
> > +
> > +struct G { E operator*(); };
> > +using CG = const G;
> > +
> > +SA( ! __is_invocable( G ) );
> > +SA(   __is_invocable( void (E::*)(), G ) );
> > +SA( ! __is_invocable( void (E::*)(), G* ) );
> > +SA( ! __is_invocable( void (E::*)(), CG ) );
> > +
> > +struct H { E& operator*(); };
> > +using CH = const H;
> > +
> > +SA( ! __is_invocable( H ) );
> > +SA(   __is_invocable( void (E::*)(), H ) );
> > +SA( ! __is_invocable( void (E::*)(), H* ) );
> > +SA( ! __is_invocable( void (E::*)(), CH ) );
> > +
> > +struct I { E&& operator*(); };
> > +using CI = const I;
> > +
> > +SA( ! __is_invocable( I ) );
> > +SA(   __is_invocable( void (E::*)(), I ) );
> > +SA( ! __is_invocable( void (E::*)(), I* ) );
> > +SA( ! __is_invocable( void (E::*)(), CI ) );
> > +
> > +struct K { E* operator*(); };
> > +using CK = const K;
> > +
> > +SA( ! __is_invocable( K ) );
> > +SA( ! __is_invocable( void (E::*)(), K ) );
> > +SA( ! __is_invocable( void (E::*)(), K* ) );
> > +SA( ! __is_invocable( void (E::*)(), CK ) );
> > +
> > +struct L { CE operator*(); };
> > +using CL = const L;
> > +
> > +SA( ! __is_invocable( L ) );
> > +SA( ! __is_invocable( void (E::*)(), L ) );
> > +SA( ! __is_invocable( void (E::*)(), L* ) );
> > +SA( ! __is_invocable( void (E::*)(), CL ) );
> > +
> > +struct M {
> > +  int i;
> > +private:
> > +  long l;
> > +};
> > +using CM = const M;
> > +
> > +SA( ! __is_invocable( M ) );
> > +SA( ! __is_invocable( M& ) );
> > +SA( ! __is_invocable( M&& ) );
> > +SA( ! __is_invocable( M* ) );
> > +SA( ! __is_invocable( CM ) );
> > +SA( ! __is_invocable( CM& ) );
> > +SA( ! __is_invocable( CM* ) );
> > +
> > +SA( ! __is_invocable( int M::* ) );
> > +SA(   __is_invocable( int M::*, M ) );
> > +SA(   __is_invocable( int M::*, M& ) );
> > +SA(   __is_invocable( int M::*, M&& ) );
> > +SA(   __is_invocable( int M::*, M* ) );
> > +SA(   __is_invocable( int M::*, CM ) );
> > +SA(   __is_invocable( int M::*, CM& ) );
> > +SA(   __is_invocable( int M::*, CM* ) );
> > +SA( ! __is_invocable( int M::*, int ) );
> > +
> > +SA( ! __is_invocable( int CM::* ) );
> > +SA(   __is_invocable( int CM::*, M ) );
> > +SA(   __is_invocable( int CM::*, M& ) );
> > +SA(   __is_invocable( int CM::*, M&& ) );
> > +SA(   __is_invocable( int CM::*, M* ) );
> > +SA(   __is_invocable( int CM::*, CM ) );
> > +SA(   __is_invocable( int CM::*, CM& ) );
> > +SA(   __is_invocable( int CM::*, CM* ) );
> > +SA( ! __is_invocable( int CM::*, int ) );
> > +
> > +SA( ! __is_invocable( long M::* ) );
> > +SA(   __is_invocable( long M::*, M ) );
> > +SA(   __is_invocable( long M::*, M& ) );
> > +SA(   __is_invocable( long M::*, M&& ) );
> > +SA(   __is_invocable( long M::*, M* ) );
> > +SA(   __is_invocable( long M::*, CM ) );
> > +SA(   __is_invocable( long M::*, CM& ) );
> > +SA(   __is_invocable( long M::*, CM* ) );
> > +SA( ! __is_invocable( long M::*, long ) );
> > +
> > +SA( ! __is_invocable( long CM::* ) );
> > +SA(   __is_invocable( long CM::*, M ) );
> > +SA(   __is_invocable( long CM::*, M& ) );
> > +SA(   __is_invocable( long CM::*, M&& ) );
> > +SA(   __is_invocable( long CM::*, M* ) );
> > +SA(   __is_invocable( long CM::*, CM ) );
> > +SA(   __is_invocable( long CM::*, CM& ) );
> > +SA(   __is_invocable( long CM::*, CM* ) );
> > +SA( ! __is_invocable( long CM::*, long ) );
> > +
> > +SA( ! __is_invocable( short M::* ) );
> > +SA(   __is_invocable( short M::*, M ) );
> > +SA(   __is_invocable( short M::*, M& ) );
> > +SA(   __is_invocable( short M::*, M&& ) );
> > +SA(   __is_invocable( short M::*, M* ) );
> > +SA(   __is_invocable( short M::*, CM ) );
> > +SA(   __is_invocable( short M::*, CM& ) );
> > +SA(   __is_invocable( short M::*, CM* ) );
> > +SA( ! __is_invocable( short M::*, short ) );
> > +
> > +SA( ! __is_invocable( short CM::* ) );
> > +SA(   __is_invocable( short CM::*, M ) );
> > +SA(   __is_invocable( short CM::*, M& ) );
> > +SA(   __is_invocable( short CM::*, M&& ) );
> > +SA(   __is_invocable( short CM::*, M* ) );
> > +SA(   __is_invocable( short CM::*, CM ) );
> > +SA(   __is_invocable( short CM::*, CM& ) );
> > +SA(   __is_invocable( short CM::*, CM* ) );
> > +SA( ! __is_invocable( short CM::*, short ) );
> > +
> > +struct N { M operator*(); };
> > +SA(   __is_invocable( int M::*, N ) );
> > +SA( ! __is_invocable( int M::*, N* ) );
> > +
> > +struct O { M& operator*(); };
> > +SA(   __is_invocable( int M::*, O ) );
> > +SA( ! __is_invocable( int M::*, O* ) );
> > +
> > +struct P { M&& operator*(); };
> > +SA(   __is_invocable( int M::*, P ) );
> > +SA( ! __is_invocable( int M::*, P* ) );
> > +
> > +struct Q { M* operator*(); };
> > +SA( ! __is_invocable( int M::*, Q ) );
> > +SA( ! __is_invocable( int M::*, Q* ) );
> > +
> > +struct R { void operator()(int = 0); };
> > +
> > +SA(   __is_invocable( R ) );
> > +SA(   __is_invocable( R, int ) );
> > +SA( ! __is_invocable( R, int, int ) );
> > +
> > +struct S { void operator()(int, ...); };
> > +
> > +SA( ! __is_invocable( S ) );
> > +SA(   __is_invocable( S, int ) );
> > +SA(   __is_invocable( S, int, int ) );
> > +SA(   __is_invocable( S, int, int, int ) );
> > +
> > +void fn1() {}
> > +
> > +SA(   __is_invocable( decltype(fn1) ) );
> > +
> > +void fn2(int arr[10]);
> > +
> > +SA(   __is_invocable( decltype(fn2), int[10] ) );
> > +SA(   __is_invocable( decltype(fn2), int(&)[10] ) );
> > +SA(   __is_invocable( decltype(fn2), int(&&)[10] ) );
> > +SA( ! __is_invocable( decltype(fn2), int(*)[10] ) );
> > +SA( ! __is_invocable( decltype(fn2), int(*&)[10] ) );
> > +SA( ! __is_invocable( decltype(fn2), int(*&&)[10] ) );
> > +SA(   __is_invocable( decltype(fn2), int[] ) );
> > +
> > +auto lambda = []() {};
> > +
> > +SA(   __is_invocable( decltype(lambda) ) );
> > +
> > +template <typename Func, typename... Args>
> > +struct can_invoke {
> > +    static constexpr bool value = __is_invocable( Func, Args... );
> > +};
> > +
> > +SA( can_invoke<decltype(lambda)>::value );
> > +
> > +struct T {
> > +  void func() const {}
> > +  int data;
> > +};
> > +
> > +SA(   __is_invocable( decltype(&T::func)&, T& ) );
> > +SA(   __is_invocable( decltype(&T::data)&, T& ) );
> > diff --git a/gcc/testsuite/g++.dg/ext/is_invocable2.C b/gcc/testsuite/g++.dg/ext/is_invocable2.C
> > new file mode 100644
> > index 00000000000..a68aefd3e13
> > --- /dev/null
> > +++ b/gcc/testsuite/g++.dg/ext/is_invocable2.C
> > @@ -0,0 +1,139 @@
> > +// { dg-do compile { target c++11 } }
> > +// __is_invocable should handle std::reference_wrapper correctly.
> > +
> > +#include <functional>
> > +
> > +#define SA(X) static_assert((X),#X)
> > +
> > +using std::reference_wrapper;
> > +
> > +using func_type_v0 = void(*)();
> > +
> > +SA(   __is_invocable( reference_wrapper<func_type_v0> ) );
> > +SA( ! __is_invocable( reference_wrapper<func_type_v0>, int ) );
> > +
> > +using func_type_i0 = int(*)();
> > +
> > +SA(   __is_invocable( reference_wrapper<func_type_i0> ) );
> > +SA( ! __is_invocable( reference_wrapper<func_type_i0>, int ) );
> > +
> > +using func_type_l0 = int&(*)();
> > +
> > +SA(   __is_invocable( reference_wrapper<func_type_l0> ) );
> > +SA( ! __is_invocable( reference_wrapper<func_type_l0(int)> ) );
> > +
> > +using func_type_ii = int(*)(int);
> > +
> > +SA( ! __is_invocable( reference_wrapper<func_type_ii> ) );
> > +SA(   __is_invocable( reference_wrapper<func_type_ii>, int ) );
> > +
> > +using func_type_il = int(*)(int&);
> > +
> > +SA( ! __is_invocable( reference_wrapper<func_type_il> ) );
> > +SA( ! __is_invocable( reference_wrapper<func_type_il>, int ) );
> > +SA(   __is_invocable( reference_wrapper<func_type_il>, int& ) );
> > +
> > +using func_type_ir = int(*)(int&&);
> > +
> > +SA( ! __is_invocable( reference_wrapper<func_type_ir> ) );
> > +SA( ! __is_invocable( reference_wrapper<func_type_ir>, int& ) );
> > +SA(   __is_invocable( reference_wrapper<func_type_ir>, int ) );
> > +SA(   __is_invocable( reference_wrapper<func_type_ir>, int&& ) );
> > +
> > +struct A { };
> > +
> > +using mem_type_i = int A::*;
> > +
> > +SA( ! __is_invocable( reference_wrapper<mem_type_i> ) );
> > +SA( ! __is_invocable( reference_wrapper<mem_type_i>, int ) );
> > +SA( ! __is_invocable( reference_wrapper<mem_type_i>, int* ) );
> > +SA( ! __is_invocable( reference_wrapper<mem_type_i>, int& ) );
> > +SA( ! __is_invocable( reference_wrapper<mem_type_i>, int&& ) );
> > +SA(   __is_invocable( reference_wrapper<mem_type_i>, A ) );
> > +SA(   __is_invocable( reference_wrapper<mem_type_i>, A* ) );
> > +SA(   __is_invocable( reference_wrapper<mem_type_i>, A& ) );
> > +SA(   __is_invocable( reference_wrapper<mem_type_i>, A&& ) );
> > +
> > +using memfun_type_i = int (A::*)();
> > +
> > +SA( ! __is_invocable( reference_wrapper<memfun_type_i> ) );
> > +SA( ! __is_invocable( reference_wrapper<memfun_type_i>, int ) );
> > +SA( ! __is_invocable( reference_wrapper<memfun_type_i>, int* ) );
> > +SA( ! __is_invocable( reference_wrapper<memfun_type_i>, int& ) );
> > +SA( ! __is_invocable( reference_wrapper<memfun_type_i>, int&& ) );
> > +SA(   __is_invocable( reference_wrapper<memfun_type_i>, A ) );
> > +SA(   __is_invocable( reference_wrapper<memfun_type_i>, A* ) );
> > +SA(   __is_invocable( reference_wrapper<memfun_type_i>, A& ) );
> > +SA(   __is_invocable( reference_wrapper<memfun_type_i>, A&& ) );
> > +SA( ! __is_invocable( reference_wrapper<memfun_type_i>, const A& ) );
> > +SA( ! __is_invocable( reference_wrapper<memfun_type_i>, A&, int ) );
> > +
> > +using memfun_type_ic = int (A::*)() const;
> > +
> > +SA( ! __is_invocable( reference_wrapper<memfun_type_ic> ) );
> > +SA( ! __is_invocable( reference_wrapper<memfun_type_ic>, int ) );
> > +SA( ! __is_invocable( reference_wrapper<memfun_type_ic>, int& ) );
> > +SA(   __is_invocable( reference_wrapper<memfun_type_ic>, A& ) );
> > +SA(   __is_invocable( reference_wrapper<memfun_type_ic>, A* ) );
> > +SA( ! __is_invocable( reference_wrapper<memfun_type_ic>, A&, int ) );
> > +SA( ! __is_invocable( reference_wrapper<memfun_type_ic>, A*, int& ) );
> > +SA(   __is_invocable( reference_wrapper<memfun_type_ic>, const A& ) );
> > +SA(   __is_invocable( reference_wrapper<memfun_type_ic>, const A* ) );
> > +SA( ! __is_invocable( reference_wrapper<memfun_type_ic>, const A&, int& ) );
> > +SA( ! __is_invocable( reference_wrapper<memfun_type_ic>, const A*, int ) );
> > +
> > +using memfun_type_iic = int& (A::*)(int&) const;
> > +
> > +SA( ! __is_invocable( reference_wrapper<memfun_type_iic> ) );
> > +SA( ! __is_invocable( reference_wrapper<memfun_type_iic>, int ) );
> > +SA( ! __is_invocable( reference_wrapper<memfun_type_iic>, int& ) );
> > +SA( ! __is_invocable( reference_wrapper<memfun_type_iic>, A&, int ) );
> > +SA(   __is_invocable( reference_wrapper<memfun_type_iic>, A&, int& ) );
> > +SA( ! __is_invocable( reference_wrapper<memfun_type_iic>, A*, int ) );
> > +SA(   __is_invocable( reference_wrapper<memfun_type_iic>, A*, int& ) );
> > +SA( ! __is_invocable( reference_wrapper<memfun_type_iic>, const A&, int ) );
> > +SA( ! __is_invocable( reference_wrapper<memfun_type_iic>, const A&, int&, int ) );
> > +SA(   __is_invocable( reference_wrapper<memfun_type_iic>, const A&, int& ) );
> > +SA(   __is_invocable( reference_wrapper<memfun_type_iic>, const A*, int& ) );
> > +
> > +struct B {
> > +  int& operator()();
> > +  long& operator()() const;
> > +  bool& operator()(int);
> > +private:
> > +  void operator()(int, int);
> > +};
> > +using CB = const B;
> > +
> > +SA(   __is_invocable( reference_wrapper<B> ) );
> > +SA(   __is_invocable( reference_wrapper<B>& ) );
> > +SA(   __is_invocable( reference_wrapper<B>&& ) );
> > +SA(   __is_invocable( reference_wrapper<CB> ) );
> > +SA(   __is_invocable( reference_wrapper<CB>& ) );
> > +SA(   __is_invocable( reference_wrapper<B>, int ) );
> > +SA( ! __is_invocable( reference_wrapper<B>&, int, int ) );
> > +
> > +struct C : B { int& operator()() = delete; };
> > +using CC = const C;
> > +
> > +SA( ! __is_invocable( reference_wrapper<C> ) );
> > +SA( ! __is_invocable( reference_wrapper<C>& ) );
> > +SA( ! __is_invocable( reference_wrapper<C>&& ) );
> > +SA( ! __is_invocable( reference_wrapper<CC> ) );
> > +SA( ! __is_invocable( reference_wrapper<CC>& ) );
> > +
> > +struct D { B operator*(); };
> > +using CD = const D;
> > +
> > +SA( ! __is_invocable( reference_wrapper<D> ) );
> > +SA( ! __is_invocable( reference_wrapper<D>& ) );
> > +SA( ! __is_invocable( reference_wrapper<D>&& ) );
> > +SA( ! __is_invocable( reference_wrapper<D>* ) );
> > +SA( ! __is_invocable( reference_wrapper<D*> ) );
> > +SA( ! __is_invocable( reference_wrapper<D*>* ) );
> > +
> > +std::function<void()> fn = []() {};
> > +auto refwrap = std::ref(fn);
> > +
> > +SA(   __is_invocable( decltype(fn) ) );
> > +SA(   __is_invocable( decltype(refwrap) ) );
> > diff --git a/gcc/testsuite/g++.dg/ext/is_invocable3.C b/gcc/testsuite/g++.dg/ext/is_invocable3.C
> > new file mode 100644
> > index 00000000000..e2b0c5ef406
> > --- /dev/null
> > +++ b/gcc/testsuite/g++.dg/ext/is_invocable3.C
> > @@ -0,0 +1,51 @@
> > +// { dg-do compile { target c++11 } }
> > +// __is_invocable should handle incomplete class correctly.
> > +
> > +#define SA(X) static_assert((X),#X)
> > +
> > +struct Incomplete;
> > +
> > +SA( ! __is_invocable( Incomplete ) ); // { dg-error "incomplete type" }
> > +SA( ! __is_invocable( Incomplete, int ) ); // { dg-error "incomplete type" }
> > +
> > +SA( ! __is_invocable( int, Incomplete, int ) ); // { dg-error "incomplete type" }
> > +SA( ! __is_invocable( int, Incomplete ) ); // { dg-error "incomplete type" }
> > +
> > +SA( ! __is_invocable( Incomplete, Incomplete() ) ); // { dg-error "incomplete type" }
> > +SA( ! __is_invocable( Incomplete, Incomplete(int), int ) ); // { dg-error "incomplete type" }
> > +SA( ! __is_invocable( Incomplete, Incomplete(int, int), int, int ) ); // { dg-error "incomplete type" }
> > +
> > +SA( ! __is_invocable( Incomplete, Incomplete(), int, int ) ); // { dg-error "incomplete type" }
> > +
> > +SA( ! __is_invocable( int(Incomplete), Incomplete ) ); // { dg-error "incomplete type" }
> > +SA( ! __is_invocable( int(int, Incomplete), int, Incomplete ) ); // { dg-error "incomplete type" }
> > +SA( ! __is_invocable( int(int, Incomplete), Incomplete, int ) ); // { dg-error "incomplete type" }
> > +
> > +SA(   __is_invocable( int(Incomplete&), Incomplete& ) ); // { dg-bogus "incomplete type" }
> > +SA(   __is_invocable( int(int, Incomplete&), int, Incomplete& ) ); // { dg-bogus "incomplete type" }
> > +
> > +SA(   __is_invocable( int(Incomplete&&), Incomplete&& ) ); // { dg-bogus "incomplete type" }
> > +SA(   __is_invocable( int(int, Incomplete&&), int, Incomplete&& ) ); // { dg-bogus "incomplete type" }
> > +
> > +SA(   __is_invocable( int(const Incomplete&&), const Incomplete&& ) ); // { dg-bogus "incomplete type" }
> > +SA(   __is_invocable( int(int, const Incomplete&&), int, const Incomplete&& ) ); // { dg-bogus "incomplete type" }
> > +
> > +SA(   __is_invocable( int(const Incomplete&), const Incomplete& ) ); // { dg-bogus "incomplete type" }
> > +SA(   __is_invocable( int(int, const Incomplete&), int, const Incomplete& ) ); // { dg-bogus "incomplete type" }
> > +
> > +SA(   __is_invocable( int(const Incomplete&), Incomplete& ) ); // { dg-bogus "incomplete type" }
> > +SA(   __is_invocable( int(int, const Incomplete&), int, Incomplete& ) ); // { dg-bogus "incomplete type" }
> > +
> > +SA(   __is_invocable( int Incomplete::*, const Incomplete& ) ); // { dg-bogus "incomplete type" }
> > +SA( ! __is_invocable( void (Incomplete::*)(long&), const Incomplete*, long& ) ); // { dg-bogus "incomplete type" }
> > +SA(   __is_invocable( void (Incomplete::*)(long&) const, Incomplete*, long& ) ); // { dg-bogus "incomplete type" }
> > +
> > +template <typename T>
> > +struct Holder { T t; };
> > +
> > +SA(   __is_invocable( int(Holder<Incomplete>&), Holder<Incomplete>& ) ); // { dg-bogus "incomplete type" }
> > +
> > +// Define Incomplete, which is now not incomplete.
> > +struct Incomplete { void operator()(); };
> > +
> > +SA( __is_invocable( Incomplete ) ); // { dg-bogus "incomplete type" }
> > diff --git a/gcc/testsuite/g++.dg/ext/is_invocable4.C b/gcc/testsuite/g++.dg/ext/is_invocable4.C
> > new file mode 100644
> > index 00000000000..d1efccf08f8
> > --- /dev/null
> > +++ b/gcc/testsuite/g++.dg/ext/is_invocable4.C
> > @@ -0,0 +1,33 @@
> > +// { dg-do compile { target c++11 } }
> > +// Failed access check should be a substitution failure, not an error.
> > +
> > +#define SA(X) static_assert((X),#X)
> > +
> > +template<bool B>
> > +struct bool_constant { static constexpr bool value = B; };
> > +
> > +template<typename _Fn, typename... _ArgTypes>
> > +struct is_invocable
> > +: public bool_constant<__is_invocable(_Fn, _ArgTypes...)>
> > +{ };
> > +
> > +#if __cpp_variable_templates
> > +template<typename _Fn, typename... _ArgTypes>
> > +constexpr bool is_invocable_v = __is_invocable(_Fn, _ArgTypes...);
> > +#endif
> > +
> > +class Private
> > +{
> > +  void operator()() const
> > +  {
> > +    SA( ! is_invocable<Private>::value );
> > +#if __cpp_variable_templates
> > +    SA( ! is_invocable_v<Private> );
> > +#endif
> > +  }
> > +};
> > +
> > +SA( ! is_invocable<Private>::value );
> > +#if __cpp_variable_templates
> > +SA( ! is_invocable_v<Private> );
> > +#endif
> > -- 
> > 2.42.0
> > 
> > 
>
  
Patrick Palka Oct. 20, 2023, 9:37 p.m. UTC | #3
On Fri, 20 Oct 2023, Patrick Palka wrote:

> On Fri, 20 Oct 2023, Patrick Palka wrote:
> 
> > On Fri, 20 Oct 2023, Ken Matsui wrote:
> > 
> > > This patch implements built-in trait for std::is_invocable.
> > 
> > Nice!  My email client unfortunately ate my first review attempt, so
> > apologies for my brevity this time around.
> > 
> > > gcc/cp/ChangeLog:
> > > 
> > > 	* cp-trait.def: Define __is_invocable.
> > > 	* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_INVOCABLE.
> > > 	* semantics.cc (trait_expr_value): Likewise.
> > > 	(finish_trait_expr): Likewise.
> > > 	(is_invocable_p): New function.
> > > 	* method.h: New file to export build_trait_object in method.cc.
> > > 
> > > gcc/testsuite/ChangeLog:
> > > 
> > > 	* g++.dg/ext/has-builtin-1.C: Test existence of __is_invocable.
> > > 	* g++.dg/ext/is_invocable1.C: New test.
> > > 	* g++.dg/ext/is_invocable2.C: New test.
> > > 	* g++.dg/ext/is_invocable3.C: New test.
> > > 	* g++.dg/ext/is_invocable4.C: New test.
> > > 
> > > Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
> > > ---
> > >  gcc/cp/constraint.cc                     |   6 +
> > >  gcc/cp/cp-trait.def                      |   1 +
> > >  gcc/cp/method.h                          |  28 ++
> > >  gcc/cp/semantics.cc                      | 135 +++++++++
> > >  gcc/testsuite/g++.dg/ext/has-builtin-1.C |   3 +
> > >  gcc/testsuite/g++.dg/ext/is_invocable1.C | 337 +++++++++++++++++++++++
> > >  gcc/testsuite/g++.dg/ext/is_invocable2.C | 139 ++++++++++
> > >  gcc/testsuite/g++.dg/ext/is_invocable3.C |  51 ++++
> > >  gcc/testsuite/g++.dg/ext/is_invocable4.C |  33 +++
> > >  9 files changed, 733 insertions(+)
> > >  create mode 100644 gcc/cp/method.h
> > >  create mode 100644 gcc/testsuite/g++.dg/ext/is_invocable1.C
> > >  create mode 100644 gcc/testsuite/g++.dg/ext/is_invocable2.C
> > >  create mode 100644 gcc/testsuite/g++.dg/ext/is_invocable3.C
> > >  create mode 100644 gcc/testsuite/g++.dg/ext/is_invocable4.C
> > > 
> > > diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
> > > index 9fce36e12d1..29bf548d30a 100644
> > > --- a/gcc/cp/constraint.cc
> > > +++ b/gcc/cp/constraint.cc
> > > @@ -3754,6 +3754,12 @@ diagnose_trait_expr (tree expr, tree args)
> > >      case CPTK_IS_FUNCTION:
> > >        inform (loc, "  %qT is not a function", t1);
> > >        break;
> > > +    case CPTK_IS_INVOCABLE:
> > > +      if (!t2)
> > > +    inform (loc, "  %qT is not invocable", t1);
> > > +      else
> > > +    inform (loc, "  %qT is not invocable by %qE", t1, t2);
> > > +      break;
> > >      case CPTK_IS_LAYOUT_COMPATIBLE:
> > >        inform (loc, "  %qT is not layout compatible with %qT", t1, t2);
> > >        break;
> > > diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
> > > index 05514a51c21..b8b7608c122 100644
> > > --- a/gcc/cp/cp-trait.def
> > > +++ b/gcc/cp/cp-trait.def
> > > @@ -71,6 +71,7 @@ DEFTRAIT_EXPR (IS_EMPTY, "__is_empty", 1)
> > >  DEFTRAIT_EXPR (IS_ENUM, "__is_enum", 1)
> > >  DEFTRAIT_EXPR (IS_FINAL, "__is_final", 1)
> > >  DEFTRAIT_EXPR (IS_FUNCTION, "__is_function", 1)
> > > +DEFTRAIT_EXPR (IS_INVOCABLE, "__is_invocable", -1)
> > >  DEFTRAIT_EXPR (IS_LAYOUT_COMPATIBLE, "__is_layout_compatible", 2)
> > >  DEFTRAIT_EXPR (IS_LITERAL_TYPE, "__is_literal_type", 1)
> > >  DEFTRAIT_EXPR (IS_MEMBER_FUNCTION_POINTER, "__is_member_function_pointer", 1)
> > > diff --git a/gcc/cp/method.h b/gcc/cp/method.h
> > > new file mode 100644
> > > index 00000000000..1aec8ec5cfd
> > > --- /dev/null
> > > +++ b/gcc/cp/method.h
> > > @@ -0,0 +1,28 @@
> > > +/* Functions exported by method.cc.
> > > +   Copyright (C) 2023 Free Software Foundation, Inc.
> > > +
> > > +This file is part of GCC.
> > > +
> > > +GCC is free software; you can redistribute it and/or modify
> > > +it under the terms of the GNU General Public License as published by
> > > +the Free Software Foundation; either version 3, or (at your option)
> > > +any later version.
> > > +
> > > +GCC is distributed in the hope that it will be useful,
> > > +but WITHOUT ANY WARRANTY; without even the implied warranty of
> > > +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > > +GNU General Public License for more details.
> > > +
> > > +You should have received a copy of the GNU General Public License
> > > +along with GCC; see the file COPYING3.  If not see
> > > +<http://www.gnu.org/licenses/>.  */
> > > +
> > > +#ifndef GCC_CP_METHOD_H
> > > +#define GCC_CP_METHOD_H 1
> > > +
> > > +#include "tree.h"
> > > +
> > > +/* In method.cc  */
> > > +extern tree build_trait_object (tree type);
> > 
> > Since other method.cc exports are already declared in cp-tree.h, for now
> > let's just declare this in cp-tree.h as well (under build_stub_object)
> > instead of creating a new header file.
> > 
> > > +
> > > +#endif  /* GCC_CP_METHOD_H  */
> > > diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
> > > index 7cccbae5287..cc2e400531a 100644
> > > --- a/gcc/cp/semantics.cc
> > > +++ b/gcc/cp/semantics.cc
> > > @@ -45,6 +45,10 @@ along with GCC; see the file COPYING3.  If not see
> > >  #include "gomp-constants.h"
> > >  #include "predict.h"
> > >  #include "memmodel.h"
> > > +#include "method.h"
> > > +
> > > +#include "print-tree.h"
> > > +#include "tree-pretty-print.h"
> > >  
> > >  /* There routines provide a modular interface to perform many parsing
> > >     operations.  They may therefore be used during actual parsing, or
> > > @@ -11714,6 +11718,133 @@ classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
> > >    return saw_copy;
> > >  }
> > >  
> > > +/* Return true if FN_TYPE is invocable with the given ARG_TYPES.  */
> > > +
> > > +static bool
> > > +is_invocable_p (tree fn_type, tree arg_types)

(Sorry for the spam)  We'll eventually want to implement a built-in for
invoke_result, so perhaps we should preemptively factor out the bulk
of this function into a 'build_INVOKE' helper function that returns the
built tree?

> > > +{
> > > +  /* ARG_TYPES must be a TREE_VEC.  */
> > > +  gcc_assert (TREE_CODE (arg_types) == TREE_VEC);
> > > +
> > > +  /* Access check is required to determine if the given is invocable.  */
> > > +  deferring_access_check_sentinel acs (dk_no_deferred);
> > > +
> > > +  /* std::is_invocable is an unevaluated context.  */
> > > +  cp_unevaluated cp_uneval_guard;
> > > +
> > > +  bool is_ptrdatamem;
> > > +  bool is_ptrmemfunc;
> > > +  if (TREE_CODE (fn_type) == REFERENCE_TYPE)
> > > +    {
> > > +      tree deref_fn_type = TREE_TYPE (fn_type);
> > > +      is_ptrdatamem = TYPE_PTRDATAMEM_P (deref_fn_type);
> > > +      is_ptrmemfunc = TYPE_PTRMEMFUNC_P (deref_fn_type);
> > > +
> > > +      /* Dereference fn_type if it is a pointer to member.  */
> > > +      if (is_ptrdatamem || is_ptrmemfunc)
> > > +	fn_type = deref_fn_type;
> > > +    }
> > > +  else
> > > +    {
> > > +      is_ptrdatamem = TYPE_PTRDATAMEM_P (fn_type);
> > > +      is_ptrmemfunc = TYPE_PTRMEMFUNC_P (fn_type);
> > > +    }
> > > +
> > > +  if (is_ptrdatamem && TREE_VEC_LENGTH (arg_types) != 1)
> > > +    /* A pointer to data member with non-one argument is not invocable.  */
> > > +    return false;
> > > +
> > > +  if (is_ptrmemfunc && TREE_VEC_LENGTH (arg_types) == 0)
> > > +    /* A pointer to member function with no arguments is not invocable.  */
> > > +    return false;
> > > +
> > > +  /* Construct an expression of a pointer to member.  */
> > > +  tree datum;
> > > +  if (is_ptrdatamem || is_ptrmemfunc)
> > > +    {
> > > +      tree datum_type = TREE_VEC_ELT (arg_types, 0);
> > > +
> > > +      /* Dereference datum.  */
> > > +      if (CLASS_TYPE_P (datum_type))
> > > +	{
> > > +	  bool is_refwrap = false;
> > > +
> > > +	  tree datum_decl = TYPE_NAME (TYPE_MAIN_VARIANT (datum_type));
> > > +	  if (decl_in_std_namespace_p (datum_decl))
> > > +	    {
> > > +	      tree name = DECL_NAME (datum_decl);
> > > +	      if (name && (id_equal (name, "reference_wrapper")))
> > > +		{
> > > +		  /* Handle std::reference_wrapper.  */
> > > +		  is_refwrap = true;
> > > +		  datum_type = cp_build_reference_type (datum_type, false);
> > > +		}
> > > +	    }
> > > +
> > > +	  datum = build_trait_object (datum_type);
> > > +
> > > +	  /* If datum_type was not std::reference_wrapper, check if it has
> > > +	     operator*() overload.  If datum_type was std::reference_wrapper,
> > > +	     avoid dereferencing the datum twice.  */
> > > +	  if (!is_refwrap)
> > > +	    if (get_class_binding (datum_type, get_identifier ("operator*")))
> > 
> > We probably should use lookup_member instead of get_class_binding since
> > IIUC the latter doesn't look into bases:
> > 
> >   struct A { int m; };
> >   struct B { A& operator*(): };
> >   struct C : B { };
> >   static_assert(std::is_invocable_v<int A::*, C>);
> > 
> > However, I notice that the specification of INVOKE
> > (https://eel.is/c++draft/func.require#lib:INVOKE) doesn't mention name
> > lookup at all so it strikes me as suspicious that we'd perform name
> > lookup here.  I think this would misbehave for:
> > 
> >   struct A { };
> >   struct B : A { A& operator*() = delete; };
> >   static_assert(std::is_invocable_v<int A::*, B>);
> > 
> >   struct C : private A { A& operator*(); };
> >   static_assert(std::is_invocable_v<int A::*, C>);
> 
> Oops, this static_assert is missing a !
> 
> > 
> > ultimately because we end up choosing the dereference form of INVOKE,
> > but according to 1.1/1.4 we should choose the non-dereference form?
> > 
> > > +	      /* Handle operator*().  */
> > > +	      datum = build_x_indirect_ref (UNKNOWN_LOCATION, datum,
> > > +					    RO_UNARY_STAR, NULL_TREE,
> > > +					    tf_none);
> > > +	}
> > > +      else if (POINTER_TYPE_P (datum_type))
> > > +	datum = build_trait_object (TREE_TYPE (datum_type));
> > > +      else
> > > +	datum = build_trait_object (datum_type);
> > > +    }
> > > +
> > > +  /* Build a function expression.  */
> > > +  tree fn;
> > > +  if (is_ptrdatamem)
> > > +    fn = build_m_component_ref (datum, build_trait_object (fn_type), tf_none);
> > 
> > Maybe exit early for the is_ptrdatamem case here (and simplify the rest
> > of the function accordingly)?
> > 
> > > +  else if (is_ptrmemfunc)
> > > +    fn = build_trait_object (TYPE_PTRMEMFUNC_FN_TYPE (fn_type));
> > > +  else
> > > +    fn = build_trait_object (fn_type);
> > > +
> > > +  /* Construct arguments to the function and an expression of a call.  */
> > > +  if (!is_ptrdatamem)
> > > +    {
> > > +      releasing_vec args;
> > > +
> > > +      if (is_ptrmemfunc)
> > > +	{
> > > +	  /* A pointer to member function is internally converted to a pointer
> > > +	     to function that takes a pointer to the dereferenced datum type
> > > +	     as its first argument and original arguments afterward.  If the
> > > +	     function is a const member function, the first argument also
> > > +	     requires a const datum pointer and vice-versa.  */
> > > +
> > > +	  tree datum_type = TREE_TYPE (datum);
> > > +	  if (TYPE_REF_P (datum_type))
> > > +	    datum_type = TREE_TYPE (datum_type);
> > > +
> > > +	  datum = build_trait_object (build_pointer_type (datum_type));
> > > +	  vec_safe_push (args, datum);
> > > +	}
> > > +
> > > +      for (int i = is_ptrmemfunc ? 1 : 0; i < TREE_VEC_LENGTH (arg_types); ++i)
> > > +	{
> > > +	  tree arg_type = TREE_VEC_ELT (arg_types, i);
> > > +	  tree arg = build_trait_object (arg_type);
> > > +	  vec_safe_push (args, arg);
> > > +	}
> > > +
> > > +      fn = finish_call_expr (fn, &args, false, false, tf_none);
> > > +    }
> > > +
> > > +  if (error_operand_p (fn))
> > > +    return false;
> > > +
> > > +  return true;
> > > +}
> > > +
> > >  /* Return true if DERIVED is pointer interconvertible base of BASE.  */
> > >  
> > >  static bool
> > > @@ -12181,6 +12312,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
> > >      case CPTK_IS_FUNCTION:
> > >        return type_code1 == FUNCTION_TYPE;
> > >  
> > > +    case CPTK_IS_INVOCABLE:
> > > +      return is_invocable_p (type1, type2);
> > > +
> > >      case CPTK_IS_LAYOUT_COMPATIBLE:
> > >        return layout_compatible_type_p (type1, type2);
> > >  
> > > @@ -12390,6 +12524,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
> > >        break;
> > >  
> > >      case CPTK_IS_CONVERTIBLE:
> > > +    case CPTK_IS_INVOCABLE:
> > >      case CPTK_IS_NOTHROW_ASSIGNABLE:
> > >      case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
> > >      case CPTK_IS_NOTHROW_CONVERTIBLE:
> > > diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> > > index b1430e9bd8b..3a9bda1ee03 100644
> > > --- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> > > +++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
> > > @@ -92,6 +92,9 @@
> > >  #if !__has_builtin (__is_function)
> > >  # error "__has_builtin (__is_function) failed"
> > >  #endif
> > > +#if !__has_builtin (__is_invocable)
> > > +# error "__has_builtin (__is_invocable) failed"
> > > +#endif
> > >  #if !__has_builtin (__is_layout_compatible)
> > >  # error "__has_builtin (__is_layout_compatible) failed"
> > >  #endif
> > > diff --git a/gcc/testsuite/g++.dg/ext/is_invocable1.C b/gcc/testsuite/g++.dg/ext/is_invocable1.C
> > > new file mode 100644
> > > index 00000000000..2fd3906b571
> > > --- /dev/null
> > > +++ b/gcc/testsuite/g++.dg/ext/is_invocable1.C
> > > @@ -0,0 +1,337 @@
> > > +// { dg-do compile { target c++11 } }
> > > +
> > > +#define SA(X) static_assert((X),#X)
> > > +
> > > +using func_type_v0 = void(*)();
> > > +
> > > +SA(   __is_invocable( func_type_v0 ) );
> > > +SA( ! __is_invocable( func_type_v0, int ) );
> > > +
> > > +using func_type_i0 = int(*)();
> > > +
> > > +SA(   __is_invocable( func_type_i0 ) );
> > > +SA( ! __is_invocable( func_type_i0, int ) );
> > > +
> > > +using func_type_l0 = int&(*)();
> > > +
> > > +SA(   __is_invocable( func_type_l0 ) );
> > > +SA( ! __is_invocable( func_type_l0(int) ) );
> > > +
> > > +using func_type_ii = int(*)(int);
> > > +
> > > +SA( ! __is_invocable( func_type_ii ) );
> > > +SA(   __is_invocable( func_type_ii, int ) );
> > > +
> > > +using func_type_il = int(*)(int&);
> > > +
> > > +SA( ! __is_invocable( func_type_il ) );
> > > +SA( ! __is_invocable( func_type_il, int ) );
> > > +SA(   __is_invocable( func_type_il, int& ) );
> > > +
> > > +using func_type_ir = int(*)(int&&);
> > > +
> > > +SA( ! __is_invocable( func_type_ir ) );
> > > +SA( ! __is_invocable( func_type_ir, int& ) );
> > > +SA(   __is_invocable( func_type_ir, int ) );
> > > +SA(   __is_invocable( func_type_ir, int&& ) );
> > > +
> > > +struct A { };
> > > +
> > > +using mem_type_i = int A::*;
> > > +
> > > +SA( ! __is_invocable( mem_type_i ) );
> > > +SA( ! __is_invocable( mem_type_i, int ) );
> > > +SA( ! __is_invocable( mem_type_i, int* ) );
> > > +SA( ! __is_invocable( mem_type_i, int& ) );
> > > +SA( ! __is_invocable( mem_type_i, int&& ) );
> > > +SA(   __is_invocable( mem_type_i, A ) );
> > > +SA(   __is_invocable( mem_type_i, A* ) );
> > > +SA(   __is_invocable( mem_type_i, A& ) );
> > > +SA(   __is_invocable( mem_type_i, A&& ) );
> > > +SA(   __is_invocable( mem_type_i, const A& ) );
> > > +SA( ! __is_invocable( mem_type_i, A&, int ) );
> > > +
> > > +using memfun_type_i = int (A::*)();
> > > +
> > > +SA( ! __is_invocable( memfun_type_i ) );
> > > +SA( ! __is_invocable( memfun_type_i, int ) );
> > > +SA( ! __is_invocable( memfun_type_i, int* ) );
> > > +SA( ! __is_invocable( memfun_type_i, int& ) );
> > > +SA( ! __is_invocable( memfun_type_i, int&& ) );
> > > +SA(   __is_invocable( memfun_type_i, A ) );
> > > +SA(   __is_invocable( memfun_type_i, A* ) );
> > > +SA(   __is_invocable( memfun_type_i, A& ) );
> > > +SA(   __is_invocable( memfun_type_i, A&& ) );
> > > +SA( ! __is_invocable( memfun_type_i, const A& ) );
> > > +SA( ! __is_invocable( memfun_type_i, A&, int ) );
> > > +
> > > +using memfun_type_ic = int (A::*)() const;
> > > +
> > > +SA( ! __is_invocable( memfun_type_ic ) );
> > > +SA( ! __is_invocable( memfun_type_ic, int ) );
> > > +SA( ! __is_invocable( memfun_type_ic, int& ) );
> > > +SA(   __is_invocable( memfun_type_ic, A& ) );
> > > +SA(   __is_invocable( memfun_type_ic, A* ) );
> > > +SA( ! __is_invocable( memfun_type_ic, A&, int ) );
> > > +SA( ! __is_invocable( memfun_type_ic, A*, int& ) );
> > > +SA(   __is_invocable( memfun_type_ic, const A& ) );
> > > +SA(   __is_invocable( memfun_type_ic, const A* ) );
> > > +SA( ! __is_invocable( memfun_type_ic, const A&, int& ) );
> > > +SA( ! __is_invocable( memfun_type_ic, const A*, int ) );
> > > +
> > > +using memfun_type_iic = int& (A::*)(int&) const;
> > > +
> > > +SA( ! __is_invocable( memfun_type_iic ) );
> > > +SA( ! __is_invocable( memfun_type_iic, int ) );
> > > +SA( ! __is_invocable( memfun_type_iic, int& ) );
> > > +SA( ! __is_invocable( memfun_type_iic, A&, int ) );
> > > +SA(   __is_invocable( memfun_type_iic, A&, int& ) );
> > > +SA( ! __is_invocable( memfun_type_iic, A*, int ) );
> > > +SA(   __is_invocable( memfun_type_iic, A*, int& ) );
> > > +SA( ! __is_invocable( memfun_type_iic, const A&, int ) );
> > > +SA( ! __is_invocable( memfun_type_iic, const A&, int&, int ) );
> > > +SA(   __is_invocable( memfun_type_iic, const A&, int& ) );
> > > +SA(   __is_invocable( memfun_type_iic, const A*, int& ) );
> > > +
> > > +struct B {
> > > +  int& operator()();
> > > +  long& operator()() const;
> > > +  bool& operator()(int);
> > > +private:
> > > +  void operator()(int, int);
> > > +};
> > > +using CB = const B;
> > > +
> > > +SA(   __is_invocable( B ) );
> > > +SA(   __is_invocable( B& ) );
> > > +SA(   __is_invocable( B&& ) );
> > > +SA( ! __is_invocable( B* ) );
> > > +SA(   __is_invocable( CB ) );
> > > +SA(   __is_invocable( CB& ) );
> > > +SA( ! __is_invocable( CB* ) );
> > > +
> > > +SA(   __is_invocable( B, int ) );
> > > +SA(   __is_invocable( B&, int ) );
> > > +SA(   __is_invocable( B&&, int ) );
> > > +SA( ! __is_invocable( B*, int ) );
> > > +SA( ! __is_invocable( CB, int ) );
> > > +SA( ! __is_invocable( CB&, int ) );
> > > +SA( ! __is_invocable( CB*, int ) );
> > > +
> > > +SA( ! __is_invocable( B, int, int ) );
> > > +SA( ! __is_invocable( B&, int, int ) );
> > > +SA( ! __is_invocable( B&&, int, int ) );
> > > +SA( ! __is_invocable( B*, int, int ) );
> > > +SA( ! __is_invocable( CB, int, int ) );
> > > +SA( ! __is_invocable( CB&, int, int ) );
> > > +SA( ! __is_invocable( CB*, int, int ) );
> > > +
> > > +struct C : B { int& operator()() = delete; };
> > > +using CC = const C;
> > > +
> > > +SA( ! __is_invocable( C ) );
> > > +SA( ! __is_invocable( C& ) );
> > > +SA( ! __is_invocable( C&& ) );
> > > +SA( ! __is_invocable( C* ) );
> > > +SA( ! __is_invocable( CC ) );
> > > +SA( ! __is_invocable( CC& ) );
> > > +SA( ! __is_invocable( CC* ) );
> > > +
> > > +struct D { B operator*(); };
> > > +using CD = const D;
> > > +
> > > +SA( ! __is_invocable( D ) );
> > > +
> > > +struct E { void v(); };
> > > +using CE = const E;
> > > +
> > > +SA( ! __is_invocable( E ) );
> > > +SA( ! __is_invocable( void (E::*)() ) );
> > > +SA(   __is_invocable( void (E::*)(), E ) );
> > > +SA(   __is_invocable( void (E::*)(), E* ) );
> > > +SA( ! __is_invocable( void (E::*)(), CE ) );
> > > +
> > > +struct F : E {};
> > > +using CF = const F;
> > > +
> > > +SA( ! __is_invocable( F ) );
> > > +SA(   __is_invocable( void (E::*)(), F ) );
> > > +SA(   __is_invocable( void (E::*)(), F* ) );
> > > +SA( ! __is_invocable( void (E::*)(), CF ) );
> > > +
> > > +struct G { E operator*(); };
> > > +using CG = const G;
> > > +
> > > +SA( ! __is_invocable( G ) );
> > > +SA(   __is_invocable( void (E::*)(), G ) );
> > > +SA( ! __is_invocable( void (E::*)(), G* ) );
> > > +SA( ! __is_invocable( void (E::*)(), CG ) );
> > > +
> > > +struct H { E& operator*(); };
> > > +using CH = const H;
> > > +
> > > +SA( ! __is_invocable( H ) );
> > > +SA(   __is_invocable( void (E::*)(), H ) );
> > > +SA( ! __is_invocable( void (E::*)(), H* ) );
> > > +SA( ! __is_invocable( void (E::*)(), CH ) );
> > > +
> > > +struct I { E&& operator*(); };
> > > +using CI = const I;
> > > +
> > > +SA( ! __is_invocable( I ) );
> > > +SA(   __is_invocable( void (E::*)(), I ) );
> > > +SA( ! __is_invocable( void (E::*)(), I* ) );
> > > +SA( ! __is_invocable( void (E::*)(), CI ) );
> > > +
> > > +struct K { E* operator*(); };
> > > +using CK = const K;
> > > +
> > > +SA( ! __is_invocable( K ) );
> > > +SA( ! __is_invocable( void (E::*)(), K ) );
> > > +SA( ! __is_invocable( void (E::*)(), K* ) );
> > > +SA( ! __is_invocable( void (E::*)(), CK ) );
> > > +
> > > +struct L { CE operator*(); };
> > > +using CL = const L;
> > > +
> > > +SA( ! __is_invocable( L ) );
> > > +SA( ! __is_invocable( void (E::*)(), L ) );
> > > +SA( ! __is_invocable( void (E::*)(), L* ) );
> > > +SA( ! __is_invocable( void (E::*)(), CL ) );
> > > +
> > > +struct M {
> > > +  int i;
> > > +private:
> > > +  long l;
> > > +};
> > > +using CM = const M;
> > > +
> > > +SA( ! __is_invocable( M ) );
> > > +SA( ! __is_invocable( M& ) );
> > > +SA( ! __is_invocable( M&& ) );
> > > +SA( ! __is_invocable( M* ) );
> > > +SA( ! __is_invocable( CM ) );
> > > +SA( ! __is_invocable( CM& ) );
> > > +SA( ! __is_invocable( CM* ) );
> > > +
> > > +SA( ! __is_invocable( int M::* ) );
> > > +SA(   __is_invocable( int M::*, M ) );
> > > +SA(   __is_invocable( int M::*, M& ) );
> > > +SA(   __is_invocable( int M::*, M&& ) );
> > > +SA(   __is_invocable( int M::*, M* ) );
> > > +SA(   __is_invocable( int M::*, CM ) );
> > > +SA(   __is_invocable( int M::*, CM& ) );
> > > +SA(   __is_invocable( int M::*, CM* ) );
> > > +SA( ! __is_invocable( int M::*, int ) );
> > > +
> > > +SA( ! __is_invocable( int CM::* ) );
> > > +SA(   __is_invocable( int CM::*, M ) );
> > > +SA(   __is_invocable( int CM::*, M& ) );
> > > +SA(   __is_invocable( int CM::*, M&& ) );
> > > +SA(   __is_invocable( int CM::*, M* ) );
> > > +SA(   __is_invocable( int CM::*, CM ) );
> > > +SA(   __is_invocable( int CM::*, CM& ) );
> > > +SA(   __is_invocable( int CM::*, CM* ) );
> > > +SA( ! __is_invocable( int CM::*, int ) );
> > > +
> > > +SA( ! __is_invocable( long M::* ) );
> > > +SA(   __is_invocable( long M::*, M ) );
> > > +SA(   __is_invocable( long M::*, M& ) );
> > > +SA(   __is_invocable( long M::*, M&& ) );
> > > +SA(   __is_invocable( long M::*, M* ) );
> > > +SA(   __is_invocable( long M::*, CM ) );
> > > +SA(   __is_invocable( long M::*, CM& ) );
> > > +SA(   __is_invocable( long M::*, CM* ) );
> > > +SA( ! __is_invocable( long M::*, long ) );
> > > +
> > > +SA( ! __is_invocable( long CM::* ) );
> > > +SA(   __is_invocable( long CM::*, M ) );
> > > +SA(   __is_invocable( long CM::*, M& ) );
> > > +SA(   __is_invocable( long CM::*, M&& ) );
> > > +SA(   __is_invocable( long CM::*, M* ) );
> > > +SA(   __is_invocable( long CM::*, CM ) );
> > > +SA(   __is_invocable( long CM::*, CM& ) );
> > > +SA(   __is_invocable( long CM::*, CM* ) );
> > > +SA( ! __is_invocable( long CM::*, long ) );
> > > +
> > > +SA( ! __is_invocable( short M::* ) );
> > > +SA(   __is_invocable( short M::*, M ) );
> > > +SA(   __is_invocable( short M::*, M& ) );
> > > +SA(   __is_invocable( short M::*, M&& ) );
> > > +SA(   __is_invocable( short M::*, M* ) );
> > > +SA(   __is_invocable( short M::*, CM ) );
> > > +SA(   __is_invocable( short M::*, CM& ) );
> > > +SA(   __is_invocable( short M::*, CM* ) );
> > > +SA( ! __is_invocable( short M::*, short ) );
> > > +
> > > +SA( ! __is_invocable( short CM::* ) );
> > > +SA(   __is_invocable( short CM::*, M ) );
> > > +SA(   __is_invocable( short CM::*, M& ) );
> > > +SA(   __is_invocable( short CM::*, M&& ) );
> > > +SA(   __is_invocable( short CM::*, M* ) );
> > > +SA(   __is_invocable( short CM::*, CM ) );
> > > +SA(   __is_invocable( short CM::*, CM& ) );
> > > +SA(   __is_invocable( short CM::*, CM* ) );
> > > +SA( ! __is_invocable( short CM::*, short ) );
> > > +
> > > +struct N { M operator*(); };
> > > +SA(   __is_invocable( int M::*, N ) );
> > > +SA( ! __is_invocable( int M::*, N* ) );
> > > +
> > > +struct O { M& operator*(); };
> > > +SA(   __is_invocable( int M::*, O ) );
> > > +SA( ! __is_invocable( int M::*, O* ) );
> > > +
> > > +struct P { M&& operator*(); };
> > > +SA(   __is_invocable( int M::*, P ) );
> > > +SA( ! __is_invocable( int M::*, P* ) );
> > > +
> > > +struct Q { M* operator*(); };
> > > +SA( ! __is_invocable( int M::*, Q ) );
> > > +SA( ! __is_invocable( int M::*, Q* ) );
> > > +
> > > +struct R { void operator()(int = 0); };
> > > +
> > > +SA(   __is_invocable( R ) );
> > > +SA(   __is_invocable( R, int ) );
> > > +SA( ! __is_invocable( R, int, int ) );
> > > +
> > > +struct S { void operator()(int, ...); };
> > > +
> > > +SA( ! __is_invocable( S ) );
> > > +SA(   __is_invocable( S, int ) );
> > > +SA(   __is_invocable( S, int, int ) );
> > > +SA(   __is_invocable( S, int, int, int ) );
> > > +
> > > +void fn1() {}
> > > +
> > > +SA(   __is_invocable( decltype(fn1) ) );
> > > +
> > > +void fn2(int arr[10]);
> > > +
> > > +SA(   __is_invocable( decltype(fn2), int[10] ) );
> > > +SA(   __is_invocable( decltype(fn2), int(&)[10] ) );
> > > +SA(   __is_invocable( decltype(fn2), int(&&)[10] ) );
> > > +SA( ! __is_invocable( decltype(fn2), int(*)[10] ) );
> > > +SA( ! __is_invocable( decltype(fn2), int(*&)[10] ) );
> > > +SA( ! __is_invocable( decltype(fn2), int(*&&)[10] ) );
> > > +SA(   __is_invocable( decltype(fn2), int[] ) );
> > > +
> > > +auto lambda = []() {};
> > > +
> > > +SA(   __is_invocable( decltype(lambda) ) );
> > > +
> > > +template <typename Func, typename... Args>
> > > +struct can_invoke {
> > > +    static constexpr bool value = __is_invocable( Func, Args... );
> > > +};
> > > +
> > > +SA( can_invoke<decltype(lambda)>::value );
> > > +
> > > +struct T {
> > > +  void func() const {}
> > > +  int data;
> > > +};
> > > +
> > > +SA(   __is_invocable( decltype(&T::func)&, T& ) );
> > > +SA(   __is_invocable( decltype(&T::data)&, T& ) );
> > > diff --git a/gcc/testsuite/g++.dg/ext/is_invocable2.C b/gcc/testsuite/g++.dg/ext/is_invocable2.C
> > > new file mode 100644
> > > index 00000000000..a68aefd3e13
> > > --- /dev/null
> > > +++ b/gcc/testsuite/g++.dg/ext/is_invocable2.C
> > > @@ -0,0 +1,139 @@
> > > +// { dg-do compile { target c++11 } }
> > > +// __is_invocable should handle std::reference_wrapper correctly.
> > > +
> > > +#include <functional>
> > > +
> > > +#define SA(X) static_assert((X),#X)
> > > +
> > > +using std::reference_wrapper;
> > > +
> > > +using func_type_v0 = void(*)();
> > > +
> > > +SA(   __is_invocable( reference_wrapper<func_type_v0> ) );
> > > +SA( ! __is_invocable( reference_wrapper<func_type_v0>, int ) );
> > > +
> > > +using func_type_i0 = int(*)();
> > > +
> > > +SA(   __is_invocable( reference_wrapper<func_type_i0> ) );
> > > +SA( ! __is_invocable( reference_wrapper<func_type_i0>, int ) );
> > > +
> > > +using func_type_l0 = int&(*)();
> > > +
> > > +SA(   __is_invocable( reference_wrapper<func_type_l0> ) );
> > > +SA( ! __is_invocable( reference_wrapper<func_type_l0(int)> ) );
> > > +
> > > +using func_type_ii = int(*)(int);
> > > +
> > > +SA( ! __is_invocable( reference_wrapper<func_type_ii> ) );
> > > +SA(   __is_invocable( reference_wrapper<func_type_ii>, int ) );
> > > +
> > > +using func_type_il = int(*)(int&);
> > > +
> > > +SA( ! __is_invocable( reference_wrapper<func_type_il> ) );
> > > +SA( ! __is_invocable( reference_wrapper<func_type_il>, int ) );
> > > +SA(   __is_invocable( reference_wrapper<func_type_il>, int& ) );
> > > +
> > > +using func_type_ir = int(*)(int&&);
> > > +
> > > +SA( ! __is_invocable( reference_wrapper<func_type_ir> ) );
> > > +SA( ! __is_invocable( reference_wrapper<func_type_ir>, int& ) );
> > > +SA(   __is_invocable( reference_wrapper<func_type_ir>, int ) );
> > > +SA(   __is_invocable( reference_wrapper<func_type_ir>, int&& ) );
> > > +
> > > +struct A { };
> > > +
> > > +using mem_type_i = int A::*;
> > > +
> > > +SA( ! __is_invocable( reference_wrapper<mem_type_i> ) );
> > > +SA( ! __is_invocable( reference_wrapper<mem_type_i>, int ) );
> > > +SA( ! __is_invocable( reference_wrapper<mem_type_i>, int* ) );
> > > +SA( ! __is_invocable( reference_wrapper<mem_type_i>, int& ) );
> > > +SA( ! __is_invocable( reference_wrapper<mem_type_i>, int&& ) );
> > > +SA(   __is_invocable( reference_wrapper<mem_type_i>, A ) );
> > > +SA(   __is_invocable( reference_wrapper<mem_type_i>, A* ) );
> > > +SA(   __is_invocable( reference_wrapper<mem_type_i>, A& ) );
> > > +SA(   __is_invocable( reference_wrapper<mem_type_i>, A&& ) );
> > > +
> > > +using memfun_type_i = int (A::*)();
> > > +
> > > +SA( ! __is_invocable( reference_wrapper<memfun_type_i> ) );
> > > +SA( ! __is_invocable( reference_wrapper<memfun_type_i>, int ) );
> > > +SA( ! __is_invocable( reference_wrapper<memfun_type_i>, int* ) );
> > > +SA( ! __is_invocable( reference_wrapper<memfun_type_i>, int& ) );
> > > +SA( ! __is_invocable( reference_wrapper<memfun_type_i>, int&& ) );
> > > +SA(   __is_invocable( reference_wrapper<memfun_type_i>, A ) );
> > > +SA(   __is_invocable( reference_wrapper<memfun_type_i>, A* ) );
> > > +SA(   __is_invocable( reference_wrapper<memfun_type_i>, A& ) );
> > > +SA(   __is_invocable( reference_wrapper<memfun_type_i>, A&& ) );
> > > +SA( ! __is_invocable( reference_wrapper<memfun_type_i>, const A& ) );
> > > +SA( ! __is_invocable( reference_wrapper<memfun_type_i>, A&, int ) );
> > > +
> > > +using memfun_type_ic = int (A::*)() const;
> > > +
> > > +SA( ! __is_invocable( reference_wrapper<memfun_type_ic> ) );
> > > +SA( ! __is_invocable( reference_wrapper<memfun_type_ic>, int ) );
> > > +SA( ! __is_invocable( reference_wrapper<memfun_type_ic>, int& ) );
> > > +SA(   __is_invocable( reference_wrapper<memfun_type_ic>, A& ) );
> > > +SA(   __is_invocable( reference_wrapper<memfun_type_ic>, A* ) );
> > > +SA( ! __is_invocable( reference_wrapper<memfun_type_ic>, A&, int ) );
> > > +SA( ! __is_invocable( reference_wrapper<memfun_type_ic>, A*, int& ) );
> > > +SA(   __is_invocable( reference_wrapper<memfun_type_ic>, const A& ) );
> > > +SA(   __is_invocable( reference_wrapper<memfun_type_ic>, const A* ) );
> > > +SA( ! __is_invocable( reference_wrapper<memfun_type_ic>, const A&, int& ) );
> > > +SA( ! __is_invocable( reference_wrapper<memfun_type_ic>, const A*, int ) );
> > > +
> > > +using memfun_type_iic = int& (A::*)(int&) const;
> > > +
> > > +SA( ! __is_invocable( reference_wrapper<memfun_type_iic> ) );
> > > +SA( ! __is_invocable( reference_wrapper<memfun_type_iic>, int ) );
> > > +SA( ! __is_invocable( reference_wrapper<memfun_type_iic>, int& ) );
> > > +SA( ! __is_invocable( reference_wrapper<memfun_type_iic>, A&, int ) );
> > > +SA(   __is_invocable( reference_wrapper<memfun_type_iic>, A&, int& ) );
> > > +SA( ! __is_invocable( reference_wrapper<memfun_type_iic>, A*, int ) );
> > > +SA(   __is_invocable( reference_wrapper<memfun_type_iic>, A*, int& ) );
> > > +SA( ! __is_invocable( reference_wrapper<memfun_type_iic>, const A&, int ) );
> > > +SA( ! __is_invocable( reference_wrapper<memfun_type_iic>, const A&, int&, int ) );
> > > +SA(   __is_invocable( reference_wrapper<memfun_type_iic>, const A&, int& ) );
> > > +SA(   __is_invocable( reference_wrapper<memfun_type_iic>, const A*, int& ) );
> > > +
> > > +struct B {
> > > +  int& operator()();
> > > +  long& operator()() const;
> > > +  bool& operator()(int);
> > > +private:
> > > +  void operator()(int, int);
> > > +};
> > > +using CB = const B;
> > > +
> > > +SA(   __is_invocable( reference_wrapper<B> ) );
> > > +SA(   __is_invocable( reference_wrapper<B>& ) );
> > > +SA(   __is_invocable( reference_wrapper<B>&& ) );
> > > +SA(   __is_invocable( reference_wrapper<CB> ) );
> > > +SA(   __is_invocable( reference_wrapper<CB>& ) );
> > > +SA(   __is_invocable( reference_wrapper<B>, int ) );
> > > +SA( ! __is_invocable( reference_wrapper<B>&, int, int ) );
> > > +
> > > +struct C : B { int& operator()() = delete; };
> > > +using CC = const C;
> > > +
> > > +SA( ! __is_invocable( reference_wrapper<C> ) );
> > > +SA( ! __is_invocable( reference_wrapper<C>& ) );
> > > +SA( ! __is_invocable( reference_wrapper<C>&& ) );
> > > +SA( ! __is_invocable( reference_wrapper<CC> ) );
> > > +SA( ! __is_invocable( reference_wrapper<CC>& ) );
> > > +
> > > +struct D { B operator*(); };
> > > +using CD = const D;
> > > +
> > > +SA( ! __is_invocable( reference_wrapper<D> ) );
> > > +SA( ! __is_invocable( reference_wrapper<D>& ) );
> > > +SA( ! __is_invocable( reference_wrapper<D>&& ) );
> > > +SA( ! __is_invocable( reference_wrapper<D>* ) );
> > > +SA( ! __is_invocable( reference_wrapper<D*> ) );
> > > +SA( ! __is_invocable( reference_wrapper<D*>* ) );
> > > +
> > > +std::function<void()> fn = []() {};
> > > +auto refwrap = std::ref(fn);
> > > +
> > > +SA(   __is_invocable( decltype(fn) ) );
> > > +SA(   __is_invocable( decltype(refwrap) ) );
> > > diff --git a/gcc/testsuite/g++.dg/ext/is_invocable3.C b/gcc/testsuite/g++.dg/ext/is_invocable3.C
> > > new file mode 100644
> > > index 00000000000..e2b0c5ef406
> > > --- /dev/null
> > > +++ b/gcc/testsuite/g++.dg/ext/is_invocable3.C
> > > @@ -0,0 +1,51 @@
> > > +// { dg-do compile { target c++11 } }
> > > +// __is_invocable should handle incomplete class correctly.
> > > +
> > > +#define SA(X) static_assert((X),#X)
> > > +
> > > +struct Incomplete;
> > > +
> > > +SA( ! __is_invocable( Incomplete ) ); // { dg-error "incomplete type" }
> > > +SA( ! __is_invocable( Incomplete, int ) ); // { dg-error "incomplete type" }
> > > +
> > > +SA( ! __is_invocable( int, Incomplete, int ) ); // { dg-error "incomplete type" }
> > > +SA( ! __is_invocable( int, Incomplete ) ); // { dg-error "incomplete type" }
> > > +
> > > +SA( ! __is_invocable( Incomplete, Incomplete() ) ); // { dg-error "incomplete type" }
> > > +SA( ! __is_invocable( Incomplete, Incomplete(int), int ) ); // { dg-error "incomplete type" }
> > > +SA( ! __is_invocable( Incomplete, Incomplete(int, int), int, int ) ); // { dg-error "incomplete type" }
> > > +
> > > +SA( ! __is_invocable( Incomplete, Incomplete(), int, int ) ); // { dg-error "incomplete type" }
> > > +
> > > +SA( ! __is_invocable( int(Incomplete), Incomplete ) ); // { dg-error "incomplete type" }
> > > +SA( ! __is_invocable( int(int, Incomplete), int, Incomplete ) ); // { dg-error "incomplete type" }
> > > +SA( ! __is_invocable( int(int, Incomplete), Incomplete, int ) ); // { dg-error "incomplete type" }
> > > +
> > > +SA(   __is_invocable( int(Incomplete&), Incomplete& ) ); // { dg-bogus "incomplete type" }
> > > +SA(   __is_invocable( int(int, Incomplete&), int, Incomplete& ) ); // { dg-bogus "incomplete type" }
> > > +
> > > +SA(   __is_invocable( int(Incomplete&&), Incomplete&& ) ); // { dg-bogus "incomplete type" }
> > > +SA(   __is_invocable( int(int, Incomplete&&), int, Incomplete&& ) ); // { dg-bogus "incomplete type" }
> > > +
> > > +SA(   __is_invocable( int(const Incomplete&&), const Incomplete&& ) ); // { dg-bogus "incomplete type" }
> > > +SA(   __is_invocable( int(int, const Incomplete&&), int, const Incomplete&& ) ); // { dg-bogus "incomplete type" }
> > > +
> > > +SA(   __is_invocable( int(const Incomplete&), const Incomplete& ) ); // { dg-bogus "incomplete type" }
> > > +SA(   __is_invocable( int(int, const Incomplete&), int, const Incomplete& ) ); // { dg-bogus "incomplete type" }
> > > +
> > > +SA(   __is_invocable( int(const Incomplete&), Incomplete& ) ); // { dg-bogus "incomplete type" }
> > > +SA(   __is_invocable( int(int, const Incomplete&), int, Incomplete& ) ); // { dg-bogus "incomplete type" }
> > > +
> > > +SA(   __is_invocable( int Incomplete::*, const Incomplete& ) ); // { dg-bogus "incomplete type" }
> > > +SA( ! __is_invocable( void (Incomplete::*)(long&), const Incomplete*, long& ) ); // { dg-bogus "incomplete type" }
> > > +SA(   __is_invocable( void (Incomplete::*)(long&) const, Incomplete*, long& ) ); // { dg-bogus "incomplete type" }
> > > +
> > > +template <typename T>
> > > +struct Holder { T t; };
> > > +
> > > +SA(   __is_invocable( int(Holder<Incomplete>&), Holder<Incomplete>& ) ); // { dg-bogus "incomplete type" }
> > > +
> > > +// Define Incomplete, which is now not incomplete.
> > > +struct Incomplete { void operator()(); };
> > > +
> > > +SA( __is_invocable( Incomplete ) ); // { dg-bogus "incomplete type" }
> > > diff --git a/gcc/testsuite/g++.dg/ext/is_invocable4.C b/gcc/testsuite/g++.dg/ext/is_invocable4.C
> > > new file mode 100644
> > > index 00000000000..d1efccf08f8
> > > --- /dev/null
> > > +++ b/gcc/testsuite/g++.dg/ext/is_invocable4.C
> > > @@ -0,0 +1,33 @@
> > > +// { dg-do compile { target c++11 } }
> > > +// Failed access check should be a substitution failure, not an error.
> > > +
> > > +#define SA(X) static_assert((X),#X)
> > > +
> > > +template<bool B>
> > > +struct bool_constant { static constexpr bool value = B; };
> > > +
> > > +template<typename _Fn, typename... _ArgTypes>
> > > +struct is_invocable
> > > +: public bool_constant<__is_invocable(_Fn, _ArgTypes...)>
> > > +{ };
> > > +
> > > +#if __cpp_variable_templates
> > > +template<typename _Fn, typename... _ArgTypes>
> > > +constexpr bool is_invocable_v = __is_invocable(_Fn, _ArgTypes...);
> > > +#endif
> > > +
> > > +class Private
> > > +{
> > > +  void operator()() const
> > > +  {
> > > +    SA( ! is_invocable<Private>::value );
> > > +#if __cpp_variable_templates
> > > +    SA( ! is_invocable_v<Private> );
> > > +#endif
> > > +  }
> > > +};
> > > +
> > > +SA( ! is_invocable<Private>::value );
> > > +#if __cpp_variable_templates
> > > +SA( ! is_invocable_v<Private> );
> > > +#endif
> > > -- 
> > > 2.42.0
> > > 
> > > 
> > 
>
  
Jason Merrill Oct. 23, 2023, 9:23 p.m. UTC | #4
On 10/20/23 17:37, Patrick Palka wrote:
> On Fri, 20 Oct 2023, Patrick Palka wrote:
> 
>> On Fri, 20 Oct 2023, Patrick Palka wrote:
>>
>>> On Fri, 20 Oct 2023, Ken Matsui wrote:
>>>
>>>> This patch implements built-in trait for std::is_invocable.
>>>
>>> Nice!  My email client unfortunately ate my first review attempt, so
>>> apologies for my brevity this time around.
>>>
>>>> gcc/cp/ChangeLog:
>>>>
>>>> 	* cp-trait.def: Define __is_invocable.
>>>> 	* constraint.cc (diagnose_trait_expr): Handle CPTK_IS_INVOCABLE.
>>>> 	* semantics.cc (trait_expr_value): Likewise.
>>>> 	(finish_trait_expr): Likewise.
>>>> 	(is_invocable_p): New function.
>>>> 	* method.h: New file to export build_trait_object in method.cc.

Given how much larger semantics.cc is than method.cc, maybe let's put 
is_invocable_p in method.cc instead?  And in general declarations can go 
in cp-tree.h.

>>>> diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
>>>> index 7cccbae5287..cc2e400531a 100644
>>>> --- a/gcc/cp/semantics.cc
>>>> +++ b/gcc/cp/semantics.cc
>>>> @@ -45,6 +45,10 @@ along with GCC; see the file COPYING3.  If not see
>>>>   #include "gomp-constants.h"
>>>>   #include "predict.h"
>>>>   #include "memmodel.h"
>>>> +#include "method.h"
>>>> +
>>>> +#include "print-tree.h"
>>>> +#include "tree-pretty-print.h"
>>>>   
>>>>   /* There routines provide a modular interface to perform many parsing
>>>>      operations.  They may therefore be used during actual parsing, or
>>>> @@ -11714,6 +11718,133 @@ classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
>>>>     return saw_copy;
>>>>   }
>>>>   
>>>> +/* Return true if FN_TYPE is invocable with the given ARG_TYPES.  */
>>>> +
>>>> +static bool
>>>> +is_invocable_p (tree fn_type, tree arg_types)
> 
> (Sorry for the spam)  We'll eventually want to implement a built-in for
> invoke_result, so perhaps we should preemptively factor out the bulk
> of this function into a 'build_INVOKE' helper function that returns the
> built tree?
> 
>>>> +{
>>>> +  /* ARG_TYPES must be a TREE_VEC.  */
>>>> +  gcc_assert (TREE_CODE (arg_types) == TREE_VEC);
>>>> +
>>>> +  /* Access check is required to determine if the given is invocable.  */
>>>> +  deferring_access_check_sentinel acs (dk_no_deferred);
>>>> +
>>>> +  /* std::is_invocable is an unevaluated context.  */
>>>> +  cp_unevaluated cp_uneval_guard;
>>>> +
>>>> +  bool is_ptrdatamem;
>>>> +  bool is_ptrmemfunc;
>>>> +  if (TREE_CODE (fn_type) == REFERENCE_TYPE)
>>>> +    {
>>>> +      tree deref_fn_type = TREE_TYPE (fn_type);
>>>> +      is_ptrdatamem = TYPE_PTRDATAMEM_P (deref_fn_type);
>>>> +      is_ptrmemfunc = TYPE_PTRMEMFUNC_P (deref_fn_type);
>>>> +
>>>> +      /* Dereference fn_type if it is a pointer to member.  */
>>>> +      if (is_ptrdatamem || is_ptrmemfunc)
>>>> +	fn_type = deref_fn_type;
>>>> +    }
>>>> +  else
>>>> +    {
>>>> +      is_ptrdatamem = TYPE_PTRDATAMEM_P (fn_type);
>>>> +      is_ptrmemfunc = TYPE_PTRMEMFUNC_P (fn_type);
>>>> +    }
>>>> +
>>>> +  if (is_ptrdatamem && TREE_VEC_LENGTH (arg_types) != 1)
>>>> +    /* A pointer to data member with non-one argument is not invocable.  */
>>>> +    return false;
>>>> +
>>>> +  if (is_ptrmemfunc && TREE_VEC_LENGTH (arg_types) == 0)
>>>> +    /* A pointer to member function with no arguments is not invocable.  */
>>>> +    return false;
>>>> +
>>>> +  /* Construct an expression of a pointer to member.  */
>>>> +  tree datum;
>>>> +  if (is_ptrdatamem || is_ptrmemfunc)
>>>> +    {
>>>> +      tree datum_type = TREE_VEC_ELT (arg_types, 0);
>>>> +
>>>> +      /* Dereference datum.  */
>>>> +      if (CLASS_TYPE_P (datum_type))
>>>> +	{
>>>> +	  bool is_refwrap = false;
>>>> +
>>>> +	  tree datum_decl = TYPE_NAME (TYPE_MAIN_VARIANT (datum_type));
>>>> +	  if (decl_in_std_namespace_p (datum_decl))
>>>> +	    {
>>>> +	      tree name = DECL_NAME (datum_decl);
>>>> +	      if (name && (id_equal (name, "reference_wrapper")))
>>>> +		{
>>>> +		  /* Handle std::reference_wrapper.  */
>>>> +		  is_refwrap = true;
>>>> +		  datum_type = cp_build_reference_type (datum_type, false);

Why do you change datum_type from std::reference_wrapper<...> to 
std::reference_wrapper<...>&?

>>>> +		}
>>>> +	    }
>>>> +
>>>> +	  datum = build_trait_object (datum_type);
>>>> +
>>>> +	  /* If datum_type was not std::reference_wrapper, check if it has
>>>> +	     operator*() overload.  If datum_type was std::reference_wrapper,
>>>> +	     avoid dereferencing the datum twice.  */
>>>> +	  if (!is_refwrap)
>>>> +	    if (get_class_binding (datum_type, get_identifier ("operator*")))
>>>
>>> We probably should use lookup_member instead of get_class_binding since
>>> IIUC the latter doesn't look into bases:
>>>
>>>    struct A { int m; };
>>>    struct B { A& operator*(): };
>>>    struct C : B { };
>>>    static_assert(std::is_invocable_v<int A::*, C>);
>>>
>>> However, I notice that the specification of INVOKE
>>> (https://eel.is/c++draft/func.require#lib:INVOKE) doesn't mention name
>>> lookup at all so it strikes me as suspicious that we'd perform name
>>> lookup here.

Agreed.  It seems that whether or not to build_x_indirect_ref should 
depend instead on whether f is a pointer to a member of decltype(t1) (as 
well as is_refwrap).

>>>  I think this would misbehave for:
>>>
>>>    struct A { };
>>>    struct B : A { A& operator*() = delete; };
>>>    static_assert(std::is_invocable_v<int A::*, B>);
>>>
>>>    struct C : private A { A& operator*(); };
>>>    static_assert(std::is_invocable_v<int A::*, C>);
>>
>> Oops, this static_assert is missing a !
>>
>>>
>>> ultimately because we end up choosing the dereference form of INVOKE,
>>> but according to 1.1/1.4 we should choose the non-dereference form?
>>>
>>>> +	      /* Handle operator*().  */
>>>> +	      datum = build_x_indirect_ref (UNKNOWN_LOCATION, datum,
>>>> +					    RO_UNARY_STAR, NULL_TREE,
>>>> +					    tf_none);
>>>> +	}
>>>> +      else if (POINTER_TYPE_P (datum_type))
>>>> +	datum = build_trait_object (TREE_TYPE (datum_type));
>>>> +      else
>>>> +	datum = build_trait_object (datum_type);
>>>> +    }
>>>> +
>>>> +  /* Build a function expression.  */
>>>> +  tree fn;
>>>> +  if (is_ptrdatamem)
>>>> +    fn = build_m_component_ref (datum, build_trait_object (fn_type), tf_none);
>>>
>>> Maybe exit early for the is_ptrdatamem case here (and simplify the rest
>>> of the function accordingly)?
>>>
>>>> +  else if (is_ptrmemfunc)
>>>> +    fn = build_trait_object (TYPE_PTRMEMFUNC_FN_TYPE (fn_type));

Why not use build_m_component_ref and build_offset_ref_call_from_tree 
like it would if you wrote (t1.*f)() directly?

Jason
  
Ken Matsui Feb. 20, 2024, 1:35 a.m. UTC | #5
On Mon, Oct 23, 2023 at 2:23 PM Jason Merrill <jason@redhat.com> wrote:
>
> On 10/20/23 17:37, Patrick Palka wrote:
> > On Fri, 20 Oct 2023, Patrick Palka wrote:
> >
> >> On Fri, 20 Oct 2023, Patrick Palka wrote:
> >>
> >>> On Fri, 20 Oct 2023, Ken Matsui wrote:
> >>>
> >>>> This patch implements built-in trait for std::is_invocable.
> >>>
> >>> Nice!  My email client unfortunately ate my first review attempt, so
> >>> apologies for my brevity this time around.
> >>>
> >>>> gcc/cp/ChangeLog:
> >>>>
> >>>>    * cp-trait.def: Define __is_invocable.
> >>>>    * constraint.cc (diagnose_trait_expr): Handle CPTK_IS_INVOCABLE.
> >>>>    * semantics.cc (trait_expr_value): Likewise.
> >>>>    (finish_trait_expr): Likewise.
> >>>>    (is_invocable_p): New function.
> >>>>    * method.h: New file to export build_trait_object in method.cc.
>
> Given how much larger semantics.cc is than method.cc, maybe let's put
> is_invocable_p in method.cc instead?  And in general declarations can go
> in cp-tree.h.
>
> >>>> diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
> >>>> index 7cccbae5287..cc2e400531a 100644
> >>>> --- a/gcc/cp/semantics.cc
> >>>> +++ b/gcc/cp/semantics.cc
> >>>> @@ -45,6 +45,10 @@ along with GCC; see the file COPYING3.  If not see
> >>>>   #include "gomp-constants.h"
> >>>>   #include "predict.h"
> >>>>   #include "memmodel.h"
> >>>> +#include "method.h"
> >>>> +
> >>>> +#include "print-tree.h"
> >>>> +#include "tree-pretty-print.h"
> >>>>
> >>>>   /* There routines provide a modular interface to perform many parsing
> >>>>      operations.  They may therefore be used during actual parsing, or
> >>>> @@ -11714,6 +11718,133 @@ classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
> >>>>     return saw_copy;
> >>>>   }
> >>>>
> >>>> +/* Return true if FN_TYPE is invocable with the given ARG_TYPES.  */
> >>>> +
> >>>> +static bool
> >>>> +is_invocable_p (tree fn_type, tree arg_types)
> >
> > (Sorry for the spam)  We'll eventually want to implement a built-in for
> > invoke_result, so perhaps we should preemptively factor out the bulk
> > of this function into a 'build_INVOKE' helper function that returns the
> > built tree?
> >
> >>>> +{
> >>>> +  /* ARG_TYPES must be a TREE_VEC.  */
> >>>> +  gcc_assert (TREE_CODE (arg_types) == TREE_VEC);
> >>>> +
> >>>> +  /* Access check is required to determine if the given is invocable.  */
> >>>> +  deferring_access_check_sentinel acs (dk_no_deferred);
> >>>> +
> >>>> +  /* std::is_invocable is an unevaluated context.  */
> >>>> +  cp_unevaluated cp_uneval_guard;
> >>>> +
> >>>> +  bool is_ptrdatamem;
> >>>> +  bool is_ptrmemfunc;
> >>>> +  if (TREE_CODE (fn_type) == REFERENCE_TYPE)
> >>>> +    {
> >>>> +      tree deref_fn_type = TREE_TYPE (fn_type);
> >>>> +      is_ptrdatamem = TYPE_PTRDATAMEM_P (deref_fn_type);
> >>>> +      is_ptrmemfunc = TYPE_PTRMEMFUNC_P (deref_fn_type);
> >>>> +
> >>>> +      /* Dereference fn_type if it is a pointer to member.  */
> >>>> +      if (is_ptrdatamem || is_ptrmemfunc)
> >>>> +  fn_type = deref_fn_type;
> >>>> +    }
> >>>> +  else
> >>>> +    {
> >>>> +      is_ptrdatamem = TYPE_PTRDATAMEM_P (fn_type);
> >>>> +      is_ptrmemfunc = TYPE_PTRMEMFUNC_P (fn_type);
> >>>> +    }
> >>>> +
> >>>> +  if (is_ptrdatamem && TREE_VEC_LENGTH (arg_types) != 1)
> >>>> +    /* A pointer to data member with non-one argument is not invocable.  */
> >>>> +    return false;
> >>>> +
> >>>> +  if (is_ptrmemfunc && TREE_VEC_LENGTH (arg_types) == 0)
> >>>> +    /* A pointer to member function with no arguments is not invocable.  */
> >>>> +    return false;
> >>>> +
> >>>> +  /* Construct an expression of a pointer to member.  */
> >>>> +  tree datum;
> >>>> +  if (is_ptrdatamem || is_ptrmemfunc)
> >>>> +    {
> >>>> +      tree datum_type = TREE_VEC_ELT (arg_types, 0);
> >>>> +
> >>>> +      /* Dereference datum.  */
> >>>> +      if (CLASS_TYPE_P (datum_type))
> >>>> +  {
> >>>> +    bool is_refwrap = false;
> >>>> +
> >>>> +    tree datum_decl = TYPE_NAME (TYPE_MAIN_VARIANT (datum_type));
> >>>> +    if (decl_in_std_namespace_p (datum_decl))
> >>>> +      {
> >>>> +        tree name = DECL_NAME (datum_decl);
> >>>> +        if (name && (id_equal (name, "reference_wrapper")))
> >>>> +          {
> >>>> +            /* Handle std::reference_wrapper.  */
> >>>> +            is_refwrap = true;
> >>>> +            datum_type = cp_build_reference_type (datum_type, false);
>
> Why do you change datum_type from std::reference_wrapper<...> to
> std::reference_wrapper<...>&?
>
> >>>> +          }
> >>>> +      }
> >>>> +
> >>>> +    datum = build_trait_object (datum_type);
> >>>> +
> >>>> +    /* If datum_type was not std::reference_wrapper, check if it has
> >>>> +       operator*() overload.  If datum_type was std::reference_wrapper,
> >>>> +       avoid dereferencing the datum twice.  */
> >>>> +    if (!is_refwrap)
> >>>> +      if (get_class_binding (datum_type, get_identifier ("operator*")))
> >>>
> >>> We probably should use lookup_member instead of get_class_binding since
> >>> IIUC the latter doesn't look into bases:
> >>>
> >>>    struct A { int m; };
> >>>    struct B { A& operator*(): };
> >>>    struct C : B { };
> >>>    static_assert(std::is_invocable_v<int A::*, C>);
> >>>
> >>> However, I notice that the specification of INVOKE
> >>> (https://eel.is/c++draft/func.require#lib:INVOKE) doesn't mention name
> >>> lookup at all so it strikes me as suspicious that we'd perform name
> >>> lookup here.
>
> Agreed.  It seems that whether or not to build_x_indirect_ref should
> depend instead on whether f is a pointer to a member of decltype(t1) (as
> well as is_refwrap).
>
> >>>  I think this would misbehave for:
> >>>
> >>>    struct A { };
> >>>    struct B : A { A& operator*() = delete; };
> >>>    static_assert(std::is_invocable_v<int A::*, B>);
> >>>
> >>>    struct C : private A { A& operator*(); };
> >>>    static_assert(std::is_invocable_v<int A::*, C>);
> >>
> >> Oops, this static_assert is missing a !
> >>
> >>>
> >>> ultimately because we end up choosing the dereference form of INVOKE,
> >>> but according to 1.1/1.4 we should choose the non-dereference form?
> >>>
> >>>> +        /* Handle operator*().  */
> >>>> +        datum = build_x_indirect_ref (UNKNOWN_LOCATION, datum,
> >>>> +                                      RO_UNARY_STAR, NULL_TREE,
> >>>> +                                      tf_none);
> >>>> +  }
> >>>> +      else if (POINTER_TYPE_P (datum_type))
> >>>> +  datum = build_trait_object (TREE_TYPE (datum_type));
> >>>> +      else
> >>>> +  datum = build_trait_object (datum_type);
> >>>> +    }
> >>>> +
> >>>> +  /* Build a function expression.  */
> >>>> +  tree fn;
> >>>> +  if (is_ptrdatamem)
> >>>> +    fn = build_m_component_ref (datum, build_trait_object (fn_type), tf_none);
> >>>
> >>> Maybe exit early for the is_ptrdatamem case here (and simplify the rest
> >>> of the function accordingly)?
> >>>
> >>>> +  else if (is_ptrmemfunc)
> >>>> +    fn = build_trait_object (TYPE_PTRMEMFUNC_FN_TYPE (fn_type));
>
> Why not use build_m_component_ref and build_offset_ref_call_from_tree
> like it would if you wrote (t1.*f)() directly?
>

Thank you so much for your review!  I will apply your suggestions.

> Jason
>
  

Patch

diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index 9fce36e12d1..29bf548d30a 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3754,6 +3754,12 @@  diagnose_trait_expr (tree expr, tree args)
     case CPTK_IS_FUNCTION:
       inform (loc, "  %qT is not a function", t1);
       break;
+    case CPTK_IS_INVOCABLE:
+      if (!t2)
+    inform (loc, "  %qT is not invocable", t1);
+      else
+    inform (loc, "  %qT is not invocable by %qE", t1, t2);
+      break;
     case CPTK_IS_LAYOUT_COMPATIBLE:
       inform (loc, "  %qT is not layout compatible with %qT", t1, t2);
       break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 05514a51c21..b8b7608c122 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -71,6 +71,7 @@  DEFTRAIT_EXPR (IS_EMPTY, "__is_empty", 1)
 DEFTRAIT_EXPR (IS_ENUM, "__is_enum", 1)
 DEFTRAIT_EXPR (IS_FINAL, "__is_final", 1)
 DEFTRAIT_EXPR (IS_FUNCTION, "__is_function", 1)
+DEFTRAIT_EXPR (IS_INVOCABLE, "__is_invocable", -1)
 DEFTRAIT_EXPR (IS_LAYOUT_COMPATIBLE, "__is_layout_compatible", 2)
 DEFTRAIT_EXPR (IS_LITERAL_TYPE, "__is_literal_type", 1)
 DEFTRAIT_EXPR (IS_MEMBER_FUNCTION_POINTER, "__is_member_function_pointer", 1)
diff --git a/gcc/cp/method.h b/gcc/cp/method.h
new file mode 100644
index 00000000000..1aec8ec5cfd
--- /dev/null
+++ b/gcc/cp/method.h
@@ -0,0 +1,28 @@ 
+/* Functions exported by method.cc.
+   Copyright (C) 2023 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3, or (at your option)
+any later version.
+
+GCC is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#ifndef GCC_CP_METHOD_H
+#define GCC_CP_METHOD_H 1
+
+#include "tree.h"
+
+/* In method.cc  */
+extern tree build_trait_object (tree type);
+
+#endif  /* GCC_CP_METHOD_H  */
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 7cccbae5287..cc2e400531a 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -45,6 +45,10 @@  along with GCC; see the file COPYING3.  If not see
 #include "gomp-constants.h"
 #include "predict.h"
 #include "memmodel.h"
+#include "method.h"
+
+#include "print-tree.h"
+#include "tree-pretty-print.h"
 
 /* There routines provide a modular interface to perform many parsing
    operations.  They may therefore be used during actual parsing, or
@@ -11714,6 +11718,133 @@  classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
   return saw_copy;
 }
 
+/* Return true if FN_TYPE is invocable with the given ARG_TYPES.  */
+
+static bool
+is_invocable_p (tree fn_type, tree arg_types)
+{
+  /* ARG_TYPES must be a TREE_VEC.  */
+  gcc_assert (TREE_CODE (arg_types) == TREE_VEC);
+
+  /* Access check is required to determine if the given is invocable.  */
+  deferring_access_check_sentinel acs (dk_no_deferred);
+
+  /* std::is_invocable is an unevaluated context.  */
+  cp_unevaluated cp_uneval_guard;
+
+  bool is_ptrdatamem;
+  bool is_ptrmemfunc;
+  if (TREE_CODE (fn_type) == REFERENCE_TYPE)
+    {
+      tree deref_fn_type = TREE_TYPE (fn_type);
+      is_ptrdatamem = TYPE_PTRDATAMEM_P (deref_fn_type);
+      is_ptrmemfunc = TYPE_PTRMEMFUNC_P (deref_fn_type);
+
+      /* Dereference fn_type if it is a pointer to member.  */
+      if (is_ptrdatamem || is_ptrmemfunc)
+	fn_type = deref_fn_type;
+    }
+  else
+    {
+      is_ptrdatamem = TYPE_PTRDATAMEM_P (fn_type);
+      is_ptrmemfunc = TYPE_PTRMEMFUNC_P (fn_type);
+    }
+
+  if (is_ptrdatamem && TREE_VEC_LENGTH (arg_types) != 1)
+    /* A pointer to data member with non-one argument is not invocable.  */
+    return false;
+
+  if (is_ptrmemfunc && TREE_VEC_LENGTH (arg_types) == 0)
+    /* A pointer to member function with no arguments is not invocable.  */
+    return false;
+
+  /* Construct an expression of a pointer to member.  */
+  tree datum;
+  if (is_ptrdatamem || is_ptrmemfunc)
+    {
+      tree datum_type = TREE_VEC_ELT (arg_types, 0);
+
+      /* Dereference datum.  */
+      if (CLASS_TYPE_P (datum_type))
+	{
+	  bool is_refwrap = false;
+
+	  tree datum_decl = TYPE_NAME (TYPE_MAIN_VARIANT (datum_type));
+	  if (decl_in_std_namespace_p (datum_decl))
+	    {
+	      tree name = DECL_NAME (datum_decl);
+	      if (name && (id_equal (name, "reference_wrapper")))
+		{
+		  /* Handle std::reference_wrapper.  */
+		  is_refwrap = true;
+		  datum_type = cp_build_reference_type (datum_type, false);
+		}
+	    }
+
+	  datum = build_trait_object (datum_type);
+
+	  /* If datum_type was not std::reference_wrapper, check if it has
+	     operator*() overload.  If datum_type was std::reference_wrapper,
+	     avoid dereferencing the datum twice.  */
+	  if (!is_refwrap)
+	    if (get_class_binding (datum_type, get_identifier ("operator*")))
+	      /* Handle operator*().  */
+	      datum = build_x_indirect_ref (UNKNOWN_LOCATION, datum,
+					    RO_UNARY_STAR, NULL_TREE,
+					    tf_none);
+	}
+      else if (POINTER_TYPE_P (datum_type))
+	datum = build_trait_object (TREE_TYPE (datum_type));
+      else
+	datum = build_trait_object (datum_type);
+    }
+
+  /* Build a function expression.  */
+  tree fn;
+  if (is_ptrdatamem)
+    fn = build_m_component_ref (datum, build_trait_object (fn_type), tf_none);
+  else if (is_ptrmemfunc)
+    fn = build_trait_object (TYPE_PTRMEMFUNC_FN_TYPE (fn_type));
+  else
+    fn = build_trait_object (fn_type);
+
+  /* Construct arguments to the function and an expression of a call.  */
+  if (!is_ptrdatamem)
+    {
+      releasing_vec args;
+
+      if (is_ptrmemfunc)
+	{
+	  /* A pointer to member function is internally converted to a pointer
+	     to function that takes a pointer to the dereferenced datum type
+	     as its first argument and original arguments afterward.  If the
+	     function is a const member function, the first argument also
+	     requires a const datum pointer and vice-versa.  */
+
+	  tree datum_type = TREE_TYPE (datum);
+	  if (TYPE_REF_P (datum_type))
+	    datum_type = TREE_TYPE (datum_type);
+
+	  datum = build_trait_object (build_pointer_type (datum_type));
+	  vec_safe_push (args, datum);
+	}
+
+      for (int i = is_ptrmemfunc ? 1 : 0; i < TREE_VEC_LENGTH (arg_types); ++i)
+	{
+	  tree arg_type = TREE_VEC_ELT (arg_types, i);
+	  tree arg = build_trait_object (arg_type);
+	  vec_safe_push (args, arg);
+	}
+
+      fn = finish_call_expr (fn, &args, false, false, tf_none);
+    }
+
+  if (error_operand_p (fn))
+    return false;
+
+  return true;
+}
+
 /* Return true if DERIVED is pointer interconvertible base of BASE.  */
 
 static bool
@@ -12181,6 +12312,9 @@  trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
     case CPTK_IS_FUNCTION:
       return type_code1 == FUNCTION_TYPE;
 
+    case CPTK_IS_INVOCABLE:
+      return is_invocable_p (type1, type2);
+
     case CPTK_IS_LAYOUT_COMPATIBLE:
       return layout_compatible_type_p (type1, type2);
 
@@ -12390,6 +12524,7 @@  finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
       break;
 
     case CPTK_IS_CONVERTIBLE:
+    case CPTK_IS_INVOCABLE:
     case CPTK_IS_NOTHROW_ASSIGNABLE:
     case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
     case CPTK_IS_NOTHROW_CONVERTIBLE:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index b1430e9bd8b..3a9bda1ee03 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -92,6 +92,9 @@ 
 #if !__has_builtin (__is_function)
 # error "__has_builtin (__is_function) failed"
 #endif
+#if !__has_builtin (__is_invocable)
+# error "__has_builtin (__is_invocable) failed"
+#endif
 #if !__has_builtin (__is_layout_compatible)
 # error "__has_builtin (__is_layout_compatible) failed"
 #endif
diff --git a/gcc/testsuite/g++.dg/ext/is_invocable1.C b/gcc/testsuite/g++.dg/ext/is_invocable1.C
new file mode 100644
index 00000000000..2fd3906b571
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_invocable1.C
@@ -0,0 +1,337 @@ 
+// { dg-do compile { target c++11 } }
+
+#define SA(X) static_assert((X),#X)
+
+using func_type_v0 = void(*)();
+
+SA(   __is_invocable( func_type_v0 ) );
+SA( ! __is_invocable( func_type_v0, int ) );
+
+using func_type_i0 = int(*)();
+
+SA(   __is_invocable( func_type_i0 ) );
+SA( ! __is_invocable( func_type_i0, int ) );
+
+using func_type_l0 = int&(*)();
+
+SA(   __is_invocable( func_type_l0 ) );
+SA( ! __is_invocable( func_type_l0(int) ) );
+
+using func_type_ii = int(*)(int);
+
+SA( ! __is_invocable( func_type_ii ) );
+SA(   __is_invocable( func_type_ii, int ) );
+
+using func_type_il = int(*)(int&);
+
+SA( ! __is_invocable( func_type_il ) );
+SA( ! __is_invocable( func_type_il, int ) );
+SA(   __is_invocable( func_type_il, int& ) );
+
+using func_type_ir = int(*)(int&&);
+
+SA( ! __is_invocable( func_type_ir ) );
+SA( ! __is_invocable( func_type_ir, int& ) );
+SA(   __is_invocable( func_type_ir, int ) );
+SA(   __is_invocable( func_type_ir, int&& ) );
+
+struct A { };
+
+using mem_type_i = int A::*;
+
+SA( ! __is_invocable( mem_type_i ) );
+SA( ! __is_invocable( mem_type_i, int ) );
+SA( ! __is_invocable( mem_type_i, int* ) );
+SA( ! __is_invocable( mem_type_i, int& ) );
+SA( ! __is_invocable( mem_type_i, int&& ) );
+SA(   __is_invocable( mem_type_i, A ) );
+SA(   __is_invocable( mem_type_i, A* ) );
+SA(   __is_invocable( mem_type_i, A& ) );
+SA(   __is_invocable( mem_type_i, A&& ) );
+SA(   __is_invocable( mem_type_i, const A& ) );
+SA( ! __is_invocable( mem_type_i, A&, int ) );
+
+using memfun_type_i = int (A::*)();
+
+SA( ! __is_invocable( memfun_type_i ) );
+SA( ! __is_invocable( memfun_type_i, int ) );
+SA( ! __is_invocable( memfun_type_i, int* ) );
+SA( ! __is_invocable( memfun_type_i, int& ) );
+SA( ! __is_invocable( memfun_type_i, int&& ) );
+SA(   __is_invocable( memfun_type_i, A ) );
+SA(   __is_invocable( memfun_type_i, A* ) );
+SA(   __is_invocable( memfun_type_i, A& ) );
+SA(   __is_invocable( memfun_type_i, A&& ) );
+SA( ! __is_invocable( memfun_type_i, const A& ) );
+SA( ! __is_invocable( memfun_type_i, A&, int ) );
+
+using memfun_type_ic = int (A::*)() const;
+
+SA( ! __is_invocable( memfun_type_ic ) );
+SA( ! __is_invocable( memfun_type_ic, int ) );
+SA( ! __is_invocable( memfun_type_ic, int& ) );
+SA(   __is_invocable( memfun_type_ic, A& ) );
+SA(   __is_invocable( memfun_type_ic, A* ) );
+SA( ! __is_invocable( memfun_type_ic, A&, int ) );
+SA( ! __is_invocable( memfun_type_ic, A*, int& ) );
+SA(   __is_invocable( memfun_type_ic, const A& ) );
+SA(   __is_invocable( memfun_type_ic, const A* ) );
+SA( ! __is_invocable( memfun_type_ic, const A&, int& ) );
+SA( ! __is_invocable( memfun_type_ic, const A*, int ) );
+
+using memfun_type_iic = int& (A::*)(int&) const;
+
+SA( ! __is_invocable( memfun_type_iic ) );
+SA( ! __is_invocable( memfun_type_iic, int ) );
+SA( ! __is_invocable( memfun_type_iic, int& ) );
+SA( ! __is_invocable( memfun_type_iic, A&, int ) );
+SA(   __is_invocable( memfun_type_iic, A&, int& ) );
+SA( ! __is_invocable( memfun_type_iic, A*, int ) );
+SA(   __is_invocable( memfun_type_iic, A*, int& ) );
+SA( ! __is_invocable( memfun_type_iic, const A&, int ) );
+SA( ! __is_invocable( memfun_type_iic, const A&, int&, int ) );
+SA(   __is_invocable( memfun_type_iic, const A&, int& ) );
+SA(   __is_invocable( memfun_type_iic, const A*, int& ) );
+
+struct B {
+  int& operator()();
+  long& operator()() const;
+  bool& operator()(int);
+private:
+  void operator()(int, int);
+};
+using CB = const B;
+
+SA(   __is_invocable( B ) );
+SA(   __is_invocable( B& ) );
+SA(   __is_invocable( B&& ) );
+SA( ! __is_invocable( B* ) );
+SA(   __is_invocable( CB ) );
+SA(   __is_invocable( CB& ) );
+SA( ! __is_invocable( CB* ) );
+
+SA(   __is_invocable( B, int ) );
+SA(   __is_invocable( B&, int ) );
+SA(   __is_invocable( B&&, int ) );
+SA( ! __is_invocable( B*, int ) );
+SA( ! __is_invocable( CB, int ) );
+SA( ! __is_invocable( CB&, int ) );
+SA( ! __is_invocable( CB*, int ) );
+
+SA( ! __is_invocable( B, int, int ) );
+SA( ! __is_invocable( B&, int, int ) );
+SA( ! __is_invocable( B&&, int, int ) );
+SA( ! __is_invocable( B*, int, int ) );
+SA( ! __is_invocable( CB, int, int ) );
+SA( ! __is_invocable( CB&, int, int ) );
+SA( ! __is_invocable( CB*, int, int ) );
+
+struct C : B { int& operator()() = delete; };
+using CC = const C;
+
+SA( ! __is_invocable( C ) );
+SA( ! __is_invocable( C& ) );
+SA( ! __is_invocable( C&& ) );
+SA( ! __is_invocable( C* ) );
+SA( ! __is_invocable( CC ) );
+SA( ! __is_invocable( CC& ) );
+SA( ! __is_invocable( CC* ) );
+
+struct D { B operator*(); };
+using CD = const D;
+
+SA( ! __is_invocable( D ) );
+
+struct E { void v(); };
+using CE = const E;
+
+SA( ! __is_invocable( E ) );
+SA( ! __is_invocable( void (E::*)() ) );
+SA(   __is_invocable( void (E::*)(), E ) );
+SA(   __is_invocable( void (E::*)(), E* ) );
+SA( ! __is_invocable( void (E::*)(), CE ) );
+
+struct F : E {};
+using CF = const F;
+
+SA( ! __is_invocable( F ) );
+SA(   __is_invocable( void (E::*)(), F ) );
+SA(   __is_invocable( void (E::*)(), F* ) );
+SA( ! __is_invocable( void (E::*)(), CF ) );
+
+struct G { E operator*(); };
+using CG = const G;
+
+SA( ! __is_invocable( G ) );
+SA(   __is_invocable( void (E::*)(), G ) );
+SA( ! __is_invocable( void (E::*)(), G* ) );
+SA( ! __is_invocable( void (E::*)(), CG ) );
+
+struct H { E& operator*(); };
+using CH = const H;
+
+SA( ! __is_invocable( H ) );
+SA(   __is_invocable( void (E::*)(), H ) );
+SA( ! __is_invocable( void (E::*)(), H* ) );
+SA( ! __is_invocable( void (E::*)(), CH ) );
+
+struct I { E&& operator*(); };
+using CI = const I;
+
+SA( ! __is_invocable( I ) );
+SA(   __is_invocable( void (E::*)(), I ) );
+SA( ! __is_invocable( void (E::*)(), I* ) );
+SA( ! __is_invocable( void (E::*)(), CI ) );
+
+struct K { E* operator*(); };
+using CK = const K;
+
+SA( ! __is_invocable( K ) );
+SA( ! __is_invocable( void (E::*)(), K ) );
+SA( ! __is_invocable( void (E::*)(), K* ) );
+SA( ! __is_invocable( void (E::*)(), CK ) );
+
+struct L { CE operator*(); };
+using CL = const L;
+
+SA( ! __is_invocable( L ) );
+SA( ! __is_invocable( void (E::*)(), L ) );
+SA( ! __is_invocable( void (E::*)(), L* ) );
+SA( ! __is_invocable( void (E::*)(), CL ) );
+
+struct M {
+  int i;
+private:
+  long l;
+};
+using CM = const M;
+
+SA( ! __is_invocable( M ) );
+SA( ! __is_invocable( M& ) );
+SA( ! __is_invocable( M&& ) );
+SA( ! __is_invocable( M* ) );
+SA( ! __is_invocable( CM ) );
+SA( ! __is_invocable( CM& ) );
+SA( ! __is_invocable( CM* ) );
+
+SA( ! __is_invocable( int M::* ) );
+SA(   __is_invocable( int M::*, M ) );
+SA(   __is_invocable( int M::*, M& ) );
+SA(   __is_invocable( int M::*, M&& ) );
+SA(   __is_invocable( int M::*, M* ) );
+SA(   __is_invocable( int M::*, CM ) );
+SA(   __is_invocable( int M::*, CM& ) );
+SA(   __is_invocable( int M::*, CM* ) );
+SA( ! __is_invocable( int M::*, int ) );
+
+SA( ! __is_invocable( int CM::* ) );
+SA(   __is_invocable( int CM::*, M ) );
+SA(   __is_invocable( int CM::*, M& ) );
+SA(   __is_invocable( int CM::*, M&& ) );
+SA(   __is_invocable( int CM::*, M* ) );
+SA(   __is_invocable( int CM::*, CM ) );
+SA(   __is_invocable( int CM::*, CM& ) );
+SA(   __is_invocable( int CM::*, CM* ) );
+SA( ! __is_invocable( int CM::*, int ) );
+
+SA( ! __is_invocable( long M::* ) );
+SA(   __is_invocable( long M::*, M ) );
+SA(   __is_invocable( long M::*, M& ) );
+SA(   __is_invocable( long M::*, M&& ) );
+SA(   __is_invocable( long M::*, M* ) );
+SA(   __is_invocable( long M::*, CM ) );
+SA(   __is_invocable( long M::*, CM& ) );
+SA(   __is_invocable( long M::*, CM* ) );
+SA( ! __is_invocable( long M::*, long ) );
+
+SA( ! __is_invocable( long CM::* ) );
+SA(   __is_invocable( long CM::*, M ) );
+SA(   __is_invocable( long CM::*, M& ) );
+SA(   __is_invocable( long CM::*, M&& ) );
+SA(   __is_invocable( long CM::*, M* ) );
+SA(   __is_invocable( long CM::*, CM ) );
+SA(   __is_invocable( long CM::*, CM& ) );
+SA(   __is_invocable( long CM::*, CM* ) );
+SA( ! __is_invocable( long CM::*, long ) );
+
+SA( ! __is_invocable( short M::* ) );
+SA(   __is_invocable( short M::*, M ) );
+SA(   __is_invocable( short M::*, M& ) );
+SA(   __is_invocable( short M::*, M&& ) );
+SA(   __is_invocable( short M::*, M* ) );
+SA(   __is_invocable( short M::*, CM ) );
+SA(   __is_invocable( short M::*, CM& ) );
+SA(   __is_invocable( short M::*, CM* ) );
+SA( ! __is_invocable( short M::*, short ) );
+
+SA( ! __is_invocable( short CM::* ) );
+SA(   __is_invocable( short CM::*, M ) );
+SA(   __is_invocable( short CM::*, M& ) );
+SA(   __is_invocable( short CM::*, M&& ) );
+SA(   __is_invocable( short CM::*, M* ) );
+SA(   __is_invocable( short CM::*, CM ) );
+SA(   __is_invocable( short CM::*, CM& ) );
+SA(   __is_invocable( short CM::*, CM* ) );
+SA( ! __is_invocable( short CM::*, short ) );
+
+struct N { M operator*(); };
+SA(   __is_invocable( int M::*, N ) );
+SA( ! __is_invocable( int M::*, N* ) );
+
+struct O { M& operator*(); };
+SA(   __is_invocable( int M::*, O ) );
+SA( ! __is_invocable( int M::*, O* ) );
+
+struct P { M&& operator*(); };
+SA(   __is_invocable( int M::*, P ) );
+SA( ! __is_invocable( int M::*, P* ) );
+
+struct Q { M* operator*(); };
+SA( ! __is_invocable( int M::*, Q ) );
+SA( ! __is_invocable( int M::*, Q* ) );
+
+struct R { void operator()(int = 0); };
+
+SA(   __is_invocable( R ) );
+SA(   __is_invocable( R, int ) );
+SA( ! __is_invocable( R, int, int ) );
+
+struct S { void operator()(int, ...); };
+
+SA( ! __is_invocable( S ) );
+SA(   __is_invocable( S, int ) );
+SA(   __is_invocable( S, int, int ) );
+SA(   __is_invocable( S, int, int, int ) );
+
+void fn1() {}
+
+SA(   __is_invocable( decltype(fn1) ) );
+
+void fn2(int arr[10]);
+
+SA(   __is_invocable( decltype(fn2), int[10] ) );
+SA(   __is_invocable( decltype(fn2), int(&)[10] ) );
+SA(   __is_invocable( decltype(fn2), int(&&)[10] ) );
+SA( ! __is_invocable( decltype(fn2), int(*)[10] ) );
+SA( ! __is_invocable( decltype(fn2), int(*&)[10] ) );
+SA( ! __is_invocable( decltype(fn2), int(*&&)[10] ) );
+SA(   __is_invocable( decltype(fn2), int[] ) );
+
+auto lambda = []() {};
+
+SA(   __is_invocable( decltype(lambda) ) );
+
+template <typename Func, typename... Args>
+struct can_invoke {
+    static constexpr bool value = __is_invocable( Func, Args... );
+};
+
+SA( can_invoke<decltype(lambda)>::value );
+
+struct T {
+  void func() const {}
+  int data;
+};
+
+SA(   __is_invocable( decltype(&T::func)&, T& ) );
+SA(   __is_invocable( decltype(&T::data)&, T& ) );
diff --git a/gcc/testsuite/g++.dg/ext/is_invocable2.C b/gcc/testsuite/g++.dg/ext/is_invocable2.C
new file mode 100644
index 00000000000..a68aefd3e13
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_invocable2.C
@@ -0,0 +1,139 @@ 
+// { dg-do compile { target c++11 } }
+// __is_invocable should handle std::reference_wrapper correctly.
+
+#include <functional>
+
+#define SA(X) static_assert((X),#X)
+
+using std::reference_wrapper;
+
+using func_type_v0 = void(*)();
+
+SA(   __is_invocable( reference_wrapper<func_type_v0> ) );
+SA( ! __is_invocable( reference_wrapper<func_type_v0>, int ) );
+
+using func_type_i0 = int(*)();
+
+SA(   __is_invocable( reference_wrapper<func_type_i0> ) );
+SA( ! __is_invocable( reference_wrapper<func_type_i0>, int ) );
+
+using func_type_l0 = int&(*)();
+
+SA(   __is_invocable( reference_wrapper<func_type_l0> ) );
+SA( ! __is_invocable( reference_wrapper<func_type_l0(int)> ) );
+
+using func_type_ii = int(*)(int);
+
+SA( ! __is_invocable( reference_wrapper<func_type_ii> ) );
+SA(   __is_invocable( reference_wrapper<func_type_ii>, int ) );
+
+using func_type_il = int(*)(int&);
+
+SA( ! __is_invocable( reference_wrapper<func_type_il> ) );
+SA( ! __is_invocable( reference_wrapper<func_type_il>, int ) );
+SA(   __is_invocable( reference_wrapper<func_type_il>, int& ) );
+
+using func_type_ir = int(*)(int&&);
+
+SA( ! __is_invocable( reference_wrapper<func_type_ir> ) );
+SA( ! __is_invocable( reference_wrapper<func_type_ir>, int& ) );
+SA(   __is_invocable( reference_wrapper<func_type_ir>, int ) );
+SA(   __is_invocable( reference_wrapper<func_type_ir>, int&& ) );
+
+struct A { };
+
+using mem_type_i = int A::*;
+
+SA( ! __is_invocable( reference_wrapper<mem_type_i> ) );
+SA( ! __is_invocable( reference_wrapper<mem_type_i>, int ) );
+SA( ! __is_invocable( reference_wrapper<mem_type_i>, int* ) );
+SA( ! __is_invocable( reference_wrapper<mem_type_i>, int& ) );
+SA( ! __is_invocable( reference_wrapper<mem_type_i>, int&& ) );
+SA(   __is_invocable( reference_wrapper<mem_type_i>, A ) );
+SA(   __is_invocable( reference_wrapper<mem_type_i>, A* ) );
+SA(   __is_invocable( reference_wrapper<mem_type_i>, A& ) );
+SA(   __is_invocable( reference_wrapper<mem_type_i>, A&& ) );
+
+using memfun_type_i = int (A::*)();
+
+SA( ! __is_invocable( reference_wrapper<memfun_type_i> ) );
+SA( ! __is_invocable( reference_wrapper<memfun_type_i>, int ) );
+SA( ! __is_invocable( reference_wrapper<memfun_type_i>, int* ) );
+SA( ! __is_invocable( reference_wrapper<memfun_type_i>, int& ) );
+SA( ! __is_invocable( reference_wrapper<memfun_type_i>, int&& ) );
+SA(   __is_invocable( reference_wrapper<memfun_type_i>, A ) );
+SA(   __is_invocable( reference_wrapper<memfun_type_i>, A* ) );
+SA(   __is_invocable( reference_wrapper<memfun_type_i>, A& ) );
+SA(   __is_invocable( reference_wrapper<memfun_type_i>, A&& ) );
+SA( ! __is_invocable( reference_wrapper<memfun_type_i>, const A& ) );
+SA( ! __is_invocable( reference_wrapper<memfun_type_i>, A&, int ) );
+
+using memfun_type_ic = int (A::*)() const;
+
+SA( ! __is_invocable( reference_wrapper<memfun_type_ic> ) );
+SA( ! __is_invocable( reference_wrapper<memfun_type_ic>, int ) );
+SA( ! __is_invocable( reference_wrapper<memfun_type_ic>, int& ) );
+SA(   __is_invocable( reference_wrapper<memfun_type_ic>, A& ) );
+SA(   __is_invocable( reference_wrapper<memfun_type_ic>, A* ) );
+SA( ! __is_invocable( reference_wrapper<memfun_type_ic>, A&, int ) );
+SA( ! __is_invocable( reference_wrapper<memfun_type_ic>, A*, int& ) );
+SA(   __is_invocable( reference_wrapper<memfun_type_ic>, const A& ) );
+SA(   __is_invocable( reference_wrapper<memfun_type_ic>, const A* ) );
+SA( ! __is_invocable( reference_wrapper<memfun_type_ic>, const A&, int& ) );
+SA( ! __is_invocable( reference_wrapper<memfun_type_ic>, const A*, int ) );
+
+using memfun_type_iic = int& (A::*)(int&) const;
+
+SA( ! __is_invocable( reference_wrapper<memfun_type_iic> ) );
+SA( ! __is_invocable( reference_wrapper<memfun_type_iic>, int ) );
+SA( ! __is_invocable( reference_wrapper<memfun_type_iic>, int& ) );
+SA( ! __is_invocable( reference_wrapper<memfun_type_iic>, A&, int ) );
+SA(   __is_invocable( reference_wrapper<memfun_type_iic>, A&, int& ) );
+SA( ! __is_invocable( reference_wrapper<memfun_type_iic>, A*, int ) );
+SA(   __is_invocable( reference_wrapper<memfun_type_iic>, A*, int& ) );
+SA( ! __is_invocable( reference_wrapper<memfun_type_iic>, const A&, int ) );
+SA( ! __is_invocable( reference_wrapper<memfun_type_iic>, const A&, int&, int ) );
+SA(   __is_invocable( reference_wrapper<memfun_type_iic>, const A&, int& ) );
+SA(   __is_invocable( reference_wrapper<memfun_type_iic>, const A*, int& ) );
+
+struct B {
+  int& operator()();
+  long& operator()() const;
+  bool& operator()(int);
+private:
+  void operator()(int, int);
+};
+using CB = const B;
+
+SA(   __is_invocable( reference_wrapper<B> ) );
+SA(   __is_invocable( reference_wrapper<B>& ) );
+SA(   __is_invocable( reference_wrapper<B>&& ) );
+SA(   __is_invocable( reference_wrapper<CB> ) );
+SA(   __is_invocable( reference_wrapper<CB>& ) );
+SA(   __is_invocable( reference_wrapper<B>, int ) );
+SA( ! __is_invocable( reference_wrapper<B>&, int, int ) );
+
+struct C : B { int& operator()() = delete; };
+using CC = const C;
+
+SA( ! __is_invocable( reference_wrapper<C> ) );
+SA( ! __is_invocable( reference_wrapper<C>& ) );
+SA( ! __is_invocable( reference_wrapper<C>&& ) );
+SA( ! __is_invocable( reference_wrapper<CC> ) );
+SA( ! __is_invocable( reference_wrapper<CC>& ) );
+
+struct D { B operator*(); };
+using CD = const D;
+
+SA( ! __is_invocable( reference_wrapper<D> ) );
+SA( ! __is_invocable( reference_wrapper<D>& ) );
+SA( ! __is_invocable( reference_wrapper<D>&& ) );
+SA( ! __is_invocable( reference_wrapper<D>* ) );
+SA( ! __is_invocable( reference_wrapper<D*> ) );
+SA( ! __is_invocable( reference_wrapper<D*>* ) );
+
+std::function<void()> fn = []() {};
+auto refwrap = std::ref(fn);
+
+SA(   __is_invocable( decltype(fn) ) );
+SA(   __is_invocable( decltype(refwrap) ) );
diff --git a/gcc/testsuite/g++.dg/ext/is_invocable3.C b/gcc/testsuite/g++.dg/ext/is_invocable3.C
new file mode 100644
index 00000000000..e2b0c5ef406
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_invocable3.C
@@ -0,0 +1,51 @@ 
+// { dg-do compile { target c++11 } }
+// __is_invocable should handle incomplete class correctly.
+
+#define SA(X) static_assert((X),#X)
+
+struct Incomplete;
+
+SA( ! __is_invocable( Incomplete ) ); // { dg-error "incomplete type" }
+SA( ! __is_invocable( Incomplete, int ) ); // { dg-error "incomplete type" }
+
+SA( ! __is_invocable( int, Incomplete, int ) ); // { dg-error "incomplete type" }
+SA( ! __is_invocable( int, Incomplete ) ); // { dg-error "incomplete type" }
+
+SA( ! __is_invocable( Incomplete, Incomplete() ) ); // { dg-error "incomplete type" }
+SA( ! __is_invocable( Incomplete, Incomplete(int), int ) ); // { dg-error "incomplete type" }
+SA( ! __is_invocable( Incomplete, Incomplete(int, int), int, int ) ); // { dg-error "incomplete type" }
+
+SA( ! __is_invocable( Incomplete, Incomplete(), int, int ) ); // { dg-error "incomplete type" }
+
+SA( ! __is_invocable( int(Incomplete), Incomplete ) ); // { dg-error "incomplete type" }
+SA( ! __is_invocable( int(int, Incomplete), int, Incomplete ) ); // { dg-error "incomplete type" }
+SA( ! __is_invocable( int(int, Incomplete), Incomplete, int ) ); // { dg-error "incomplete type" }
+
+SA(   __is_invocable( int(Incomplete&), Incomplete& ) ); // { dg-bogus "incomplete type" }
+SA(   __is_invocable( int(int, Incomplete&), int, Incomplete& ) ); // { dg-bogus "incomplete type" }
+
+SA(   __is_invocable( int(Incomplete&&), Incomplete&& ) ); // { dg-bogus "incomplete type" }
+SA(   __is_invocable( int(int, Incomplete&&), int, Incomplete&& ) ); // { dg-bogus "incomplete type" }
+
+SA(   __is_invocable( int(const Incomplete&&), const Incomplete&& ) ); // { dg-bogus "incomplete type" }
+SA(   __is_invocable( int(int, const Incomplete&&), int, const Incomplete&& ) ); // { dg-bogus "incomplete type" }
+
+SA(   __is_invocable( int(const Incomplete&), const Incomplete& ) ); // { dg-bogus "incomplete type" }
+SA(   __is_invocable( int(int, const Incomplete&), int, const Incomplete& ) ); // { dg-bogus "incomplete type" }
+
+SA(   __is_invocable( int(const Incomplete&), Incomplete& ) ); // { dg-bogus "incomplete type" }
+SA(   __is_invocable( int(int, const Incomplete&), int, Incomplete& ) ); // { dg-bogus "incomplete type" }
+
+SA(   __is_invocable( int Incomplete::*, const Incomplete& ) ); // { dg-bogus "incomplete type" }
+SA( ! __is_invocable( void (Incomplete::*)(long&), const Incomplete*, long& ) ); // { dg-bogus "incomplete type" }
+SA(   __is_invocable( void (Incomplete::*)(long&) const, Incomplete*, long& ) ); // { dg-bogus "incomplete type" }
+
+template <typename T>
+struct Holder { T t; };
+
+SA(   __is_invocable( int(Holder<Incomplete>&), Holder<Incomplete>& ) ); // { dg-bogus "incomplete type" }
+
+// Define Incomplete, which is now not incomplete.
+struct Incomplete { void operator()(); };
+
+SA( __is_invocable( Incomplete ) ); // { dg-bogus "incomplete type" }
diff --git a/gcc/testsuite/g++.dg/ext/is_invocable4.C b/gcc/testsuite/g++.dg/ext/is_invocable4.C
new file mode 100644
index 00000000000..d1efccf08f8
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_invocable4.C
@@ -0,0 +1,33 @@ 
+// { dg-do compile { target c++11 } }
+// Failed access check should be a substitution failure, not an error.
+
+#define SA(X) static_assert((X),#X)
+
+template<bool B>
+struct bool_constant { static constexpr bool value = B; };
+
+template<typename _Fn, typename... _ArgTypes>
+struct is_invocable
+: public bool_constant<__is_invocable(_Fn, _ArgTypes...)>
+{ };
+
+#if __cpp_variable_templates
+template<typename _Fn, typename... _ArgTypes>
+constexpr bool is_invocable_v = __is_invocable(_Fn, _ArgTypes...);
+#endif
+
+class Private
+{
+  void operator()() const
+  {
+    SA( ! is_invocable<Private>::value );
+#if __cpp_variable_templates
+    SA( ! is_invocable_v<Private> );
+#endif
+  }
+};
+
+SA( ! is_invocable<Private>::value );
+#if __cpp_variable_templates
+SA( ! is_invocable_v<Private> );
+#endif