<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	
	>
<channel>
	<title>
	Comments on: To RAII or Not to RAII?	</title>
	<atom:link href="https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/</link>
	<description>Jonathan Boccara&#039;s blog</description>
	<lastBuildDate>Fri, 08 Jun 2018 22:44:00 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.3</generator>
	<item>
		<title>
		By: na2th		</title>
		<link>https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/#comment-1053</link>

		<dc:creator><![CDATA[na2th]]></dc:creator>
		<pubDate>Fri, 08 Jun 2018 22:44:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=2379#comment-1053</guid>

					<description><![CDATA[I would say Context Manager is a better name for using a class destructor to ensure that something happens at the end of the scope, just like what python does.
But of course, the name we give it is minor compared to the idea itself. Good article.]]></description>
			<content:encoded><![CDATA[<p>I would say Context Manager is a better name for using a class destructor to ensure that something happens at the end of the scope, just like what python does.<br />
But of course, the name we give it is minor compared to the idea itself. Good article.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: David Tallman		</title>
		<link>https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/#comment-827</link>

		<dc:creator><![CDATA[David Tallman]]></dc:creator>
		<pubDate>Thu, 15 Feb 2018 23:46:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=2379#comment-827</guid>

					<description><![CDATA[Like all destructors, the ones used for RAII purposes should be set up so that no exceptions escape. Otherwise, destructors called because of exceptions in flight could terminate the program. The database operations in Persistor seem likely to throw, so they should be wrapped in a try/catch.]]></description>
			<content:encoded><![CDATA[<p>Like all destructors, the ones used for RAII purposes should be set up so that no exceptions escape. Otherwise, destructors called because of exceptions in flight could terminate the program. The database operations in Persistor seem likely to throw, so they should be wrapped in a try/catch.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Daryn		</title>
		<link>https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/#comment-826</link>

		<dc:creator><![CDATA[Daryn]]></dc:creator>
		<pubDate>Thu, 15 Feb 2018 06:59:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=2379#comment-826</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/#comment-825&quot;&gt;Andrew Hoffmeyer&lt;/a&gt;.

&lt;blockquote&gt;
I have to disagree to a certain extent. To describe it as by-the-book RAII may be technically incorrect...
&lt;/blockquote&gt;

Surely it&#039;s a pretty important point since the article is titled &quot;To RAII or Not to RAII?&quot;, and the example didn&#039;t &quot;RA&quot; when doing &quot;I&quot;.

&lt;blockquote&gt;
It would be pointless to open the connection when the object is created, do a bunch of other processing unrelated to the Persistor instance, then finally save the state and close when the scope exits.
&lt;/blockquote&gt;

That&#039;s RAII. You can disagree with it and say why. I think RAII advocates would have good responses, but that&#039;s not my point. My point is clarity about the debate, and I&#039;m arguing that what you can&#039;t do is call something &quot;RAII&quot; when it&#039;s definitely not.

Yes, RAII makes use of destructors to ensure that resources are cleaned up. That guranteed cleanup is what makes it great. But &lt;em&gt;that only makes sense if you&#039;re acquiring resources at some earlier time&lt;/em&gt;. If you acquire a resource, then something throws an exception, the dtor guarantees you cleanup the resource. Awesome sauce.

On the other hand, the original example given shows three things happening in the dtor itself: 1) acquire resource, 2) use it, 3) release the resource.
There is simply zero reason to put this in a dtor. No reason at all. Stick those 3 steps in a function somewhere. It&#039;s not RAII. It&#039;s not cleaning anything up. It doesn&#039;t survive exceptions being thrown. This example is just using scope exit as a technique to call a function—the dtor—... but I can&#039;t think of any reason why you would want to do that.

In fact, putting it in dtor makes it worse than just having those 3 steps in a function:
Acquiring resources can fail.
Actions using those resources can fail.

Doing the acquiring and actions in a destructor would mean there is &lt;strong&gt;no way to handle failure&lt;/strong&gt;, or even know about it. The calling code would have to... keep going on the assumption that the call succeeded??

Or worse, if the code you call to acquire resources or do actions throws an exception, and you&#039;re doing those things in a destructor, then you just crashed your program or put it into undefined territory.

So in conclusion: even if you don&#039;t buy RAII&#039;s goal of only acquiring resources in &lt;em&gt;constructors&lt;/em&gt; (that&#039;s what they mean by &quot;Initialization&quot;), you&#039;ve got to represent the meaning of RAII properly when debating it. Saying that C++ lets you use the scope to run destructors is fine, but that&#039;s not RAII. And specifically, acquiring new resources inside a destructor is like the opposite of RAII.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/#comment-825">Andrew Hoffmeyer</a>.</p>
<blockquote><p>
I have to disagree to a certain extent. To describe it as by-the-book RAII may be technically incorrect&#8230;
</p></blockquote>
<p>Surely it&#8217;s a pretty important point since the article is titled &#8220;To RAII or Not to RAII?&#8221;, and the example didn&#8217;t &#8220;RA&#8221; when doing &#8220;I&#8221;.</p>
<blockquote><p>
It would be pointless to open the connection when the object is created, do a bunch of other processing unrelated to the Persistor instance, then finally save the state and close when the scope exits.
</p></blockquote>
<p>That&#8217;s RAII. You can disagree with it and say why. I think RAII advocates would have good responses, but that&#8217;s not my point. My point is clarity about the debate, and I&#8217;m arguing that what you can&#8217;t do is call something &#8220;RAII&#8221; when it&#8217;s definitely not.</p>
<p>Yes, RAII makes use of destructors to ensure that resources are cleaned up. That guranteed cleanup is what makes it great. But <em>that only makes sense if you&#8217;re acquiring resources at some earlier time</em>. If you acquire a resource, then something throws an exception, the dtor guarantees you cleanup the resource. Awesome sauce.</p>
<p>On the other hand, the original example given shows three things happening in the dtor itself: 1) acquire resource, 2) use it, 3) release the resource.<br />
There is simply zero reason to put this in a dtor. No reason at all. Stick those 3 steps in a function somewhere. It&#8217;s not RAII. It&#8217;s not cleaning anything up. It doesn&#8217;t survive exceptions being thrown. This example is just using scope exit as a technique to call a function—the dtor—&#8230; but I can&#8217;t think of any reason why you would want to do that.</p>
<p>In fact, putting it in dtor makes it worse than just having those 3 steps in a function:<br />
Acquiring resources can fail.<br />
Actions using those resources can fail.</p>
<p>Doing the acquiring and actions in a destructor would mean there is <strong>no way to handle failure</strong>, or even know about it. The calling code would have to&#8230; keep going on the assumption that the call succeeded??</p>
<p>Or worse, if the code you call to acquire resources or do actions throws an exception, and you&#8217;re doing those things in a destructor, then you just crashed your program or put it into undefined territory.</p>
<p>So in conclusion: even if you don&#8217;t buy RAII&#8217;s goal of only acquiring resources in <em>constructors</em> (that&#8217;s what they mean by &#8220;Initialization&#8221;), you&#8217;ve got to represent the meaning of RAII properly when debating it. Saying that C++ lets you use the scope to run destructors is fine, but that&#8217;s not RAII. And specifically, acquiring new resources inside a destructor is like the opposite of RAII.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Andrew Hoffmeyer		</title>
		<link>https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/#comment-825</link>

		<dc:creator><![CDATA[Andrew Hoffmeyer]]></dc:creator>
		<pubDate>Wed, 14 Feb 2018 20:43:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=2379#comment-825</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/#comment-820&quot;&gt;Daryn&lt;/a&gt;.

