Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 0.45 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Managing object lifetime with lambdas in C  
  2. #include<functional>
  3. std::function<int()> Count() {
  4.     int x = 0;
  5.     return [=]() mutable { return x++; };
  6. }
  7.  
  8. std::function<int()> Negate(std::function<int()> x) {
  9.     return [=]() { return -x(); };
  10. }
  11.  
  12. int main() {
  13.     std::function<int()> n;
  14.     int z = 0;
  15.     if (true) {
  16.         auto c = Count();
  17.         z = c();
  18.         n = Negate(c);
  19.         z = n();
  20.         z = n();
  21.     }
  22.     z = n();
  23.     z = n();
  24. }