fork download
  1. #include <iostream>
  2.  
  3. template<typename T, typename U>
  4. auto helper(T a, U b, int) -> decltype (a/b) {
  5. std::cout << "first" << std::endl;
  6. return a/b;
  7. }
  8.  
  9. template<typename T, typename U>
  10. auto helper(T a, U b, ...) -> decltype (a * (U(1)/b)) {
  11. std::cout << "second" << std::endl;
  12. return a * (U(1)/b);
  13. }
  14.  
  15. template<typename T, typename U>
  16. auto smart_division(T a, U b) -> decltype (helper(a, b)) {
  17. return helper(a, b, 0);
  18. }
  19.  
  20.  
  21. struct Test {
  22. explicit Test(int) {}
  23. };
  24. int operator / (Test a, Test b) {
  25. return 1;
  26. }
  27.  
  28. int main() {
  29. std::cout << smart_division(1.0, 2.0) << std::endl;
  30. Test t{5};
  31. std::cout << smart_division(1, t) << std::endl;
  32. return 0;
  33. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
first
0.5
second
1