I have to disagree to a certain extent.  To describe it as by-the-book RAII may be technically incorrect, but in the example given, it makes no sense to open the database connection in the constructor, when the only time it is used is in the destructor.  A common best practice is to declare variables as close to their first use as possible.  The Persistor class, conceptually speaking, isn&#039;t meant to manage resources, but rather to save its state on destruction.  In the first version of Persistor that is shown, the database connection is completely unneeded in every part of the class except the destructor, where all the work is done.  It would be pointless to open the connection when the object is created, do a bunch of other processing unrelated to the Persistor instance, then finally save the state and close when the scope exits.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/#comment-820">Daryn</a>.</p>
<p>I have to disagree to a certain extent.  To describe it as by-the-book RAII may be technically incorrect, but in the example given, it makes no sense to open the database connection in the constructor, when the only time it is used is in the destructor.  A common best practice is to declare variables as close to their first use as possible.  The Persistor class, conceptually speaking, isn&#8217;t meant to manage resources, but rather to save its state on destruction.  In the first version of Persistor that is shown, the database connection is completely unneeded in every part of the class except the destructor, where all the work is done.  It would be pointless to open the connection when the object is created, do a bunch of other processing unrelated to the Persistor instance, then finally save the state and close when the scope exits.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Andrew Hoffmeyer		</title>
		<link>https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/#comment-824</link>

		<dc:creator><![CDATA[Andrew Hoffmeyer]]></dc:creator>
		<pubDate>Wed, 14 Feb 2018 20:34:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=2379#comment-824</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/#comment-819&quot;&gt;Andrew Haining ☭<img src="https://s.w.org/images/core/emoji/15.0.3/72x72/2638.png" alt="☸" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/15.0.3/72x72/262e.png" alt="☮" class="wp-smiley" style="height: 1em; max-height: 1em;" />&lt;/a&gt;.

