<?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: How to Construct C++ Objects Without Making Copies	</title>
	<atom:link href="https://www.fluentcpp.com/2018/07/17/how-to-construct-c-objects-without-making-copies/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.fluentcpp.com/2018/07/17/how-to-construct-c-objects-without-making-copies/</link>
	<description>Jonathan Boccara&#039;s blog</description>
	<lastBuildDate>Thu, 18 Oct 2018 20:45: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: thomas		</title>
		<link>https://www.fluentcpp.com/2018/07/17/how-to-construct-c-objects-without-making-copies/#comment-1394</link>

		<dc:creator><![CDATA[thomas]]></dc:creator>
		<pubDate>Thu, 18 Oct 2018 20:45:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4008#comment-1394</guid>

					<description><![CDATA[Holding a reference with this API &quot;TextDisplayer(std::string const&#038; text)&quot; is bad API design. Giving the class a wide open template parameter also seems like suboptimal choice. Make it two classes, each with a name that says what it does without forcing the user to read code, something like &quot;TextViewDisplayer( std::string_view )&quot; and &quot;TextDisplayer( std::string )&quot;.]]></description>
			<content:encoded><![CDATA[<p>Holding a reference with this API &#8220;TextDisplayer(std::string const&amp; text)&#8221; is bad API design. Giving the class a wide open template parameter also seems like suboptimal choice. Make it two classes, each with a name that says what it does without forcing the user to read code, something like &#8220;TextViewDisplayer( std::string_view )&#8221; and &#8220;TextDisplayer( std::string )&#8221;.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Thomas Huxhorn		</title>
		<link>https://www.fluentcpp.com/2018/07/17/how-to-construct-c-objects-without-making-copies/#comment-1204</link>

		<dc:creator><![CDATA[Thomas Huxhorn]]></dc:creator>
		<pubDate>Fri, 03 Aug 2018 13:34:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4008#comment-1204</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/07/17/how-to-construct-c-objects-without-making-copies/#comment-1180&quot;&gt;Miguel Raggi&lt;/a&gt;.

That is a much better example! 

Of course, string_view, iterators and the return type of reversed() are non-owning and store a reference. I&#039;m still searching for the right words... They are all temporary objects. It is easy to see that they never have a dangling reference. Except they are used as non-temporary objects. 

What I want to say: Dangling references are very hard to debug. If people never see code which store references, people never write code which store references. And that should be our goal. To show the people to use the right techniques. 

If one need a non-owning string object: use std::string_view. It is primary designed to use as temporary object. If one need a permanent non-owning string object, use this or that pattern.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/07/17/how-to-construct-c-objects-without-making-copies/#comment-1180">Miguel Raggi</a>.</p>
<p>That is a much better example! </p>
<p>Of course, string_view, iterators and the return type of reversed() are non-owning and store a reference. I&#8217;m still searching for the right words&#8230; They are all temporary objects. It is easy to see that they never have a dangling reference. Except they are used as non-temporary objects. </p>
<p>What I want to say: Dangling references are very hard to debug. If people never see code which store references, people never write code which store references. And that should be our goal. To show the people to use the right techniques. </p>
<p>If one need a non-owning string object: use std::string_view. It is primary designed to use as temporary object. If one need a permanent non-owning string object, use this or that pattern.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Miguel Raggi		</title>
		<link>https://www.fluentcpp.com/2018/07/17/how-to-construct-c-objects-without-making-copies/#comment-1181</link>

		<dc:creator><![CDATA[Miguel Raggi]]></dc:creator>
		<pubDate>Fri, 27 Jul 2018 13:41:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4008#comment-1181</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/07/17/how-to-construct-c-objects-without-making-copies/#comment-1173&quot;&gt;Panos KJ&lt;/a&gt;.

Aah, I see. I&#039;m sorry, I don&#039;t know... :(]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/07/17/how-to-construct-c-objects-without-making-copies/#comment-1173">Panos KJ</a>.</p>
<p>Aah, I see. I&#8217;m sorry, I don&#8217;t know&#8230; 🙁</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Miguel Raggi		</title>
		<link>https://www.fluentcpp.com/2018/07/17/how-to-construct-c-objects-without-making-copies/#comment-1180</link>

		<dc:creator><![CDATA[Miguel Raggi]]></dc:creator>
		<pubDate>Fri, 27 Jul 2018 13:37:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4008#comment-1180</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/07/17/how-to-construct-c-objects-without-making-copies/#comment-1152&quot;&gt;Thomas Huxhorn&lt;/a&gt;.

Thank you for your comments.

Maybe I chose the example wrong. I didn&#039;t want to add more complexity. Have you ever used string views? They aren&#039;t owning either.

Imagine you want to make (function/object/whatever) that lets you see a container in reverse (a la rangesv3), so that you can do something like:
for (auto&#038;&#038; x : reversed(mycontainer))
{
// do stuff
}

But you don&#039;t want all the elements in mycontainer to be copied, as this would incur a nontrivial performance penalty. What can you do? Reversed must store a reference to myContainer. But then this won&#039;t work:
for (auto&#038;&#038; x : reversed(get_container()))

The solution proposed works on both cases, and never makes a copy. Of course, you could, in this case, do:
for (auto it = mycontainer.rbegin(); it != mycontainer.rend(); ++it)
but aside from begin ugly and not expressive, what if you instead want a filter? or any other &quot;view&quot; type thing?]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/07/17/how-to-construct-c-objects-without-making-copies/#comment-1152">Thomas Huxhorn</a>.</p>
<p>Thank you for your comments.</p>
<p>Maybe I chose the example wrong. I didn&#8217;t want to add more complexity. Have you ever used string views? They aren&#8217;t owning either.</p>
<p>Imagine you want to make (function/object/whatever) that lets you see a container in reverse (a la rangesv3), so that you can do something like:<br />
for (auto&amp;&amp; x : reversed(mycontainer))<br />
{<br />
// do stuff<br />
}</p>
<p>But you don&#8217;t want all the elements in mycontainer to be copied, as this would incur a nontrivial performance penalty. What can you do? Reversed must store a reference to myContainer. But then this won&#8217;t work:<br />
for (auto&amp;&amp; x : reversed(get_container()))</p>
<p>The solution proposed works on both cases, and never makes a copy. Of course, you could, in this case, do:<br />
for (auto it = mycontainer.rbegin(); it != mycontainer.rend(); ++it)<br />
but aside from begin ugly and not expressive, what if you instead want a filter? or any other &#8220;view&#8221; type thing?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Panos KJ		</title>
		<link>https://www.fluentcpp.com/2018/07/17/how-to-construct-c-objects-without-making-copies/#comment-1173</link>

		<dc:creator><![CDATA[Panos KJ]]></dc:creator>
		<pubDate>Fri, 27 Jul 2018 11:16:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4008#comment-1173</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/07/17/how-to-construct-c-objects-without-making-copies/#comment-1143&quot;&gt;Miguel Raggi&lt;/a&gt;.

Hey, thanks for the reply but you misunderstood me. I was just inspired by your post to ask a bit unrelated question. My class looks like:


class bar {
std::shared_ptr myptr; // could be a more expensive-to-copy resource
void foo() const &#038; {
// copy myptr and do work with it
}
void foo() &#038;&#038; {
// here I can move myptr
}
};


I know foo() will often be called on rvalues, and there&#039;s no reason to pay for what I don&#039;t need, but is there an alternative to writing the same method twice?]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/07/17/how-to-construct-c-objects-without-making-copies/#comment-1143">Miguel Raggi</a>.</p>
<p>Hey, thanks for the reply but you misunderstood me. I was just inspired by your post to ask a bit unrelated question. My class looks like:</p>
<p>class bar {<br />
std::shared_ptr myptr; // could be a more expensive-to-copy resource<br />
void foo() const &amp; {<br />
// copy myptr and do work with it<br />
}<br />
void foo() &amp;&amp; {<br />
// here I can move myptr<br />
}<br />
};</p>
<p>I know foo() will often be called on rvalues, and there&#8217;s no reason to pay for what I don&#8217;t need, but is there an alternative to writing the same method twice?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2018/07/17/how-to-construct-c-objects-without-making-copies/#comment-1171</link>

		<dc:creator><![CDATA[Jonathan Boccara]]></dc:creator>
		<pubDate>Fri, 27 Jul 2018 11:00:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4008#comment-1171</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/07/17/how-to-construct-c-objects-without-making-copies/#comment-1157&quot;&gt;Sergey Bastrakov&lt;/a&gt;.

Fixed, thanks!]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/07/17/how-to-construct-c-objects-without-making-copies/#comment-1157">Sergey Bastrakov</a>.</p>
<p>Fixed, thanks!</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Sergey Bastrakov		</title>
		<link>https://www.fluentcpp.com/2018/07/17/how-to-construct-c-objects-without-making-copies/#comment-1157</link>

		<dc:creator><![CDATA[Sergey Bastrakov]]></dc:creator>
		<pubDate>Wed, 25 Jul 2018 14:43:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4008#comment-1157</guid>

					<description><![CDATA[In the sentence &quot;This is the phenomenon described in The Formidable Const Reference That Isn’t Const.&quot; the last part was likely supposed to be a link to a recent post with the same name (from July 13th), but now it is just a plain text.]]></description>
			<content:encoded><![CDATA[<p>In the sentence &#8220;This is the phenomenon described in The Formidable Const Reference That Isn’t Const.&#8221; the last part was likely supposed to be a link to a recent post with the same name (from July 13th), but now it is just a plain text.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Thomas Huxhorn		</title>
		<link>https://www.fluentcpp.com/2018/07/17/how-to-construct-c-objects-without-making-copies/#comment-1152</link>

		<dc:creator><![CDATA[Thomas Huxhorn]]></dc:creator>
		<pubDate>Tue, 24 Jul 2018 09:59:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4008#comment-1152</guid>

					<description><![CDATA[You did a great job, I always like to read your blog. But storing references is _always_ the wrong design decision. So please don&#039;t show people to work around this, and another work around for that and don&#039;t forget that your code still explodes. This is why C++ has such a bad reputation.

The topic is very interesting. To choose between const&#038; or to move for function parameters is helpful to not duplicate code.  
A thing which displays text either owns it&#039;s content or not. But not both. If it not owns the content it have to ask the owner if there is something to display. That&#039;s the way to go. 

I have a (german) example which extend sort() with a visitor which can passed a lvalue or rvalue. You may want to take a look http://roboblog.fatal-fury.de/?p=3390
Thomas]]></description>
			<content:encoded><![CDATA[<p>You did a great job, I always like to read your blog. But storing references is _always_ the wrong design decision. So please don&#8217;t show people to work around this, and another work around for that and don&#8217;t forget that your code still explodes. This is why C++ has such a bad reputation.</p>
<p>The topic is very interesting. To choose between const&amp; or to move for function parameters is helpful to not duplicate code.<br />
A thing which displays text either owns it&#8217;s content or not. But not both. If it not owns the content it have to ask the owner if there is something to display. That&#8217;s the way to go. </p>
<p>I have a (german) example which extend sort() with a visitor which can passed a lvalue or rvalue. You may want to take a look <a href="http://roboblog.fatal-fury.de/?p=3390" rel="nofollow ugc">http://roboblog.fatal-fury.de/?p=3390</a><br />
Thomas</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Miguel Raggi		</title>
		<link>https://www.fluentcpp.com/2018/07/17/how-to-construct-c-objects-without-making-copies/#comment-1144</link>

		<dc:creator><![CDATA[Miguel Raggi]]></dc:creator>
		<pubDate>Fri, 20 Jul 2018 13:28:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4008#comment-1144</guid>

					<description><![CDATA[You are completely right! The forwarding reference is used in the deduction guide, not the constructor. Thank you for pointing it out! I&#039;ll contact Jonathan to update the text. 

What happened is that in my original implementation I was using a forwarding reference in the constructor too, (like template  TextDisplayer(U&#038;&#038; u)) but then realized I didn&#039;t need it, but never realized that it&#039;s not called a forwarding reference then.]]></description>
			<content:encoded><![CDATA[<p>You are completely right! The forwarding reference is used in the deduction guide, not the constructor. Thank you for pointing it out! I&#8217;ll contact Jonathan to update the text. </p>
<p>What happened is that in my original implementation I was using a forwarding reference in the constructor too, (like template  TextDisplayer(U&amp;&amp; u)) but then realized I didn&#8217;t need it, but never realized that it&#8217;s not called a forwarding reference then.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Miguel Raggi		</title>
		<link>https://www.fluentcpp.com/2018/07/17/how-to-construct-c-objects-without-making-copies/#comment-1143</link>

		<dc:creator><![CDATA[Miguel Raggi]]></dc:creator>
		<pubDate>Fri, 20 Jul 2018 13:24:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=4008#comment-1143</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/07/17/how-to-construct-c-objects-without-making-copies/#comment-1135&quot;&gt;Panos KJ&lt;/a&gt;.

I don&#039;t think the use case is the same. First, shared_ptr is a pretty small object, so copying or moving cost almost the same (except that moving does not update the control structure associated to a shared_ptr, but oh well). Also, you are not holding the shared_ptr by reference like, ever, so the use case is not the same.

I would pass-by-value-and-then-move, like so:

class MyClass
{
public:
    MyClass(std::shared_ptr ptr) : mypointer_(std::move(ptr)) {}
private:
    std::shared_ptr mypointer_
}

Then if you get passed an lvalue it copies, but if you get passed an rvalue it moves.

Compare this to what we were trying to accomplish in the post, in which if you get passed an lvalue it *references*, and if you get passed an rvalue it moves.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/07/17/how-to-construct-c-objects-without-making-copies/#comment-1135">Panos KJ</a>.</p>
<p>I don&#8217;t think the use case is the same. First, shared_ptr is a pretty small object, so copying or moving cost almost the same (except that moving does not update the control structure associated to a shared_ptr, but oh well). Also, you are not holding the shared_ptr by reference like, ever, so the use case is not the same.</p>
<p>I would pass-by-value-and-then-move, like so:</p>
<p>class MyClass<br />
{<br />
public:<br />
    MyClass(std::shared_ptr ptr) : mypointer_(std::move(ptr)) {}<br />
private:<br />
    std::shared_ptr mypointer_<br />
}</p>
<p>Then if you get passed an lvalue it copies, but if you get passed an rvalue it moves.</p>
<p>Compare this to what we were trying to accomplish in the post, in which if you get passed an lvalue it *references*, and if you get passed an rvalue it moves.</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
