C++ Logo

sg20

Advanced search

Re: [SG20] A draft paper to fix the range-based for loop to make it teachable

From: Nicolai Josuttis <nico_at_[hidden]>
Date: Tue, 10 Nov 2020 11:59:10 +0100
Wow, you teach:
 don't use range-based for loop at all
?

We have 4,5 million C++ programmers.
The percentage who know for_each() is pretty low
(despite that the next question is WHICH for_each()).
The percentage of programmers who understands the problem of the
rg-for-loop is even lower.

IMO, nothing and nobody will change the fact that the range-based for
loop now is THE loop of C++.
And I don't think deprecating it is an option.
It just should simply work.

And we can fix it.
So I don't understand why we shouldn't simply fix it.

Of course, you can still try to teach to use something else/better.



Am 10.11.2020 um 11:43 schrieb Yehezkel Bernat:
>
>
> On Tue, Nov 10, 2020 at 12:15 PM Nicolai Josuttis <nico_at_[hidden]
> <mailto:nico_at_[hidden]>> wrote:
>
> Thanks for the feedback.
>
> Am 10.11.2020 um 10:41 schrieb Yehezkel Bernat:
> >
> > 2. I agree this is an issue, and even range-based for loop with
> > initializer doesn't fully solve it as we still have to be careful when
> > to use the initializer and what part of the expression should be
> there.
> >
> They don't solve it at all,
> because this is not fixing the problem but a workaround.
>
> > 3. IMHO, the best and simplest solution is to use
> std::ranges::for_each,
> > because it holds the temporary till the end of the loop due to the
> > function call semantics.
> >
> IMO, this is also not a solution.
> This is also a workaround (I also have to teach after explaining the
> problem), which requires a lot of other explanations.
>
>
> Maybe we don't agree on what is considered as a solution.
> I'm totally fine with telling my students: "don't use range-based for
> loop, it has sharp edges and you may hurt yourself while using it. Use
> ranges::for_each instead, the usage is as easy as with range-based for
> and it doesn't suffer from the same issues."
> C++ has already enough things that we warn our students to not use and
> use safer alternatives instead.
> It's a bit sad to put range-based for in the same bucket, but it's not
> the end of the world, IMO, especially as some people were always against
> it and preferred using for_each instead (I think Jon Kalb mentioned it
> long time ago on cppchat).
>

-- 
Nicolai M. Josuttis
www.josuttis.de
+49 (0)531 / 129 88 86
+49 (0)700 / JOSUTTIS
Books:
 C++: http://cppstdlib.com, http://cppstd17.com,
      http://tmplbook.com, http://cppmove.com
 SOA: http://soa-in-practice.com

Received on 2020-11-10 04:59:14