On Tue, Nov 10, 2020 at 12:15 PM Nicolai Josuttis <nico@josuttis.de> 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).