fork(1) download
  1. #include <type_traits>
  2. #include <iostream>
  3.  
  4. // Operators are valid.
  5. template<int I>
  6. std::enable_if_t<(I > 0) && (I < 5) && !(I % 2), void>
  7. func() {
  8. std::cout << "Template parameter is small, positive, and even: " << I << '\n';
  9. }
  10.  
  11. template<int I>
  12. std::enable_if_t<I && (I < 5) && !((I > 0) && !(I % 2)), void>
  13. func() { std::cout << "I'm Batman.\n"; }
  14.  
  15. template<int I>
  16. std::enable_if_t<I >= 5 && I != 7, void>
  17. func() { std::cout << "Flyin' high\n"; }
  18.  
  19. // We can do this, too.
  20. template<int I>
  21. std::enable_if_t<!I, void>
  22. func() { std::cout << "Silence will fall.\n"; }
  23.  
  24. template<int I>
  25. std::enable_if_t<I == 7, void>
  26. func() { std::cout << "...And we're done.\n"; }
  27.  
  28. template<int I>
  29. struct Caller : public Caller<I - 1> {
  30. Caller() { func<I>(); }
  31. };
  32.  
  33. template<>
  34. struct Caller<0> {
  35. Caller() { func<0>(); }
  36. };
  37.  
  38. int main() {
  39. Caller<(7 || 0) + 6>{}; // Syntactic salt for Caller<7>{}.
  40. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
Silence will fall.
I'm Batman.
Template parameter is small, positive, and even: 2
I'm Batman.
Template parameter is small, positive, and even: 4
Flyin' high
Flyin' high
...And we're done.