<?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: Is std::for_each obsolete?	</title>
	<atom:link href="https://www.fluentcpp.com/2018/03/30/is-stdfor_each-obsolete/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.fluentcpp.com/2018/03/30/is-stdfor_each-obsolete/</link>
	<description>Jonathan Boccara&#039;s blog</description>
	<lastBuildDate>Fri, 25 May 2018 09:30:40 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.3</generator>
	<item>
		<title>
		By: Uri Goren		</title>
		<link>https://www.fluentcpp.com/2018/03/30/is-stdfor_each-obsolete/#comment-923</link>

		<dc:creator><![CDATA[Uri Goren]]></dc:creator>
		<pubDate>Tue, 10 Apr 2018 17:56:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1293#comment-923</guid>

					<description><![CDATA[In this example std::for_each looks elegant, but a slight complication ruins it. What if we want to print to an arbitrary stream rather than std::cout?

Add an ostream&#038; parameter to displayAsInstruction, that&#039;s easy enough.
But now we can&#039;t pass it to for_each, and the lambda returns, as ugly as can be:

ostream &#038;os = ...;
ranges::for_each(numbers, [&#038;] (int n) { displayAsInstruction(os, n);});  // Guy Fawkes smiley?

Perhaps std::bind would slightly improve it (depending how painful you find std::placeholders::_1).

The range based for loop doesn&#039;t suffer at all:

ostream &#038;os = ...;
for (auto number : numbers)
{
    displayAsInstruction(os, number);
}]]></description>
			<content:encoded><![CDATA[<p>In this example std::for_each looks elegant, but a slight complication ruins it. What if we want to print to an arbitrary stream rather than std::cout?</p>
<p>Add an ostream&amp; parameter to displayAsInstruction, that&#8217;s easy enough.<br />
But now we can&#8217;t pass it to for_each, and the lambda returns, as ugly as can be:</p>
<p>ostream &amp;os = &#8230;;<br />
ranges::for_each(numbers, [&amp;] (int n) { displayAsInstruction(os, n);});  // Guy Fawkes smiley?</p>
<p>Perhaps std::bind would slightly improve it (depending how painful you find std::placeholders::_1).</p>
<p>The range based for loop doesn&#8217;t suffer at all:</p>
<p>ostream &amp;os = &#8230;;<br />
for (auto number : numbers)<br />
{<br />
    displayAsInstruction(os, number);<br />
}</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2018/03/30/is-stdfor_each-obsolete/#comment-913</link>

		<dc:creator><![CDATA[Jonathan Boccara]]></dc:creator>
		<pubDate>Sun, 08 Apr 2018 14:55:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1293#comment-913</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/03/30/is-stdfor_each-obsolete/#comment-906&quot;&gt;Jon Kalb&lt;/a&gt;.

Thanks for pointing this out, Jon. This is an interesting point indeed, and I&#039;ve put an entry in my backlog to dig it further and hopefully come up with a more detailed analysis.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/03/30/is-stdfor_each-obsolete/#comment-906">Jon Kalb</a>.</p>
<p>Thanks for pointing this out, Jon. This is an interesting point indeed, and I&#8217;ve put an entry in my backlog to dig it further and hopefully come up with a more detailed analysis.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2018/03/30/is-stdfor_each-obsolete/#comment-912</link>

		<dc:creator><![CDATA[Jonathan Boccara]]></dc:creator>
		<pubDate>Sun, 08 Apr 2018 14:45:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1293#comment-912</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/03/30/is-stdfor_each-obsolete/#comment-905&quot;&gt;Luis Díaz Más&lt;/a&gt;.

That&#039;s a good point, I&#039;m noting in my backlog to work on a performance comparison between for_each and a range-based for loop. Will write about my findings. Thanks Luis!]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/03/30/is-stdfor_each-obsolete/#comment-905">Luis Díaz Más</a>.</p>
<p>That&#8217;s a good point, I&#8217;m noting in my backlog to work on a performance comparison between for_each and a range-based for loop. Will write about my findings. Thanks Luis!</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jon Kalb		</title>
		<link>https://www.fluentcpp.com/2018/03/30/is-stdfor_each-obsolete/#comment-906</link>

		<dc:creator><![CDATA[Jon Kalb]]></dc:creator>
		<pubDate>Fri, 06 Apr 2018 00:16:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1293#comment-906</guid>

					<description><![CDATA[Jonathan,
In your example you are passing a function address to a generic algorithm. This is fine for correctness, but Luis correctly raises the issue of the compiler&#039;s ability to inline the function call.

This is not relevant to the point that you are making but a better example for readers to follow would be to call an object with an inlined function call operator overload. Such calls can be inlined, whereas, in the general case, functions passed to generic algorithms by address cannot be inlined even if the function itself is an inline function.

Jon]]></description>
			<content:encoded><![CDATA[<p>Jonathan,<br />
In your example you are passing a function address to a generic algorithm. This is fine for correctness, but Luis correctly raises the issue of the compiler&#8217;s ability to inline the function call.</p>
<p>This is not relevant to the point that you are making but a better example for readers to follow would be to call an object with an inlined function call operator overload. Such calls can be inlined, whereas, in the general case, functions passed to generic algorithms by address cannot be inlined even if the function itself is an inline function.</p>
<p>Jon</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Luis Díaz Más		</title>
		<link>https://www.fluentcpp.com/2018/03/30/is-stdfor_each-obsolete/#comment-905</link>

		<dc:creator><![CDATA[Luis Díaz Más]]></dc:creator>
		<pubDate>Thu, 05 Apr 2018 18:07:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1293#comment-905</guid>

					<description><![CDATA[What about performance? Is std::for_each able to inline the function body to avoid function calls, or will it make functions calls for each element in the collection? I think it is an important factor to consider when deciding which version to use.]]></description>
			<content:encoded><![CDATA[<p>What about performance? Is std::for_each able to inline the function body to avoid function calls, or will it make functions calls for each element in the collection? I think it is an important factor to consider when deciding which version to use.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Arnaud Brejeon		</title>
		<link>https://www.fluentcpp.com/2018/03/30/is-stdfor_each-obsolete/#comment-903</link>

		<dc:creator><![CDATA[Arnaud Brejeon]]></dc:creator>
		<pubDate>Thu, 05 Apr 2018 01:05:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1293#comment-903</guid>

					<description><![CDATA[My most common pattern for using std::for_each is something like this:

const auto endOfPartition = std::partition(begin, end, []() { predicate code });
std::for_each(begin, endOfPartition, []() { whatever processing });]]></description>
			<content:encoded><![CDATA[<p>My most common pattern for using std::for_each is something like this:</p>
<p>const auto endOfPartition = std::partition(begin, end, []() { predicate code });<br />
std::for_each(begin, endOfPartition, []() { whatever processing });</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Shreyans		</title>
		<link>https://www.fluentcpp.com/2018/03/30/is-stdfor_each-obsolete/#comment-902</link>

		<dc:creator><![CDATA[Shreyans]]></dc:creator>
		<pubDate>Mon, 02 Apr 2018 21:19:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1293#comment-902</guid>

					<description><![CDATA[I have slightly different point of view for these 2 ways to achieve the same thing. If the function inside the loop is stateless (independent of order in which it will be called for all the values in the container), I go for for_each, otherwise I go for range based for loop.
Reason behind this is, in future, there&#039;s a chance of getting parallel for each with similar API. This way, we can reduce future efforts to make it parallel if required.
If it is stateful, there&#039;s no way but to perform the action sequentially and range based for loop meets the requirements for that.]]></description>
			<content:encoded><![CDATA[<p>I have slightly different point of view for these 2 ways to achieve the same thing. If the function inside the loop is stateless (independent of order in which it will be called for all the values in the container), I go for for_each, otherwise I go for range based for loop.<br />
Reason behind this is, in future, there&#8217;s a chance of getting parallel for each with similar API. This way, we can reduce future efforts to make it parallel if required.<br />
If it is stateful, there&#8217;s no way but to perform the action sequentially and range based for loop meets the requirements for that.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Vincent Zalzal		</title>
		<link>https://www.fluentcpp.com/2018/03/30/is-stdfor_each-obsolete/#comment-900</link>

		<dc:creator><![CDATA[Vincent Zalzal]]></dc:creator>
		<pubDate>Mon, 02 Apr 2018 20:09:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1293#comment-900</guid>

					<description><![CDATA[If the argument of abstraction isn&#039;t enough to convince one of the usefulness of std::for_each, here is another: since C++17, one can pass an ExecutionPolicy to for_each for easy parallelism. And yes, that small ranges namespace is totally worth it. At work, I have done exactly the same for _all_ algorithms, and since then, they are used much more! (note: we weren&#039;t allowed to use boost at the time).]]></description>
			<content:encoded><![CDATA[<p>If the argument of abstraction isn&#8217;t enough to convince one of the usefulness of std::for_each, here is another: since C++17, one can pass an ExecutionPolicy to for_each for easy parallelism. And yes, that small ranges namespace is totally worth it. At work, I have done exactly the same for _all_ algorithms, and since then, they are used much more! (note: we weren&#8217;t allowed to use boost at the time).</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: sephorusFR		</title>
		<link>https://www.fluentcpp.com/2018/03/30/is-stdfor_each-obsolete/#comment-899</link>

		<dc:creator><![CDATA[sephorusFR]]></dc:creator>
		<pubDate>Mon, 02 Apr 2018 16:01:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1293#comment-899</guid>

					<description><![CDATA[I would say, it&#039;s the say cullprit than with if vs ?: We tend to use if if the important thing is to claim &quot;guys, this is a conditionnal piece of code&quot; and we use ?: to say that we&#039;re selecting value.  for range loop claims this is important this piece of code is a loop, for_each put the emphasizes on what happens to the range. My 2 euro-cents]]></description>
			<content:encoded><![CDATA[<p>I would say, it&#8217;s the say cullprit than with if vs ?: We tend to use if if the important thing is to claim &#8220;guys, this is a conditionnal piece of code&#8221; and we use ?: to say that we&#8217;re selecting value.  for range loop claims this is important this piece of code is a loop, for_each put the emphasizes on what happens to the range. My 2 euro-cents</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2018/03/30/is-stdfor_each-obsolete/#comment-893</link>

		<dc:creator><![CDATA[Jonathan Boccara]]></dc:creator>
		<pubDate>Fri, 30 Mar 2018 16:54:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1293#comment-893</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/03/30/is-stdfor_each-obsolete/#comment-891&quot;&gt;Helmut Mülner&lt;/a&gt;.

Yes, thanks Helmut, it&#039;s fixed now :)]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/03/30/is-stdfor_each-obsolete/#comment-891">Helmut Mülner</a>.</p>
<p>Yes, thanks Helmut, it&#8217;s fixed now 🙂</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
