fork(1) download
  1. #include <iostream>
  2. #include <functional>
  3. using namespace std;
  4.  
  5. std::function<int(int)> makeAdd(int x) {
  6. return [x](int i) { return x + i; };
  7. }
  8.  
  9. void use(std::function<int(int)> f) {
  10. int result = f(888);
  11. cout << "f(888) = " << result;
  12. }
  13.  
  14. int main() {
  15. int i = 7; // or from cin
  16. std::function<int(int)> add = makeAdd(i);
  17. use(add);
  18. return 0;
  19. }
Success #stdin #stdout 0s 4368KB
stdin
Standard input is empty
stdout
f(888) = 895