fork download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. // 2. the second template argument is only valid if T is an integral type:
  5. template < class T,
  6. class = typename std::enable_if<std::is_integral<T>::value>::type>
  7. bool is_even (T i) {return !bool(i%2);}
  8.  
  9. int main() {
  10. short int i = 1; // code does not compile if type of i is not integral
  11. std::cout << std::boolalpha;
  12. std::cout << "i is even: " << is_even(i) << std::endl;
  13.  
  14. return 0;
  15. }
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
i is even: false