fork download
  1. #include <iostream>
  2. #include <functional>
  3. using namespace std;
  4.  
  5. int main() {
  6. std::function<int()> functions_to_call[5];
  7.  
  8. int i=0;
  9.  
  10. for(i=0; i<5; ++i)
  11. functions_to_call[i] = [&i](){ return i; };
  12.  
  13. std::cout << "f(i) with i:" << std::endl;
  14. for(i=0; i<5; ++i)
  15. std::cout << functions_to_call[i]() << std::endl;
  16.  
  17. std::cout << "f(i) with j:" << std::endl;
  18. for(int j=0; j<5; ++j)
  19. std::cout << functions_to_call[j]() << std::endl;
  20. return 0;
  21. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
f(i) with i:
0
1
2
3
4
f(i) with j:
5
5
5
5
5