I think maybe you&#039;re too hung up on the &quot;resource acquisition&quot; part of the terminology of RAII.  The techniques that RAII, by definition, allow, are useful in other areas that have nothing to do with acquiring and releasing resources, and rightly get lumped into discussions of RAII.  RAII has become more of a concept, rather than a hard rule that only applies to finite resources.  I don&#039;t think it&#039;s wrong to conceptually express scope-exit-deferred operations in terms of RAII, because they are so closely related to the concept of releasing actual resources.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/#comment-819">Andrew Haining ☭☸️☮️</a>.</p>
<p>I think maybe you&#8217;re too hung up on the &#8220;resource acquisition&#8221; part of the terminology of RAII.  The techniques that RAII, by definition, allow, are useful in other areas that have nothing to do with acquiring and releasing resources, and rightly get lumped into discussions of RAII.  RAII has become more of a concept, rather than a hard rule that only applies to finite resources.  I don&#8217;t think it&#8217;s wrong to conceptually express scope-exit-deferred operations in terms of RAII, because they are so closely related to the concept of releasing actual resources.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Raoul Sabido Gonzalez		</title>
		<link>https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/#comment-823</link>

		<dc:creator><![CDATA[Raoul Sabido Gonzalez]]></dc:creator>
		<pubDate>Wed, 14 Feb 2018 20:31:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=2379#comment-823</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/#comment-810&quot;&gt;Kai Stuhlemmer&lt;/a&gt;.

Unfortunately some analysis tools aren&#039;t as smart as the compiler and will &quot;warn&quot; about such unused variables.

However, one way to ignore the problem completely is to consider the context class as a wrapping facade for the logger itself and log things through the context.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/#comment-810">Kai Stuhlemmer</a>.</p>
<p>Unfortunately some analysis tools aren&#8217;t as smart as the compiler and will &#8220;warn&#8221; about such unused variables.</p>
<p>However, one way to ignore the problem completely is to consider the context class as a wrapping facade for the logger itself and log things through the context.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/#comment-822</link>

		<dc:creator><![CDATA[Jonathan Boccara]]></dc:creator>
		<pubDate>Wed, 14 Feb 2018 17:41:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=2379#comment-822</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/#comment-820&quot;&gt;Daryn&lt;/a&gt;.

Hi Daryn, I see, thanks for taking the time to expose this insight. I&#039;ve update the article in that sense and you&#039;ve helped me better understand RAII. Thanks for providing your feedback.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/#comment-820">Daryn</a>.</p>
<p>Hi Daryn, I see, thanks for taking the time to expose this insight. I&#8217;ve update the article in that sense and you&#8217;ve helped me better understand RAII. Thanks for providing your feedback.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Mickey_disqus		</title>
		<link>https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/#comment-821</link>

		<dc:creator><![CDATA[Mickey_disqus]]></dc:creator>
		<pubDate>Wed, 14 Feb 2018 11:50:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=2379#comment-821</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/#comment-809&quot;&gt;Andrew Haining ☭<img src="https://s.w.org/images/core/emoji/15.0.3/72x72/2638.png" alt="☸" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/15.0.3/72x72/262e.png" alt="☮" class="wp-smiley" style="height: 1em; max-height: 1em;" />&lt;/a&gt;.

People have adapted a technique developed for one use case, to a much wider range of use cases, and now the name doesn&#039;t perfectly fit anymore because the new use cases aren&#039;t necessarily about tidying up a finite resource.



Is that the crux of your argument? Do you want us to come up with a new name for the uses of the technique that are not about tidying up a finite resource?]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/#comment-809">Andrew Haining ☭☸️☮️</a>.</p>
<p>People have adapted a technique developed for one use case, to a much wider range of use cases, and now the name doesn&#8217;t perfectly fit anymore because the new use cases aren&#8217;t necessarily about tidying up a finite resource.</p>
<p>Is that the crux of your argument? Do you want us to come up with a new name for the uses of the technique that are not about tidying up a finite resource?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Daryn		</title>
		<link>https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/#comment-820</link>

		<dc:creator><![CDATA[Daryn]]></dc:creator>
		<pubDate>Wed, 14 Feb 2018 10:28:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=2379#comment-820</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/#comment-819&quot;&gt;Andrew Haining ☭<img src="https://s.w.org/images/core/emoji/15.0.3/72x72/2638.png" alt="☸" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/15.0.3/72x72/262e.png" alt="☮" class="wp-smiley" style="height: 1em; max-height: 1em;" />&lt;/a&gt;.

