fork(1) download
  1. #include <iostream>
  2. #include <type_traits>
  3. using namespace std;
  4.  
  5. namespace detail
  6. {
  7. template <typename T>
  8. auto has_to_string_helper (...) -> false_type;
  9.  
  10. template <typename T>
  11. auto has_to_string_helper (int) -> decltype(std::to_string(std::declval<T>()), true_type{});
  12. }
  13.  
  14. template <typename T>
  15. using has_to_string = decltype(detail::has_to_string_helper<T>(0));
  16.  
  17. template<typename T>
  18. enable_if_t<has_to_string<T>::value, void> stringify(T t){
  19. cout << "has\n";
  20. }
  21. template<typename T>
  22. enable_if_t<!has_to_string<T>::value, void> stringify(T t){
  23. cout << "has not\n";
  24. }
  25.  
  26. int main() {
  27. stringify(0);
  28. stringify("");
  29. return 0;
  30. }
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
has
has not