<div dir="ltr">RAII, of course! thanks!<div>btw, what about A3 above?</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Dec 4, 2013 at 5:14 PM, Jonathan M Davis <span dir="ltr"><<a href="mailto:jmdavisProg@gmx.com" target="_blank">jmdavisProg@gmx.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">On Wednesday, December 04, 2013 17:07:03 Timothee Cour wrote:<br>
> A1.<br>
><br>
> Is there a (clever?) way to achieve the following using a single function<br>
> call?<br>
><br>
> //does chdir<br>
><br>
> void fun(){<br>
> ...<br>
> string dir0=getcwd; scope(exit) chdir(dir0); chdir(dir);<br>
> ...<br>
> }<br>
><br>
> //desired:<br>
> void fun(){<br>
> ...<br>
> chdir_scoped(dir);<br>
> ...<br>
> }<br>
<br>
</div>Sounds like a job for RAII. e.g.<br>
<br>
{<br>
    auto tcd = TempCD(dir);<br>
    // do stuff<br>
} //tcd leaves scope and its destructor resets the cwd to what it was<br>
<br>
In general, I'd say that scope statements are for situations where you<br>
don't have to do the same thing in very many places, whereas RAII is better<br>
when you have to do the exact same thing in several places.<br>
<span class="HOEnZb"><font color="#888888"><br>
- Jonathan M Davis<br>
</font></span></blockquote></div><br></div>