I&#039;m afraid I must agree. The same misunderstanding of RAII is evident in the Persistor example. You (addressing Jonathan Boccara, blog author) defined a Persistor class that is actually not RAII at all. As a result, your conclusion that &quot;RAII is not adapted&quot; (and that adding a save function would be better but &quot;no longer use RAII&quot;) is quite wrong.

In your design, the constructor does nothing, and the destructor does:
      // open database connection
      // save data_ in database
      // close database connection 

This is &lt;em&gt;completely&lt;/em&gt; contrary to RAII. Every single definition of RAII agrees that resource acquisition should occur in the *constructor*. 100% &lt;strong&gt;never&lt;/strong&gt; in a destructor.

In reality, the Persistor class is actually a great candidate for RAII design. The database connection is the resource. Acquire it in the constructor, use it as needed (save function), and close it in the destructor—guaranteed to run even if exception is thrown during save or other code in the calling scope.
&lt;pre&gt;&lt;code class=&quot;C++&quot;&gt;
class Persistor
{
public:
  explicit Persistor(Data const&#038; data)
  {
    connection_ = ...; /* open database connection */
  }
  ~Persistor()
  {
    /* close database connection */
  }
  save(Data data)
  {
    /* save data in database */
  }
private:
  DatabaseConnection connection_;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;em&gt;I wouldn&#039;t normally speak with such emphasis—don&#039;t want to be mean—but since this article got linked from &lt;a href=&quot;https://isocpp.org/blog/2018/02/to-raii-or-not-to-raii-jonathan-boccara&quot; rel=&quot;nofollow&quot;&gt;the Standard C++ blog&lt;/a&gt;, I felt more strongly about it. I&#039;d suggest you update the article or even do the blog equivalent of a &quot;retraction&quot; at the top.&lt;/em&gt;]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/#comment-819">Andrew Haining ☭☸️☮️</a>.</p>
<p>I&#8217;m afraid I must agree. The same misunderstanding of RAII is evident in the Persistor example. You (addressing Jonathan Boccara, blog author) defined a Persistor class that is actually not RAII at all. As a result, your conclusion that &#8220;RAII is not adapted&#8221; (and that adding a save function would be better but &#8220;no longer use RAII&#8221;) is quite wrong.</p>
<p>In your design, the constructor does nothing, and the destructor does:<br />
      // open database connection<br />
      // save data_ in database<br />
      // close database connection </p>
<p>This is <em>completely</em> contrary to RAII. Every single definition of RAII agrees that resource acquisition should occur in the *constructor*. 100% <strong>never</strong> in a destructor.</p>
<p>In reality, the Persistor class is actually a great candidate for RAII design. The database connection is the resource. Acquire it in the constructor, use it as needed (save function), and close it in the destructor—guaranteed to run even if exception is thrown during save or other code in the calling scope.</p>
<pre><code class="C++">
class Persistor
{
public:
  explicit Persistor(Data const&amp; data)
  {
    connection_ = ...; /* open database connection */
  }
  ~Persistor()
  {
    /* close database connection */
  }
  save(Data data)
  {
    /* save data in database */
  }
private:
  DatabaseConnection connection_;
};
</code></pre>
<p><em>I wouldn&#8217;t normally speak with such emphasis—don&#8217;t want to be mean—but since this article got linked from <a href="https://isocpp.org/blog/2018/02/to-raii-or-not-to-raii-jonathan-boccara" rel="nofollow">the Standard C++ blog</a>, I felt more strongly about it. I&#8217;d suggest you update the article or even do the blog equivalent of a &#8220;retraction&#8221; at the top.</em></p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Andrew Haining ☭☸️☮️		</title>
		<link>https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/#comment-819</link>

		<dc:creator><![CDATA[Andrew Haining ☭☸️☮️]]></dc:creator>
		<pubDate>Wed, 14 Feb 2018 06:32:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=2379#comment-819</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/#comment-812&quot;&gt;Andrew Hoffmeyer&lt;/a&gt;.

I&#039;m not against doing things in the destructor but doing things in the destructor is not RAII. Not knowing what RAII is is dangerous.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/02/13/to-raii-or-not-to-raii/#comment-812">Andrew Hoffmeyer</a>.</p>
<p>I&#8217;m not against doing things in the destructor but doing things in the destructor is not RAII. Not knowing what RAII is is dangerous.</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
