It sounds to me what you&#x27;re trying to do is essentially what a Scope Guard is for. Here&#x27;s a wiki on them: <a href="https:&#x2F;&#x2F;en.wikibooks.org&#x2F;wiki&#x2F;More_C%2B%2B_Idioms&#x2F;Scope_Guard" rel="nofollow">https:&#x2F;&#x2F;en.wikibooks.org&#x2F;wiki&#x2F;More_C%2B%2B_Idioms&#x2F;Scope_Guar...</a> And here&#x27;s a nice and clean implementation of one I&#x27;ve used recently: <a href="http:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;28413370" rel="nofollow">http:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;28413370</a>