<?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: Smart developers use smart pointers (3/7) &#8211; Custom deleters	</title>
	<atom:link href="https://www.fluentcpp.com/2017/08/29/custom-deleters/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.fluentcpp.com/2017/08/29/custom-deleters/</link>
	<description>Jonathan Boccara&#039;s blog</description>
	<lastBuildDate>Tue, 18 Dec 2018 04:44:56 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.3</generator>
	<item>
		<title>
		By: Jesse Silverman		</title>
		<link>https://www.fluentcpp.com/2017/08/29/custom-deleters/#comment-1187</link>

		<dc:creator><![CDATA[Jesse Silverman]]></dc:creator>
		<pubDate>Mon, 30 Jul 2018 19:48:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1085#comment-1187</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2017/08/29/custom-deleters/#comment-512&quot;&gt;Jonathan Boccara&lt;/a&gt;.

From lots of experience in C, I can say that either always or never owning something are the two good options.  I may come back to this again later, but for now it seems we are trying to come up with elegant ways to do something that was always very hazardous in a slightly less dangerous way.

I felt like the biggest problem with having raw pointers everywhere was how easy it was to get confused about ownership.  It may be a bit Draconian to always own something, but the inherent situation of &quot;I&#039;ve got a pointer to something, now let&#039;s see, do I own this or not?&quot; really can complicates an otherwise trivial interface.

Interesting ways of dealing with it tho -- I&#039;d been completely ignoring custom deleters until now.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2017/08/29/custom-deleters/#comment-512">Jonathan Boccara</a>.</p>
<p>From lots of experience in C, I can say that either always or never owning something are the two good options.  I may come back to this again later, but for now it seems we are trying to come up with elegant ways to do something that was always very hazardous in a slightly less dangerous way.</p>
<p>I felt like the biggest problem with having raw pointers everywhere was how easy it was to get confused about ownership.  It may be a bit Draconian to always own something, but the inherent situation of &#8220;I&#8217;ve got a pointer to something, now let&#8217;s see, do I own this or not?&#8221; really can complicates an otherwise trivial interface.</p>
<p>Interesting ways of dealing with it tho &#8212; I&#8217;d been completely ignoring custom deleters until now.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2017/08/29/custom-deleters/#comment-531</link>

		<dc:creator><![CDATA[Jonathan Boccara]]></dc:creator>
		<pubDate>Thu, 07 Sep 2017 23:23:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1085#comment-531</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2017/08/29/custom-deleters/#comment-529&quot;&gt;Victor Shulyak&lt;/a&gt;.

