std::exception_ptr is a C++11 nullable pointer-like type that enables capturing, storing, and rethrowing exceptions across scope boundaries. The post explains how it works using std::current_exception and std::rethrow_exception, then demonstrates its most practical use case: propagating errors out of destructors without violating noexcept. The pattern involves passing an exception_ptr by reference into an object; the destructor catches any internal failure and stores it in the pointer rather than throwing, letting the caller inspect and handle the error after the object is destroyed. An if (!err) guard prevents silently overwriting a pre-existing in-flight exception.
Table of contents
What is std::exception_ptr ?How this can help with destructorsConclusionConnect deeper806 Impressions