- /*
- * JAVA "finally"-style exception-safe gargabe collection
- * for C++11
- *
- * cxx -std=c++11 lambda_finally_test1.cpp
- * Written by Roland Mainz <roland.mainz@nrubsig.org>
- */
- #include <functional>
- #include <stdlib.h>
- #include <stdio.h>
- class finally
- {
- std::function<void(void)> f;
- public:
- finally(const std::function<void(void)> &f) : f(f)
- {
- }
- ~finally()
- {
- f();
- }
- };
- int foo(int ac)
- {
- int arg = ac;
- finally xdone([&]{ printf("#done\n"); });
- puts("#start.");
- {
- finally x1([&]{ puts("four"); });
- puts("#one.");
- {
- finally x2([&]{ printf("three %d\n", arg); });
- puts("#two.");
- arg = 7;
- throw "blabla_exception";
- }
- }
- puts("#err_not_reachable.");
- return EXIT_FAILURE;
- }
- int main(int ac, char *av[])
- {
- int retcode = EXIT_SUCCESS;
- try
- {
- retcode = foo(ac);
- }
- catch(...)
- {
- }
- return retcode;
- }
C++ "finally", like JAVA finally
Posted by Anonymous on Thu 6th Dec 2018 13:28
raw | new post
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.