Tutorial by Examples

Bad locking: std::mutex mtx; void bad_lock_example() { mtx.lock(); try { foo(); bar(); if (baz()) { mtx.unlock(); // Have to unlock on each exit point. return; } quux(); mtx.unlock(); // Normal...
For cases when we don't want to write special classes to handle some resource, we may write a generic class: template<typename Function> class Finally final { public: explicit Finally(Function f) : f(std::move(f)) {} ~Finally() { f(); } // (1) See below Finally(const Final...
C++17 Thanks to int std::uncaught_exceptions(), we can implement action which executes only on success (no thrown exception in scope). Previously bool std::uncaught_exception() just allows to detect if any stack unwinding is running. #include <exception> #include <iostream> templa...
C++17 Thanks to int std::uncaught_exceptions(), we can implement action which executes only on failure (thrown exception in scope). Previously bool std::uncaught_exception() just allows to detect if any stack unwinding is running. #include <exception> #include <iostream> template ...

Page 1 of 1