fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. template <int I> struct choice : choice<I+1> {
  6. void func() { std::cout << "choice<" << I << "> - "; }
  7. };
  8. template <> struct choice<10> {
  9. void func() { std::cout << "choice<10>, yo! - "; }
  10. };
  11.  
  12. // arithmetic version
  13. template <class T>
  14. auto convertToStringHelper(T const& t, choice<0> c)
  15. -> decltype(std::to_string(t))
  16. {
  17. c.func();
  18. return std::to_string(t) + '\n';
  19. }
  20.  
  21. // non-arithmetic version
  22. template <class T>
  23. auto convertToStringHelper(T const& t, choice<1> c)
  24. -> decltype(std::string(t))
  25. {
  26. c.func();
  27. return std::string(t) + '\n';
  28. }
  29.  
  30. // vector version
  31. template <class T, class A>
  32. std::string convertToStringHelper(std::vector<T,A> const& v, choice<2> c)
  33. {
  34. c.func();
  35. // implementation here
  36. return "";
  37. }
  38.  
  39. template <class T>
  40. std::string convertToString(T const& t) {
  41. return convertToStringHelper(t, choice<0>{});
  42. }
  43.  
  44. int main() {
  45. std::cout << convertToString('A');
  46. std::cout << convertToString("A");
  47. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
choice<0> - 65
choice<1> - A