fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template <int N>
  5. void f();
  6.  
  7. template <>
  8. void f<0>()
  9. {
  10. printf("f<0>");
  11. }
  12.  
  13. template <>
  14. void f<1>()
  15. {
  16. printf("f<1>");
  17. }
  18.  
  19. void call_f(int i)
  20. {
  21. switch(i)
  22. {
  23. case 0:
  24. f<0>();
  25. break;
  26. case 1:
  27. f<1>();
  28. break;
  29. default:
  30. // invalid i, report error
  31. break;
  32. }
  33. }
  34.  
  35. int main() {
  36. f<0>();
  37. f<1>();
  38. // f<2>(); // compile error
  39.  
  40. call_f(0);
  41. call_f(1);
  42. call_f(2); // runtime error
  43. return 0;
  44. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
f<0>f<1>f<0>f<1>