fork(1) download
  1. #include <stdio.h>
  2. #include <type_traits>
  3.  
  4. void print()
  5. {
  6. printf("cheers from print !\n");
  7. }
  8.  
  9. class A
  10. {
  11. public:
  12. void print()
  13. {
  14. printf("cheers from A !\n");
  15. }
  16. };
  17.  
  18. template<typename Function>
  19. void run(Function& f, std::true_type tag)
  20. {
  21. f();
  22. }
  23.  
  24. template<typename Object>
  25. void run(Object& o, std::false_type tag)
  26. {
  27. o.print();
  28. }
  29.  
  30.  
  31. template<typename T>
  32. void run(T& t)
  33. {
  34. constexpr bool is_fun = std::is_function<typename std::remove_pointer<T>::type >::value;
  35. run(t, std::integral_constant<bool, is_fun>());
  36. }
  37.  
  38. int main()
  39. {
  40. run(print);
  41.  
  42. A a;
  43. run(a);
  44.  
  45. return 0;
  46. }
  47.  
Success #stdin #stdout 0s 4360KB
stdin
Standard input is empty
stdout
cheers from print !
cheers from A !