fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. using opType = void(*)(int&, int);
  5. const opType ops[3] =
  6. {
  7. [](int& , int ){ cout << "noop" << endl; },
  8. [](int& x, int i){ cout << x << " += " << i << endl; x += i; },
  9. [](int& x, int i){ cout << x << " -= " << i << endl; x -= i; }
  10. };
  11.  
  12. void doIt(int k, int &x, int i)
  13. {
  14. auto f = ops[int(k > 0) + (2 * int(k < 0))];
  15. f(x, i);
  16. }
  17.  
  18. int main()
  19. {
  20. int x = 0;
  21. cout << "x = " << x << endl;
  22. doIt(0, x, 1);
  23. doIt(1, x, 5);
  24. doIt(-1, x, 3);
  25. cout << "x = " << x << endl;
  26. return 0;
  27. }
Success #stdin #stdout 0s 4404KB
stdin
Standard input is empty
stdout
x = 0
noop
0 += 5
5 -= 3
x = 2