fork download
  1. template <typename T, typename U>
  2. struct is_same {
  3. static const bool value = false;
  4. };
  5. template <typename T>
  6. struct is_same<T, T> {
  7. static const bool value = true;
  8. };
  9.  
  10. template <bool, typename>
  11. struct enable_if {
  12. };
  13. template <typename T>
  14. struct enable_if<true, T> {
  15. typedef T type;
  16. };
  17.  
  18. template <typename OutType, typename InType>
  19. typename enable_if<is_same<OutType, InType>::value, OutType>::type foo(const InType& x)
  20. {
  21. // OutType == InType
  22. return x;
  23. }
  24. template <typename OutType, typename InType>
  25. typename enable_if<!is_same<OutType, InType>::value, OutType>::type foo(const InType& x)
  26. {
  27. // OutType != InType
  28. return x + x;
  29. }
  30.  
  31. #include <iostream>
  32. int main()
  33. {
  34. std::cout << foo<int>(1) << '\n'
  35. << foo<long>(1) << '\n';
  36. }
  37.  
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
1
2