fork download
  1. #include <iostream>
  2.  
  3. struct T1
  4. { };
  5.  
  6. struct T2
  7. { };
  8.  
  9. template<typename T>
  10. struct has_print
  11. { typedef void no_t; };
  12.  
  13. template<>
  14. struct has_print<void>
  15. { typedef void yes_t; };
  16.  
  17. template<typename T>
  18. char print_type(T t);
  19.  
  20. void print_type(T1 t)
  21. { std::cout << "T1\n"; }
  22.  
  23. template<typename T>
  24. auto print(T t) -> typename has_print<decltype(print_type(t))>::yes_t
  25. {
  26. print_type(t);
  27. }
  28.  
  29. template<typename T>
  30. auto print(T t) -> typename has_print<decltype(print_type(t))>::no_t
  31. {
  32. std::cout << "can't print this\n";
  33. }
  34.  
  35. int main()
  36. {
  37. print(T1());
  38. print(T2());
  39. }
  40.  
Success #stdin #stdout 0s 2884KB
stdin
Standard input is empty
stdout
T1
can't print this