Boost C++ Libraries: Ticket #1090: scoped_file class that deletes file at end of scope https://svn.boost.org/trac10/ticket/1090 <p> I've found myself wanting this on a couple of occasions with 1.33.1, and I've done it with boost::shared_ptr: </p> <div class="wiki-code"><div class="code"><pre><span class="n">boost</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">fs</span><span class="o">::</span><span class="n">path</span><span class="o">&gt;</span> <span class="n">removeFile</span><span class="p">(</span><span class="o">&amp;</span><span class="n">file</span><span class="p">,</span> <span class="n">bind</span><span class="p">(</span><span class="o">&amp;</span><span class="n">fs</span><span class="o">::</span><span class="n">remove</span><span class="p">,</span> <span class="n">boost</span><span class="o">::</span><span class="n">cref</span><span class="p">(</span><span class="n">file</span><span class="p">)));</span> </pre></div></div><p> That didn't work with 1.34.1 (maybe due to compiler intricacies), so I did it properly: </p> <div class="wiki-code"><div class="code"><pre><span class="k">class</span> <span class="nc">scoped_file</span> <span class="p">{</span> <span class="k">public</span><span class="o">:</span> <span class="k">explicit</span> <span class="n">scoped_file</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">file</span><span class="p">)</span> <span class="o">:</span> <span class="n">m_path</span><span class="p">(</span><span class="n">file</span><span class="p">,</span> <span class="n">boost</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">native</span><span class="p">)</span> <span class="p">{}</span> <span class="k">explicit</span> <span class="n">scoped_file</span><span class="p">(</span><span class="k">const</span> <span class="n">boost</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">path</span><span class="o">&amp;</span> <span class="n">file</span><span class="p">)</span> <span class="o">:</span> <span class="n">m_path</span><span class="p">(</span><span class="n">file</span><span class="p">)</span> <span class="p">{}</span> <span class="o">~</span><span class="n">scoped_file</span><span class="p">()</span> <span class="p">{</span> <span class="n">boost</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">remove</span><span class="p">(</span><span class="n">m_path</span><span class="p">);</span> <span class="p">}</span> <span class="k">private</span><span class="o">:</span> <span class="n">boost</span><span class="o">::</span><span class="n">filesystem</span><span class="o">::</span><span class="n">path</span> <span class="n">m_path</span><span class="p">;</span> <span class="p">};</span> </pre></div></div><p> That might be something to add? </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/1090 Trac 1.4.3 Marcus Lindblom <macke@…> Fri, 13 Jul 2007 12:36:24 GMT <link>https://svn.boost.org/trac10/ticket/1090#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/1090#comment:1</guid> <description> <p> Sorry! The shared_ptr impl should look like this: </p> <div class="wiki-code"><div class="code"><pre><span class="n">boost</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">fs</span><span class="o">::</span><span class="n">path</span><span class="o">&gt;</span> <span class="n">removeFile</span><span class="p">(</span><span class="o">&amp;</span><span class="n">file</span><span class="p">,</span> <span class="n">bind</span><span class="p">(</span><span class="o">&amp;</span><span class="n">fs</span><span class="o">::</span><span class="n">remove</span><span class="p">,</span> <span class="n">boost</span><span class="o">::</span><span class="n">cref</span><span class="p">(</span><span class="n">file</span><span class="p">)));</span> </pre></div></div> </description> <category>Ticket</category> </item> <item> <dc:creator>Marshall Clow</dc:creator> <pubDate>Mon, 13 Aug 2007 15:21:09 GMT</pubDate> <title>owner set https://svn.boost.org/trac10/ticket/1090#comment:2 https://svn.boost.org/trac10/ticket/1090#comment:2 <ul> <li><strong>owner</strong> set to <span class="trac-author">Beman Dawes</span> </li> </ul> Ticket Beman Dawes Sun, 29 Jun 2008 21:34:47 GMT type changed https://svn.boost.org/trac10/ticket/1090#comment:3 https://svn.boost.org/trac10/ticket/1090#comment:3 <ul> <li><strong>type</strong> <span class="trac-field-old">Patches</span> → <span class="trac-field-new">Feature Requests</span> </li> </ul> <p> I'm considering <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/590" title="#590: Feature Requests: mkstemp (closed: fixed)">#590</a> and <a class="new ticket" href="https://svn.boost.org/trac10/ticket/1090" title="#1090: Feature Requests: scoped_file class that deletes file at end of scope (new)">#1090</a> as indications of interest and need, but feel that there are various details to be worked out first. mkstemp and similar functions seem pretty dated. The C++ approach suggested by <a class="new ticket" href="https://svn.boost.org/trac10/ticket/1090" title="#1090: Feature Requests: scoped_file class that deletes file at end of scope (new)">#1090</a> is appealing, but more work needs to be done. Suggestions welcome. </p> <p> --Beman </p> Ticket Marcus Lindblom <macke@…> Mon, 30 Jun 2008 10:01:47 GMT <link>https://svn.boost.org/trac10/ticket/1090#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/1090#comment:4</guid> <description> <p> What is lacking in the current proposal? </p> <p> (Also, the c++ markup in the description seems to have failed. Could you edit that?) </p> </description> <category>Ticket</category> </item> </channel> </rss>