SHARE
TWEET

Untitled

a guest Apr 23rd, 2015 466 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // C++11 cleanup example
  2. #include <vector>
  3. #include <iostream>
  4. #include <functional>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7.  
  8. using namespace std;
  9.  
  10. void out(const vector<function<void()>> &cleanups)
  11. {
  12.         for (auto f : cleanups)
  13.                 f();
  14. }
  15.  
  16. int main()
  17. {
  18.         vector<function<void()>> cleanups;
  19.  
  20.         char *s[1000];
  21.  
  22.         for (int i = 0; i < 1000; ++i) {
  23.                 s[i] = new char[1000];
  24.                 cleanups.push_back([=](){delete [] s[i];});
  25.         }
  26.         int fd = open("/etc/passwd", O_RDONLY);
  27.         cleanups.push_back([=](){close(fd);});
  28.  
  29.         // here, do some work
  30.  
  31.         out(cleanups);
  32.  
  33.         return 0;
  34. }
RAW Paste Data
Top