fork download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. class Numeric {
  5. public:
  6. bool isNumeric() {
  7. return true;
  8. }
  9. };
  10.  
  11. class String {
  12. };
  13.  
  14. template<class T>
  15. class Myclass {
  16. private:
  17. T temp;
  18. public:
  19. template<typename U = T, typename std::enable_if<std::is_same<U, Numeric>::value, std::size_t>::type = 0>
  20. void checkNumeric() {
  21. std::cout << "is numeric = " << temp.isNumeric() << std::endl;
  22. }
  23.  
  24. template<typename U = T, typename std::enable_if<!std::is_same<U, Numeric>::value, std::size_t>::type = 0>
  25. void checkNumeric() {
  26. std::cout << "is numeric = false" << std::endl;
  27. }
  28. };
  29.  
  30. int main() {
  31. Myclass<Numeric> a;
  32. a.checkNumeric();
  33. Myclass<String> b;
  34. b.checkNumeric();
  35. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
is numeric = 1
is numeric = false