fork download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. using namespace std;
  5.  
  6. struct sample {
  7. int a;
  8.  
  9. std::function<int()> get_simple(int o) {
  10. return [=,this]() {
  11. return a + o;
  12. };
  13. }
  14. };
  15.  
  16. int main() {
  17. sample s;
  18. auto f = s.get_simple(5);
  19. s.a = 10;
  20. cout << f() << endl;
  21. }
  22.  
Success #stdin #stdout 0s 2984KB
stdin
Standard input is empty
stdout
15