Oh so the House class takes on the responsibility of choosing the right deleter. This is an interesting idea, hadn&#039;t thought about it. This means that the caller is relieved from choosing the right deleter, but it should avoid passing a reference to an heap-allocated object to the second constructor then, right?]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2017/08/29/custom-deleters/#comment-529">Victor Shulyak</a>.</p>
<p>Oh so the House class takes on the responsibility of choosing the right deleter. This is an interesting idea, hadn&#8217;t thought about it. This means that the caller is relieved from choosing the right deleter, but it should avoid passing a reference to an heap-allocated object to the second constructor then, right?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Victor Shulyak		</title>
		<link>https://www.fluentcpp.com/2017/08/29/custom-deleters/#comment-529</link>

		<dc:creator><![CDATA[Victor Shulyak]]></dc:creator>
		<pubDate>Thu, 07 Sep 2017 09:12:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1085#comment-529</guid>

					<description><![CDATA[What do you think about creating special constructor for stacked instructions:
// heap allocated instructions, does deleting
    explicit House(std::unique_ptr instructions)
// from stack, no need to delete
    explicit House(Instructions&#038; instructions)]]></description>
			<content:encoded><![CDATA[<p>What do you think about creating special constructor for stacked instructions:<br />
// heap allocated instructions, does deleting<br />
    explicit House(std::unique_ptr instructions)<br />
// from stack, no need to delete<br />
    explicit House(Instructions&amp; instructions)</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2017/08/29/custom-deleters/#comment-520</link>

		<dc:creator><![CDATA[Jonathan Boccara]]></dc:creator>
		<pubDate>Wed, 30 Aug 2017 16:42:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1085#comment-520</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2017/08/29/custom-deleters/#comment-519&quot;&gt;Bart&lt;/a&gt;.

Right. I&#039;ve added your remarks to the post, really grateful for your contributions!]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2017/08/29/custom-deleters/#comment-519">Bart</a>.</p>
<p>Right. I&#8217;ve added your remarks to the post, really grateful for your contributions!</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Bart		</title>
		<link>https://www.fluentcpp.com/2017/08/29/custom-deleters/#comment-519</link>

		<dc:creator><![CDATA[Bart]]></dc:creator>
		<pubDate>Wed, 30 Aug 2017 14:19:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1085#comment-519</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2017/08/29/custom-deleters/#comment-517&quot;&gt;Jonathan Boccara&lt;/a&gt;.

Yes absolutely. That would fix it. One note though: unique_ptr&#039;s don&#039;t have copy constructors, so you&#039;d need to explicitly move there.

With regard to your comment: sometimes you *have* to use raw pointers in constructors, like when using custom deleters, in all other cases you should prefer make_unique.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2017/08/29/custom-deleters/#comment-517">Jonathan Boccara</a>.</p>
<p>Yes absolutely. That would fix it. One note though: unique_ptr&#8217;s don&#8217;t have copy constructors, so you&#8217;d need to explicitly move there.</p>
<p>With regard to your comment: sometimes you *have* to use raw pointers in constructors, like when using custom deleters, in all other cases you should prefer make_unique.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2017/08/29/custom-deleters/#comment-517</link>

		<dc:creator><![CDATA[Jonathan Boccara]]></dc:creator>
		<pubDate>Wed, 30 Aug 2017 12:26:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1085#comment-517</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2017/08/29/custom-deleters/#comment-513&quot;&gt;Bart&lt;/a&gt;.

Right, it coud throw and this is an important point.
Isn&#039;t the problem that
&lt;pre&gt;&lt;code&gt;InstructionsUniquePtr(new Sketch, deleteInstructions)&lt;/code&gt;&lt;/pre&gt;
is in the same expression as the call to constructor of House, rather than the new being in the call to constructor of the unique_ptr?
So the fix would be:
&lt;pre&gt;&lt;code&gt;
InstructionsUniquePtr instructions(new Sketch, deleteInstructions);
return House(instructions, getHouseNumber());
&lt;/code&gt;&lt;/pre&gt;
and no need to take the new out of the construction of the unique_ptr, what do you think?]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2017/08/29/custom-deleters/#comment-513">Bart</a>.</p>
<p>Right, it coud throw and this is an important point.<br />
Isn&#8217;t the problem that</p>
<pre><code>InstructionsUniquePtr(new Sketch, deleteInstructions)</code></pre>
<p>is in the same expression as the call to constructor of House, rather than the new being in the call to constructor of the unique_ptr?<br />
So the fix would be:</p>
<pre><code>
InstructionsUniquePtr instructions(new Sketch, deleteInstructions);
return House(instructions, getHouseNumber());
</code></pre>
<p>and no need to take the new out of the construction of the unique_ptr, what do you think?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Sergio Adán		</title>
		<link>https://www.fluentcpp.com/2017/08/29/custom-deleters/#comment-514</link>

		<dc:creator><![CDATA[Sergio Adán]]></dc:creator>
		<pubDate>Wed, 30 Aug 2017 06:27:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1085#comment-514</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2017/08/29/custom-deleters/#comment-511&quot;&gt;Jonathan Boccara&lt;/a&gt;.

Yes, of course you can talk about it.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2017/08/29/custom-deleters/#comment-511">Jonathan Boccara</a>.</p>
<p>Yes, of course you can talk about it.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Bart		</title>
		<link>https://www.fluentcpp.com/2017/08/29/custom-deleters/#comment-513</link>

		<dc:creator><![CDATA[Bart]]></dc:creator>
		<pubDate>Wed, 30 Aug 2017 00:34:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1085#comment-513</guid>

					<description><![CDATA[You failed to mention that using unique_ptr&#039;s constructor with a raw pointer can be dangerous.

Say for example the House constructor takes another argument, say, the house number.


    return House(InstructionsUniquePtr(new Sketch, deleteInstructions), getHouseNumer());


Now, it is *possible* that `new Sketch` is executed, then getHouseNumber(), which may throw. Then the unique pointer is not created, and you have a memory leak.

When using a custom deleter, there&#039;s no way arround this issue, but it should be mentioned nonetheless. More on this in Effective Modern C++ and function call evaltuation order on http://en.cppreference.com/w/cpp/language/operator_other#Built-in_function_call_operator.]]></description>
			<content:encoded><![CDATA[<p>You failed to mention that using unique_ptr&#8217;s constructor with a raw pointer can be dangerous.</p>
<p>Say for example the House constructor takes another argument, say, the house number.</p>
<p>    return House(InstructionsUniquePtr(new Sketch, deleteInstructions), getHouseNumer());</p>
<p>Now, it is *possible* that `new Sketch` is executed, then getHouseNumber(), which may throw. Then the unique pointer is not created, and you have a memory leak.</p>
<p>When using a custom deleter, there&#8217;s no way arround this issue, but it should be mentioned nonetheless. More on this in Effective Modern C++ and function call evaltuation order on <a href="http://en.cppreference.com/w/cpp/language/operator_other#Built-in_function_call_operator" rel="nofollow ugc">http://en.cppreference.com/w/cpp/language/operator_other#Built-in_function_call_operator</a>.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2017/08/29/custom-deleters/#comment-512</link>

		<dc:creator><![CDATA[Jonathan Boccara]]></dc:creator>
		<pubDate>Tue, 29 Aug 2017 21:43:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1085#comment-512</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2017/08/29/custom-deleters/#comment-510&quot;&gt;Daniel Houck&lt;/a&gt;.

Okay I see, that makes sense.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2017/08/29/custom-deleters/#comment-510">Daniel Houck</a>.</p>
<p>Okay I see, that makes sense.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2017/08/29/custom-deleters/#comment-511</link>

		<dc:creator><![CDATA[Jonathan Boccara]]></dc:creator>
		<pubDate>Tue, 29 Aug 2017 21:42:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1085#comment-511</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2017/08/29/custom-deleters/#comment-509&quot;&gt;Sergio Adán&lt;/a&gt;.

Yes I see. Anyway I find your idea great, and I will talk about it in Friday&#039;s post, if you don&#039;t mind.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2017/08/29/custom-deleters/#comment-509">Sergio Adán</a>.</p>
<p>Yes I see. Anyway I find your idea great, and I will talk about it in Friday&#8217;s post, if you don&#8217;t mind.</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
