C++ Logo

SG20

Advanced search

Subject: Re: A draft paper to fix the range-based for loop to make it teachable
From: Peter C++ (peter.cpp_at_[hidden])
Date: 2020-11-10 05:53:28


Nico,

Since many years I am teaching C++ algorithm first. And it works. It is a matter of attitude. And range for is only useful, if your algorithm is not already in the standard library, or not convenient in the standard library.

OTOH, fixing range for for temporaries is valuable. There is no need for that safety risk.

Peter

sent from a mobile device so please excuse strange words due to autocorrection.
Peter Sommerlad
peter.cpp_at_[hidden]
+41-79-432 23 32

> On 10 Nov 2020, at 11:59, Nicolai Josuttis via SG20 <sg20_at_[hidden]> wrote:
>
> 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
>
> --
> SG20 mailing list
> SG20_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/sg20


SG20 list run by sg20-owner@lists.isocpp.org

Older Archives on Google Groups