fork download
  1. // I know you're code isn't doing something like this,
  2. // but it shows another drawback of macros vs functions
  3. #include <iostream>
  4.  
  5. #define MACRO_ABS(p) ((p) < 0 ? -(p) : (p))
  6.  
  7. template <typename T>
  8. T function_abs(T p)
  9. {
  10. return p < 0 ? -p : p;
  11. }
  12.  
  13. int main()
  14. {
  15. int i = -100;
  16.  
  17. std::cout << "macro: " << MACRO_ABS(i++) << std::endl;
  18. std::cout << "i: " << i << std::endl;
  19.  
  20. i = -100;
  21.  
  22. std::cout << "function: " << function_abs(i++) << std::endl;
  23. std::cout << "i: " << i << std::endl;
  24. }
Success #stdin #stdout 0.02s 2724KB
stdin
Standard input is empty
stdout
macro: 99
i: -98
function: 100
i: -99