Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // C++11 cleanup example
- #include <vector>
- #include <iostream>
- #include <functional>
- #include <fcntl.h>
- #include <unistd.h>
- using namespace std;
- void out(const vector<function<void()>> &cleanups)
- {
- for (auto f : cleanups)
- f();
- }
- int main()
- {
- vector<function<void()>> cleanups;
- char *s[1000];
- for (int i = 0; i < 1000; ++i) {
- s[i] = new char[1000];
- cleanups.push_back([=](){delete [] s[i];});
- }
- int fd = open("/etc/passwd", O_RDONLY);
- cleanups.push_back([=](){close(fd);});
- // here, do some work
- out(cleanups);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement