fork download
  1. #include <iostream>
  2. #include <type_traits>
  3. #include <utility>
  4. #include <functional>
  5. #include <iostream>
  6. #include <string>
  7. #include <typeinfo>
  8.  
  9. template< class... > using void_t = void;
  10.  
  11. namespace impl
  12. {
  13.  
  14. template <typename...>
  15. struct callable_args
  16. {
  17. };
  18.  
  19. template <class F, class Args, class = void>
  20. struct callable : std::false_type
  21. {
  22. };
  23.  
  24. template <class F, class... Args>
  25. struct callable<F, callable_args<Args...>, void_t<std::result_of_t<F(Args...)>>> : std::true_type
  26. {
  27. };
  28.  
  29. }
  30.  
  31. template <class F, class... Args>
  32. struct callable : impl::callable<F, impl::callable_args<Args...>>
  33. {
  34. };
  35.  
  36. template <class F, class... Args>
  37. constexpr auto callable_v = callable<F, Args...>::value;
  38.  
  39.  
  40. int main()
  41. {
  42. {
  43. using Func = std::function<void()>;
  44. auto result = callable_v<Func>;
  45. std::cout << "test 1 (should be 1) = " << result << std::endl;
  46. }
  47.  
  48. {
  49. using Func = std::function<void(int)>;
  50. auto result = callable_v<Func, int>;
  51. std::cout << "test 2 (should be 1) = " << result << std::endl;
  52. }
  53.  
  54. {
  55. using Func = std::function<int(int)>;
  56. auto result = callable_v<Func, int>;
  57. std::cout << "test 3 (should be 1) = " << result << std::endl;
  58. }
  59.  
  60. {
  61. using Func = std::function<int(double)>;
  62. auto result = callable_v<Func, std::string>;
  63. std::cout << "test 4 (should be 0) = " << result << std::endl;
  64. }
  65.  
  66. {
  67. using Func = std::function<int(double, int)>;
  68. auto result = callable_v<Func, double>;
  69. std::cout << "test 5 (should be 0) = " << result << std::endl;
  70. }
  71.  
  72. std::getchar();
  73.  
  74. return EXIT_SUCCESS;
  75. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
test 1 (should be 1) = 1
test 2 (should be 1) = 1
test 3 (should be 1) = 1
test 4 (should be 0) = 0
test 5 (should be 0) = 0