C++ - an exception-safe way to assure a global cleanup function gets called
<p>Context:</p>

<p>Suppose an external library requires that its <code>globalCleanup()</code> function gets called in order to assure all its resources are cleaned up (it might allocate some global resources during any of its calls.) A client function <code>client()</code> does this before each of its many <code>return</code>s but of course this is not exception-safe and the code is repetitive.</p>

<p>Problem:</p>

<p>Is there an elegant way to assure this global function gets called on the <code>client()</code> exit? Defining a dummy class whose only purpose is to call a global function in the destructor (RAII style) is an option, but maybe there's something simpler? </p>

<p>TLDR: How to call a global cleanup function RAII-style?</p>
