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 13:47:28 +0100
Am 10.11.2020 um 12:53 schrieb Peter C++:
> 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.
>

Sure, teach in whichever way you like.
That's fine.
I can also teach C++ that way.

My usual trainings are advanced trainings.
In the past there was a lot about the new range-based for loop.
As we didn't have lambdas a lot of programmers hate algorithms.
This might change, but still, a simple loop has its own value
(and may be because you have to implement the algorithms somehow).

Note also that Autosar recommends to prefer the range-based for loop,
whenever it is possible to use it.
That's a very dangerous recommendation right now.

All I say is that whenever somebody for the first time
learns the range-based for loop, it should just work.
And whenever I have to teach this loop I should have no obligation
to explain why/when it is broken.
KISS.


> 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

-- 
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 06:47:33