<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- qscopeguard.qdoc --> <title>QScopeGuard Class | Qt Core 5.12.6</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.12</td><td ><a href="qtcore-index.html">Qt Core</a></td><td ><a href="qtcore-module.html">C++ Classes</a></td><td >QScopeGuard</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right"><a href="qtcore-index.html">Qt 5.12.6 Reference Documentation</a></td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#public-functions">Public Functions</a></li> <li class="level1"><a href="#related-non-members">Related Non-Members</a></li> <li class="level1"><a href="#details">Detailed Description</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">QScopeGuard Class</h1> <!-- $$$QScopeGuard-brief --> <p>Provides a scope guard for calling a function at the end of a scope. <a href="#details">More...</a></p> <!-- @@@QScopeGuard --> <div class="table"><table class="alignedsummary"> <tr><td class="memItemLeft rightAlign topAlign"> Header:</td><td class="memItemRight bottomAlign"> <span class="preprocessor">#include <QScopeGuard></span> </td></tr><tr><td class="memItemLeft rightAlign topAlign"> qmake:</td><td class="memItemRight bottomAlign"> QT += core</td></tr><tr><td class="memItemLeft rightAlign topAlign"> Since:</td><td class="memItemRight bottomAlign"> Qt 5.12</td></tr></table></div><ul> <li><a href="qscopeguard-members.html">List of all members, including inherited members</a></li> </ul> <a name="public-functions"></a> <h2 id="public-functions">Public Functions</h2> <div class="table"><table class="alignedsummary"> <tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qscopeguard.html#QScopeGuard">QScopeGuard</a></b>(QScopeGuard<F> &&<i>other</i>)</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qscopeguard.html#dtor.QScopeGuard">~QScopeGuard</a></b>()</td></tr> <tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qscopeguard.html#dismiss">dismiss</a></b>()</td></tr> </table></div> <a name="related-non-members"></a> <h2 id="related-non-members">Related Non-Members</h2> <div class="table"><table class="alignedsummary"> <tr><td class="memItemLeft rightAlign topAlign"> QScopeGuard<F> </td><td class="memItemRight bottomAlign"><b><a href="qscopeguard.html#qScopeGuardx">qScopeGuard</a></b>(F <i>f</i>)</td></tr> </table></div> <a name="details"></a> <!-- $$$QScopeGuard-description --> <div class="descr"> <h2 id="details">Detailed Description</h2> <p>Provides a scope guard for calling a function at the end of a scope.</p> </div> <!-- @@@QScopeGuard --> <div class="func"> <h2>Member Function Documentation</h2> <!-- $$$QScopeGuard[overload1]$$$QScopeGuardQScopeGuard<F>&& --> <h3 class="fn" id="QScopeGuard"><a name="QScopeGuard"></a>QScopeGuard::<span class="name">QScopeGuard</span>(<span class="type"><a href="qscopeguard.html#QScopeGuard">QScopeGuard</a></span><<span class="type">F</span>> &&<i>other</i>)</h3> <p>Default constructs an instance of QScopeGuard.</p><!-- @@@QScopeGuard --> <!-- $$$~QScopeGuard[overload1]$$$~QScopeGuard --> <h3 class="fn" id="dtor.QScopeGuard"><a name="dtor.QScopeGuard"></a>QScopeGuard::<span class="name">~QScopeGuard</span>()</h3> <p>Destroys the instance of QScopeGuard.</p><!-- @@@~QScopeGuard --> <!-- $$$dismiss[overload1]$$$dismiss --> <h3 class="fn" id="dismiss"><a name="dismiss"></a><span class="type">void</span> QScopeGuard::<span class="name">dismiss</span>()</h3> <p>Disarms the scope guard, so that the function <i>F</i> will not be called at the end of the scope.</p> <!-- @@@dismiss --> </div> <div class="relnonmem"> <h2>Related Non-Members</h2> <!-- $$$qScopeGuard[overload1]$$$qScopeGuardF --> <h3 class="fn" id="qScopeGuardx"><a name="qScopeGuardx"></a><span class="type"><a href="qscopeguard.html#QScopeGuard">QScopeGuard</a></span><<span class="type">F</span>> <span class="name">qScopeGuard</span>(<span class="type">F</span> <i>f</i>)</h3> <p>The qScopeGuard function can be used to call a function at the end of the scope.</p> <p><a href="qscopeguard.html">QScopeGuard</a><F> is a class which sole purpose is to run a function <i>F</i> in its destructor. This is useful for guaranteeing your cleanup code is executed, whether the function is exited normally, exited early by a return statement, or exited by an exception.</p> <p>If <i>F</i> is a lambda then you cannot instantiate the template directly, therefore the qScopeGuard() helper is provided and <a href="qscopeguard.html">QScopeGuard</a><F> is made a private implementation detail.</p> <p>Example usage is as follows:</p> <pre class="cpp"> <span class="type">void</span> myComplexCodeWithMultipleReturnPoints(<span class="type">int</span> v) { <span class="comment">// The lambda will be executed right before your function returns</span> <span class="keyword">auto</span> cleanup <span class="operator">=</span> qScopeGuard(<span class="operator">[</span><span class="operator">]</span> { code you want executed goes HERE; }); <span class="keyword">if</span> (v <span class="operator">=</span><span class="operator">=</span> <span class="operator">-</span><span class="number">1</span>) <span class="keyword">return</span>; <span class="type">int</span> v2 <span class="operator">=</span> code_that_might_throw_exceptions(); <span class="keyword">if</span> (v2 <span class="operator">=</span><span class="operator">=</span> <span class="operator">-</span><span class="number">1</span>) <span class="keyword">return</span>; (<span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>) } </pre> <p><b>Note: </b>Exceptions are not supported. The callable shouldn't throw when executed, copied or moved.</p><p><b>See also </b><a href="qscopedvaluerollback.html">QScopedValueRollback</a>.</p> <!-- @@@qScopeGuard --> </div> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2019 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br/> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br/> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. </p> </div> </body> </html>