fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <utility>
  4. #include <type_traits>
  5.  
  6.  
  7. template<typename...>
  8. struct void_type {
  9.  
  10. using type = void;
  11. };
  12.  
  13. template<typename ...T>
  14. using void_t = typename void_type<T...>::type;
  15.  
  16.  
  17. template<typename T, typename = void>
  18. struct is_invokable_with_member_function_foo : std::false_type {};
  19.  
  20. template<typename T, typename ...Args>
  21. struct is_invokable_with_member_function_foo<
  22. T (Args...)
  23. , void_t<decltype(std::declval<T>().foo(std::declval<Args>()...))>
  24. > : std::true_type {};
  25.  
  26.  
  27. struct A {};
  28.  
  29. struct B {
  30.  
  31. void foo();
  32. void foo(int, double);
  33. };
  34.  
  35. struct C {
  36. void foo(int);
  37. void foo(int, double) const;
  38. };
  39.  
  40.  
  41. int main() {
  42. std::cout << std::boolalpha;
  43. std::cout << "A::foo() -> "
  44. << is_invokable_with_member_function_foo<A ()>()
  45. << std::endl;
  46. std::cout << "B::foo() -> "
  47. << is_invokable_with_member_function_foo<B ()>()
  48. << std::endl;
  49. std::cout << "B::foo(int) -> "
  50. << is_invokable_with_member_function_foo<B (int)>()
  51. << std::endl;
  52. std::cout << "B::foo(int, double) -> "
  53. << is_invokable_with_member_function_foo<B (int, double)>()
  54. << std::endl;
  55. std::cout << "B::foo(char, int) -> "
  56. << is_invokable_with_member_function_foo<B (char, int)>()
  57. << std::endl;
  58. std::cout << "B::foo(int, double) const -> "
  59. << is_invokable_with_member_function_foo<B const (int, double)>()
  60. << std::endl;
  61. std::cout << "C::foo(int) -> "
  62. << is_invokable_with_member_function_foo<C (int)>()
  63. << std::endl;
  64. std::cout << "C::foo(int, double) -> "
  65. << is_invokable_with_member_function_foo<C (int, double)>()
  66. << std::endl;
  67. std::cout << "C::foo(int, double) const -> "
  68. << is_invokable_with_member_function_foo<C const (int, double)>()
  69. << std::endl;
  70. }
  71.  
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
A::foo() -> false
B::foo() -> true
B::foo(int) -> false
B::foo(int, double) -> true
B::foo(char, int) -> true
B::foo(int, double) const -> false
C::foo(int) -> true
C::foo(int, double) -> true
C::foo(int, double) const -> true